Question

I'm currently programming an application that manages parallel rendering on several computers. For that, I have on my server application a pool of threads that manages the TCP connections, one thread for every question. I'd like to use the same condition variable in every TCP thread to warn an other thread if one of the network threads received data.

Problem : to use the signal function in the POSIX api, I have to use a mutex. That means that if 2 network threads receive data at the same time, they will have to wait for the mutex to become free to continue execution.

Realistically, will the use of this condition variable really slow down my code? I also can, if I don't want to use it, make in the other thread (the one who calls wait on the condition variable) a while instruction that loops to see if my network threads received something.

Thanks a lot :)

Was it helpful?

Solution

Probably not. If all you do is locking the mutex and signaling the condition variable it is no worse than getting the next item in a mutex-protected work queue.

You could theoretically replace your wait on the condition variable with a while loop polling a flag, but then you must make sure to use proper synchronization (memory barriers) when accessing this flag. This will also come at a cost. A busy wait is also not ideal for various reasons, and adding a sleep will introduce delay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top