Jump to content

  • Log In with Google      Sign In   
  • Create Account

std::thread question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 13 January 2014 - 11:36 PM

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, 13 January 2014 - 11:50 PM.


Sponsor:

#2 SeanMiddleditch   Members   -  Reputation: 5851

Like
5Likes
Like

Posted 14 January 2014 - 01:45 AM

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.



#3 BitMaster   Crossbones+   -  Reputation: 4088

Like
1Likes
Like

Posted 14 January 2014 - 02:21 AM

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.

#4 Bregma   Crossbones+   -  Reputation: 5133

Like
4Likes
Like

Posted 14 January 2014 - 06:18 AM

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

#5 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 14 January 2014 - 05:37 PM

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



#6 Hodgman   Moderators   -  Reputation: 30385

Like
4Likes
Like

Posted 14 January 2014 - 05:55 PM

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



#7 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 14 January 2014 - 10:14 PM

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, 14 January 2014 - 10:15 PM.


#8 Nübček Pænus   Members   -  Reputation: 153

Like
0Likes
Like

Posted 15 January 2014 - 01:41 PM

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



#9 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 15 January 2014 - 01:45 PM

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

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



#10 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 15 January 2014 - 03:38 PM

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?



#11 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 15 January 2014 - 07:48 PM

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, 15 January 2014 - 07:51 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS