Thread: pthread question how would I init this data structure?

  1. #1
    Registered User
    Join Date
    Sep 2008

    Question pthread question how would I init this data structure?

    Hi guys,

    I'm very new to pthreads and I'm not sure exactly how I would init this data structure.
    I have the following data structure:
    /* thread work */
    typedef struct thread_work {
      FILE *fp;
      int fd;
      struct thread_work *next;
    } thread_work_t;
    /* a thread pool */
    typedef struct tpool {
      /* pool attributes */
      int num_threads;
      /* pool state */
      pthread_t *threads;
      /* receiver mutexes */
      pthread_mutex_t read_ipc_lock;     /* need to read from ipc channel */
      pthread_mutex_t next_entry_lock;   /* need to update what the next entry to write is */
      pthread_mutex_t write_file_lock;   /* need to write to the file when it's your turn */
      pthread_mutex_t last_entry_lock;   /* need to identify the last entry */
      /* cond vars */
      pthread_cond_t next_entry_check;   /* cond var for waiting for your turn */
    } tpool_t;
    /* one each for child and parent */
    tpool_t *tp;   /* thread pool */
    extern int thread_pool_init( int poolSize );
    extern int run_pool_threads( FILE *fp, int fd, void *(*threadfn)(void *));

    and I'm going to use this function to initiate the data structure:
        Function    : thread_pool_init
        Description : Pthreads version to initialize pool of size poolSize
        Inputs      : poolSize - number of threads in the thread pool
        Outputs     : 0 if successful, -1 if failure
    int thread_pool_init( int poolSize )
      /* initialize thread pool data structure */ 
      /* everything is OK */
      return 0;
    Now would I simply use malloc? I'm confused because I have a thread_work data structure and a tpool data structure. I'm assuming the tpool data structure is where I would use the poolSize. perhaps in a for loop or would i simply set the tp->num_threads = poolSize and not use malloc at all?. And the thread_work thread I think I would just use malloc once right?

    Something like:
     thread_work_t *t_work;
    	if (( t_work = (thread_work_t *) malloc( sizeof(thread_work_t) ) ) == NULL) {

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    So how are thread_work_t and tpool_t related?

    I mean, you have this, but the comment and declaration make no sense
    /* pool state */
    pthread_t *threads;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Hi thanks for the responce, well the program will do the following:
    it will take an unsorted input and writes it to a file in the proper order. The threads have their own local store of buffers that they have read, but otherwise share access to the source pipe, destination file, the index of the current entry to write, and the index of the last entry.

    The main function in the receiver will call two functions that create and manage threads for this project: thread_pool_init, which initializes the tpool_t data structure, and run_pool_threads, which creates the threads that run the function rcv_file. Initially, create one such thread, but ultimately six threads must be run.

    When a thread reads a line, the line is placed in a store. These stores are of limited size, so the you must ensure that a store does not overflow. If a store becomes full, then you must cause thread to wait until it has the next_entry line before it can proceed. I will make sure that the input file does not cause all stores to fill.

    Here is the psudo code:
         rcv_file( input_ipc, outfile )
           initialize( entry_store );
           initialize( next_entry = 1 );
           while ( more_to_receive && more_to_write ) {
              if ( more_to_receive ) {
    	     read_entry( input_ipc, entry_store );
    	     determine if more_to_receive;
              get lowest_read from entry_store;
    	  while (TRUE) {
          	      if ( lowest_read == next_entry ) {
    	         write_entry( outfile, entry_store, lowest_read );
    	         incr next_entry
    	         get lowest_read
    	         determine if more_to_write 
    	         if ( !more_to_write ) break
              else break;
         read_entry( input_ipc, entry_store )
            initialize( buf, max_size );
            bytes = mypipe_read( input_ipc, buf, max_size );
            if ( buf has bytes ) {
              get_index( buf, index );
    	  check_for_eof( buf );
              store_entry( buf, index, bytes, entry_store );
              return index
    	else {
    	  dealloc( buf )
    	  return unusable index
         write_entry( outfile, entry_store, lowest_read )
           buf = get_entry_from_store( entry_store, lowest_read );
           write( outfile, buf );
           dealloc( buf );
    I'm not sure if this helps at all, the professor supplied the library code but didn't explain how they correlate and I was hoping any of you could take a guess at what it might be doing there.

    He mentioned this:

    With a single thread, you can put the mutual exclusion code in to protect the shared data structures. You must identify these shared data structures and add the appropriate pthreads code for mutual exclusion. The variables for mutual exclusion are provided in the data structure tpool_t.

    Then, try two threads at a time. In this case, you will need to use a condition variable for a thread to wait and be awoken. The condition variable is also defined in tpool_t.

    So to answer your question, can that pointer be used to perhaps switch between threads in the pool thread? LIke that thread pointer will point to the active thread?
    Last edited by mr_coffee; 02-23-2009 at 12:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. Deleting Data within a Structure or class
    By TankCDR in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 10:37 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM