Currently, the worker function performs unnecessary actions:
void *worker(void *arg) {
tpool *tp = (tpool *)arg;
for (;;) {
pthread_mutex_lock(&tp->tpool_lock);
......
if (...) {
......
pthread_mutex_unlock(&tp->tpool_lock);
return NULL;
}
.........
pthread_mutex_unlock(&tp->tpool_lock);
j.f(j.arg); // run the job
pthread_mutex_lock(&tp->tpool_lock);
......
pthread_mutex_unlock(&tp->tpool_lock);
}
}
you can rewrite code this way:
void *worker(void *arg) {
tpool *tp = (tpool *)arg;
pthread_mutex_lock(&tp->tpool_lock);
for (;;) {
......
if (...) {
......
break;
}
.........
pthread_mutex_unlock(&tp->tpool_lock);
j.f(j.arg); // run the job
pthread_mutex_lock(&tp->tpool_lock);
......
}
pthread_mutex_unlock(&tp->tpool_lock);
return NULL;
}
thus, at each iteration of the loop, only 1 lock and 1 unlock occur
Currently, the worker function performs unnecessary actions:
you can rewrite code this way:
thus, at each iteration of the loop, only 1 lock and 1 unlock occur