Solved

Alternatives to CPU intensive while loop in Linux C

Posted on 2010-09-21
7
457 Views
Last Modified: 2012-05-10
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
Comment
Question by:PMembrey
  • 4
  • 2
7 Comments
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
 

Author Comment

by:PMembrey
Comment Utility
That's what I was afraid of :-)
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Using a signal (as opposed to mutex) should not result in performance degradation.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:PMembrey
Comment Utility
Can't win them all :)
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

9 Experts available now in Live!

Get 1:1 Help Now