Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

Alternatives to CPU intensive while loop in Linux C

Follow up from this question about atomic i++:

http://www.experts-exchange.com/Programming/Languages/C/Q_26488392.html?cid=239#a33725922

I would like to avoid doing a:

while (ring_buffer.count > 0);

If I was using mutex, I could use a conditional mutex which would effectively make the waiting thread sleep until it was alerted some data was ready. I'm trying to avoid mutex in this case so I would love an alternative.

Code snippet requested :-)
0
PMembrey
Asked:
PMembrey
  • 4
  • 2
1 Solution
 
phoffricCommented:
For the reader to avoid doing a polling loop in their reading thread, then it needs to wait on a signal (a counting semaphore can be used to implement that - not a mutex). But it sounds like you do not wish to use any kernel calls, so in that case, you have to poll (with at least some sleep).
0
 
PMembreyAuthor Commented:
That's what I was afraid of :-)
0
 
phoffricCommented:
Using a signal (as opposed to mutex) should not result in performance degradation.
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
evilrixSenior Software Engineer (Avast)Commented:
There is little you can do to avoid a "busy" wait as long as the thread is getting a quantum. It will continue to do this unless the kernel is told to make it sleep. It will do this either if the thread is waiting on a kernel primitive (such as an event or mutex) or if you have called sleep() to tell it to yield it's quantum. The latter will still result in a busy wait but at least the thread will yield preventing thread starvation for other threads.

Any reason you don't want to use a proper threading primitive? More info would allow to give a refined answer.
0
 
phoffricCommented:
If the reader is waiting on a semaphore signal from the writer, then it doesn't have to be that the signal is sent for each byte (or even each item). Instead, the writer could send a signal when there is a batch of items for the reader to process. The right balance for maximum throughput and reducing kernel overhead is application dependent.
0
 
PMembreyAuthor Commented:
Can't win them all :)
0
 
phoffricCommented:
If you want to discuss the details of your real-time requirement(s), we may be able to help soundboard your architecture for better performance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now