Assuming you know how many threads will be calling PartialCodec
, you can use static
variables within the function to facilitate communication between threads. A bare static
would allow all threads in PartialCodec
to manipulate the same object instance.
void * PartialCodec (void *arg) {
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static struct state {
/* some state */
} critical;
struct state local;
pthread_mutex_lock(&lock);
local = critical; /* make a local copy */
/* update critical */
pthread_mutex_unlock(&lock);
/* ... refer to local copy of state ... */
}
The critical
state tracks which part of the problem a particular thread should solve. Copying it to local
establishes that the thread will work on that part of the problem. The critical
state is then updated so the next thread that reads critical
will know to work on a different part of the problem.