std::thread question

Started by
9 comments, last by lomateron 10 years, 3 months ago

I just started using threads and I am having a problem were an INT global variable that is modified by a CREATED detached thread does not has the value I expect it to have after the CREATED thread finishes, how do I know the CREATED thread finished? I change to TRUE a global variable at the end of the function the CREATED thread is running. So in the MAIN thread (not exactly in the MAIN function code but inside a function, inside another function called by the MAIN) I check if this bool is true, if it is then I read the INT value but it isn't what I want it to be.

What I want to know is where can I find good tutorials about what not to do when using global variables in threads.

Advertisement

http://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/

good tutorials about what not to do when using global variables in threads.

1) don't use global variables to communicate between threads.

2) learn about compiler optimizations, CPU data reordering, atomics, and the C++ memory model.

Watch the videos linked above, or search the terms I just provided.

Sean Middleditch – Game Systems Engineer – Join my team!

Read the documentation, especially about std::thread::joinable instead of using something blindly. Especially in C++ 'just trying it out' is not a good plan of action. A lot of things appear to work and will then stop working at the most inconvenient time; maybe days, weeks or motnhs later.

If you have to use a global variable between threads, your have to protect it as a critical section. See std::mutex. Better still, don't use global variables. You may have read that before, but it's even more true for threaded programming so read it again and listen this time.

From your description, it sounds like you may want to use std::async and std::future instead of std::thread.

Stephen M. Webb
Professional Free Software Developer

what is the best way for the MAIN thread to know when the CREATED thread finished?

I don't want to use join() because it blocks the execution of the MAIN thread. The MAIN thread is an infinite loop till I close the app

As mentioned above, you can use a variable that's shared between two threads, however it must be protected by a mutex/etc.

I never used std thread, but doing a search on google for "std thread" should give you all the info you need, and i suggest you read a bit about them before starting using threads. I think what you need is this.

Since when does C++ have standardized threading? Aww man. I gotta try this out.

Since when does C++ have standardized threading? Aww man. I gotta try this out.

Since C++11, so not that long ago, really.

I am using now std::async,

*I want to run fuction() async when I make a mouse click,

*I don't want to run function() if it is still running, like when I make lots of fast mouseclicks

*when function() finishes I want to run other code

this is the code:


#include <future>
#include <thread>
#include <chrono>

bool closeApp=false;
bool mouseClick=false;

std::future<void> futur;

void function()
{
     ...some code....
}

main()
{
      futur = std::async(std::launch::async, [] {int jil=3;}); //this code line is to just make futur...future_status...ready

      while(!closeApp)
      {
            if(mouseClick)
            {
                    //if I make lots of fast mouse clicks, this will prevent from running function()
                    if(futur.wait_for(std::chrono::milliseconds(0))==std::future_status::ready)
                    {
                              futur = std::async(function);
                    }
            }

            if(futur.wait_for(std::chrono::milliseconds(0))==std::future_status::ready)
            {
                    //here goes the code I want to do when I am sure function() is ready so 
                    ...
                    ...
                    ...
            }
      }
      return 0;
}

If I add a breakpoint at :


//here goes the code I want to do when I am sure function() is ready so  

the breakpoint will be reached only when function() takes little time to finish

do I need to use futur.get() if I want to run std::async(function) again?

This topic is closed to new replies.

Advertisement