• Advertisement
Sign in to follow this  

std::thread question

This topic is 1465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by lomateron

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Vortez

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

ooooopps I just found the code I have had an error, the code I have is very different from the code In my previous post, 

the error was that I checked if the async function() finished just one time after like 0.5 seconds, so if function took more than 0.5 seconds the code didn't make what I want

so I think the code of my previous post is correct, I will want someone to tell me if it is, I am just a starter

Edited by lomateron

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement