Alternatives to CPU intensive while loop in Linux C

Posted on 2010-09-21
Last Modified: 2012-05-10
Follow up from this question about atomic i++:

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 :-)
Question by:PMembrey
  • 4
  • 2
LVL 32

Expert Comment

ID: 33726286
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).

Author Comment

ID: 33726326
That's what I was afraid of :-)
LVL 32

Expert Comment

ID: 33726342
Using a signal (as opposed to mutex) should not result in performance degradation.
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

LVL 40

Expert Comment

ID: 33726823
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.
LVL 32

Accepted Solution

phoffric earned 500 total points
ID: 33726890
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.

Author Closing Comment

ID: 33731618
Can't win them all :)
LVL 32

Expert Comment

ID: 33731678
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.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now