Linux Processor Spinning C++

Dan

The New Helper.Net gives me great Anxiety... o.O;;
Hey guys, I'm coming to you because I love you.

I am working on a project in C++ and I was wondering if anyone knew how Linux handles sharing the processor as a resource across threads. Essentially, I have multiple threads that are going to be waiting for a flag to change. A colleague of mine said that a while loop waiting on a duration of time to pass might cause "spinning" to occur and make threads fight for resources.

My colleague wrote the original code-base for the project I'm porting over to Linux. It was an embedded system that uses something called Keil OS, so it handles threads and deals with Interrupt Scheduling.

Thoughts?
 

Slapshot136

Divide et impera
Normally there is an implementation to notify threads such that you don't need to wait a specified amount of time like mutex and semaphore - as long as there is some wait though, it shouldn't be too bad of a spin , assuming you mean CPU resources and a reasonable number of threads (say under 100) - if you need access to other stuff (static instances, etc.) then you need to be even more careful about locks to avoid stuff like the dining philosophers issue - not an expert but in linux/c++ environments I believe it's usually done via pthread and the related functions

as far as the actual CPU time scheduling and time-slicing, those still vary a ton depending on the OS/CPU itself, but I wouldn't really worry too much about that unless your 100% sure of what your platform will be AND have a reason to optimize further (premature optimization is the root of all evil)
 
Last edited:

Accname

2D-Graphics enthusiast
Really depends on how long you want to wait. If its rather short (below seconds) then busy-waiting (while loop) is okay. If its longer you should send your thread to sleep and check regularily. If its a little bit longer but not too long you could yield CPU ownership.
A yield means that your thread gives up the CPU if the CPU is requested by another thread. If the CPU has nothing better to do your thread will keep it. Sleeping is forced and will block your thread until interrupted by the OS (after a certain time for example).

Something like this (in pseudo code):
Code:
loop {
    if (condition is met?) {
        break loop
    }
    yield // or sleep(100 millis)
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    Yeah we got nothing here did not even rain
  • Ghan Ghan:
    We could use some rain.
  • The Helper The Helper:
    I am not complaining though Hurricanes are not fun I have been through alot of them
  • Blackveiled Blackveiled:
    I evacuated for it just incase since I live in La Porte pretty much right on the water (in Houston area). Didn't see anything. It's a good thing for me anyways.
  • Ghan Ghan:
    It's too bad that Blizzard messed up so terribly with Reforged.
  • Ghan Ghan:
    Still has a 0.6 user metacritic score.
  • Ghan Ghan:
    (Out of 10)
  • Varine Varine:
    I haven't even tried it yet because everyone said it sucked. How do you get .6 out of ten?
  • The Helper The Helper:
    I am waiting for Dwarf Fortress to come out on Steam then I am on it.
  • Accname Accname:
    I play Dwarf Fortress from time to time.
  • Accname Accname:
    Its Okay.
  • Accname Accname:
    But the performance is sub-par. Does not seem to be well optimized.
  • Varine Varine:
    Is the Steam version coming with a graphical overlay or is it using the ASCII graphics still?
  • tom_mai78101 tom_mai78101:
    Steam version has the graphical overlay.
  • The Helper The Helper:
    Steam Version has graphics, a new interface and will be integrated in the steam mod stuff
  • Blackveiled Blackveiled:
    I didn't think WC3 Reforged was that bad. It pretty much did what it stated (to me at least), but then again I can care less about reforged campaigns and all that. I just care about gameplay.
  • Varine Varine:
    I think people were hoping for a resurgence in mod support. I have little interest in the actual game tbh
  • Varine Varine:
    Plus I know they added that Dota precedent clause where Blizzard can steal your shit which isn't cool.
  • Ghan Ghan:
    They sold Reforged to the community as primarily a huge graphical uplift with high res textures and redone cinematics.
  • Ghan Ghan:
    They even showed one of the redone cinematics at Blizzcon and that very cinematic wasn't even in the released game.
  • Ghan Ghan:
    So I think folks were angry about that.
  • Varine Varine:
    Didn't the backwards compatability have issues too?
  • Accname Accname:
    I never had any hopes for that. Blizzard is not the same company it was 10 years ago.
  • The Helper The Helper:
    It happens in corporations. They just absorb the companies they buy and it is not about the love of making awesome games it is how much money can we make with the least amount of cost.
  • The Helper The Helper:
    Blizzard is watered down now hopefully they can pull it together

    Members online

    No members online now.

    Affiliates

    Hive Workshop
    Top