Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 804571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:56:29+00:00 2026-05-14T23:56:29+00:00

I have this POSIX thread: void subthread(void) { while(!quit_thread) { // do something …

  • 0

I have this POSIX thread:

void subthread(void)
{
  while(!quit_thread) {

     // do something
     ...

     // don't waste cpu cycles
     if(!quit_thread) usleep(500);
  }

  // free resources
  ...

  // tell main thread we're done
  quit_thread = FALSE;
}

Now I want to terminate subthread() from my main thread. I’ve tried the following:

quit_thread = TRUE;

// wait until subthread() has cleaned its resources
while(quit_thread);

But it does not work! The while() clause does never exit although my subthread clearly sets quit_thread to FALSE after having freed its resources!

If I modify my shutdown code like this:

quit_thread = TRUE;

// wait until subthread() has cleaned its resources
while(quit_thread) usleep(10);

Then everything is working fine! Could someone explain to me why the first solution does not work and why the version with usleep(10) suddenly works? I know that this is not a pretty solution. I could use semaphores/signals for this but I’d like to learn something about multithreading, so I’d like to know why my first solution doesn’t work.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:56:30+00:00Added an answer on May 14, 2026 at 11:56 pm

    What’s most likely to be happening is that your compiler is not aware that quit_thread can be changed by another thread (because C doesn’t know about threads, at least at the time this question was asked). Because of that, it’s optimising the while loop to an infinite loop.

    In other words, it looks at this code:

    quit_thread = TRUE;
    while(quit_thread);
    

    and thinks to itself, “Hah, nothing in that loop can ever change quit_thread to FALSE, so the coder obviously just meant to write while (TRUE);“.

    When you add the call to usleep, the compiler has another think about it and assumes that the function call may change the global, so it plays it safe and doesn’t optimise it.

    Normally you would mark the variable as volatile to stop the compiler from optimising it but, in this case, you should use the facilities provided by pthreads and join to the thread after setting the flag to true (and don’t have the sub-thread reset it, do that in the main thread after the join if it’s necessary). The reason for that is that a join is likely to be more efficient than a continuous loop waiting for a variable change since the thread doing the join will most likely not be executed until the join needs to be done.

    In your spinning solution, the joining thread will most likely continue to run and suck up CPU grunt.

    In other words, do something like:

    Main thread              Child thread
    -------------------      -------------------
    fStop = false
    start Child              Initialise
    Do some other stuff      while not fStop:
    fStop = true                 Do what you have to do
                             Finish up and exit
    join to Child
    Do yet more stuff
    

    And, as an aside, you should technically protect shared variables with mutexes but this is one of the few cases where it’s okay, one-way communication where half-changed values of a variable don’t matter (false/not-false).

    The reason you normally mutex-protect a variable is to stop one thread seeing it in a half-changed state. Let’s say you have a two-byte integer for a count of some objects, and it’s set to 0x00ff (255).

    Let’s further say that thread A tries to increment that count but it’s not an atomic operation. It changes the top byte to 0x01 but, before it gets a chance to change the bottom byte to 0x00, thread B swoops in and reads it as 0x01ff.

    Now that’s not going to be very good if thread B want to do something with the last element counted by that value. It should be looking at 0x0100 but will instead try to look at 0x01ff, the effect of which will be wrong, if not catastrophic.

    If the count variable were protected by a mutex, thread B wouldn’t be looking at it until thread A had finished updating it, hence no problem would occur.

    The reason that doesn’t matter with one-way booleans is because any half state will also be considered as true or false so, if thread A was halfway between turning 0x0000 into 0x0001 (just the top byte), thread B would still see that as 0x0000 (false) and keep going (until thread A finishes its update next time around).

    And if thread A was turning the boolean into 0xffff, the half state of 0xff00 would still be considered true by thread B so it would do its thing before thread A had finished updating the boolean.

    Neither of those two possibilities is bad simply because, in both, thread A is in the process of changing the boolean and it will finish eventually. Whether thread B detects it a tiny bit earlier or a tiny bit later doesn’t really matter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Solution 1) Clean your VS.Net Solution 2) Rebuild Project. 3)… May 15, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer You don't have to do anything for this. The control… May 15, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer What about a JavaScript object? var map = {}; map["ID1"]… May 15, 2026 at 8:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.