views:

141

answers:

1

I have a number of data containers that can send a signal when there are updates in them. The structure looks similar like this:

typedef struct {
  int data;
  /*...*/
  pthread_cond_t *onHaveUpdate;
} Container;

The onHaveUpdate is a pointer to a global condition that is shared by all the containers

In my application, I have a number of these structures and they can concurrently be updated by different threads.

Now, is it possible for me to have a thread that listens to the condition and can perform something on the container that sends the notification?

I know that this can be solved by using one thread per container but it feel like a waste of resources, but I was wondering if this can be done using only one thread for all container?

A: 

The problem is that your condition is shared by all containers, so when you send the condition that something has changed, you have no idea what has changed. Since the architecture of your application is not a 100% clear from your context, why not implement a Queue which holds events (and pointers to containers are pushed onto that Queue) and a worker thread which takes events from that waitqueue and performs the work. That way your worker queue needs to wait on the condition that the queue is filled (or run in a non aggressive while-true-fashion), and that way you can remove the condition variable from your container altogether.

amo-ej1
Sounds like a nice idea. Haven't implemented a event queue before but it sounds like a easy thing to do. Thanks!
DeeD