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:
Code:
/* 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:
Code:
/**********************************************************************
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:
Code:
thread_work_t *t_work;
if (( t_work = (thread_work_t *) malloc( sizeof(thread_work_t) ) ) == NULL) {
Thanks