How to make asynchronous communication between threads on a platform using pthreads
For example, windows have MsgWaitForMultipleObjects, which allows you to asynchronously wait for Windows messages, socket events, asynchronous io (IOCompletionRoutine), and AND Mutex.
On Unix, you have a choice / poll that gives you everything but the ability to break out when some pthread_mutex is unlocked.
History:
I have an application that has a main thread that is doing something with multiple sockets, pipes or files. Now from time to time there is side work (db transaction) that can take longer and if done synchronously on the main thread it will break normal socket service. So I want to perform the db operation on a separate thread. This thread will wait for some mutex in standby mode until the main thread decides to give it some task and unlocks the mutex so that the db thread can grab it. The problem is how the db thread can notify the main thread when the job has finished. The main thread has to handle sockets, so it cannot afford to sleep in pthread_mutex_lock. Doing a periodic pthread_mutex_trylock is the last thing I would like to do.I am currently considering using a pipe, but is it any better?
Using a pipe is a good idea here. Make sure no other process has an end-of-write for the open pipe, then select () or poll () on the main thread to read for read. When the worker thread is done with work, close () the end of the entry. Select () on the main thread wakes up immediately.
I don't think there is a mutex waiting and something else will be possible because in Linux mutexes are implemented with the futex (2) system call, which does not support file descriptors.
a source to share