I have written some data structures in C like task queue used by thread pool. It is synchronized with mutex and conditional variable objects. I make it like this:
struct task_queue {
// some fields
};
typedef struct task_queue task_queue_t;
static pthread_mutex_t mutex;
static pthread_cond_t cond;
task_queue_init(task_queue_t **tq);
task_queue_destroy(task_queue_t *tq);
But now I think that such approach is rather wrong as when I create with task_queue_init() several instances of task_queue struct in my program than they will be synchronized by the same pthread_mutex_t, pthread_cond_t objects. I think that I have seen such static synchronization objects declarations somewhere previously and I have used it in my code.
My question is to make sure myself that I am planning to do it right way i.e put this synchronization objects always inside this struct task_queue, or other synchronized data structure struct like below:
struct task_queue {
// hitherto fields
pthread_mutex_t mutex;
pthread_cond_t cond;
}
typedef struct task_queue task_queue_t;
//and initialize/destroy them in
task_queue_init(task_queue_t **tq);
task_queue_destroy(task_queue_t *tq);
Maybe I should use pointers to mutex, cond in this struct?