Advice / best practice - thread connection pools / mutexes
martin at lispworks.com
Tue Apr 27 10:18:09 UTC 2010
Assuming it is safe share MySQL connections between threads like that (I don't
know), then using a data structure to track the unused connections sounds like
the simplest approach.
Protect access to the data structure with a mutex and use a condition variable
to wait (with pthread_cond_wait) for a free connection if they are all in use.
When a connection is freed, use pthread_cond_signal to wake a waiting thread.
Note that both pthread_cond_wait and pthread_cond_signal must be called while
holding the mutex to prevent missed wakups.
More information about the freebsd-threads