Question

I am using a canonical ring buffer implementation in a 1Reader thread/1Writer thread setting.

Since the reader loops when the buffer is empty [the writer loops when the buffer is full] and continously polls the control variables, I call pthread_yield (that in my case is only a wrapper to sched_yield) to give priority to other threads in the system. I am not using any mutex because it is not needed for proper functioning.

Is there a better way to mitigate the polling effects (a.k.a. CPU burning) ? I was thinking of the pthread's condition variables - since I mostly block the thread when there is no data [no space] - but I am afraid of the overhead could introduce.

Thanks

Was it helpful?

Solution

Use condition variables, the overhead is much lower than busy waiting, and using mutexes correctly ensures that your data is actually there when you expect it to be (since they enforce ordering).

In addition, if you really don't need the mutex for the general case, lock contention should be low to non-existent.

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