• Advertisement

C++ std::thread finish implementation

Recommended Posts

Guess i have a thread and im on main thread of app now i would like to wait until thread finishes to execute.
  
  Thread Func
  
  void ProcessServerFrame()
{
	
	server_thread_free = false;
  
	while (!server_thread_finished)
      Do stuff
  
  	server_thread_free = true; 
  }




And then from main thread i would like to check whenever thread finished to work
  
  void CreateDWMTCPServer()
{
	if (server != 0)
	{
		server_thread_finished = true;
		while (!server_thread_free) {}
		delete server;
		server = 0;
	}
  
  
  }

Now i am not sure if thread is allowed to read data when other is writing to it or maybe theres some kind of queue that prevents that from happening, basically i dont want to check for server_thread_free when thread marks it as true

 

any thoughts?

Share this post


Link to post
Share on other sites
Advertisement

There are a number of threading primitives that may be necessary here, but I'm not 100% sure what you are trying to accomplish.

To safely read a variable that is written to from another thread, you might consider using std::atomic (in this case, likely std::atomic_bool). Atomics ensure that writes will occur fully before reads see the changes, avoiding reading a partially-modified value.

If you are actually waiting for the worker thread to be entirely finished (i.e. reach the end of its main function), then you could call join() on it from the main thread. Join will block the main thread until the worker thread exits.

If instead this is a worker thread that works on many small tasks, and you want to wait for it to be finished with the current task, then you likely want std::condition_variable. Conditional variables let you block one thread until the other signals it, which would allow your thread to sleep until the other thread wakes it up.

Share this post


Link to post
Share on other sites

In the worker example; what I use to utilize is a double sleeved Semaphore that first locks and then awaits itself if std::condition_variable is not an option. Then another thread may come and release the Semaphore so you have the same effect at the end. I first used to go with Mutex but it showed that on Windows platform a mutex is protected against self locking

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Hawkblood
      I've been away for a VERY long time, so if this topic has already been discussed, I couldn't find it.
      I started using VS2017 recently and I keep getting warnings like this:
      1>c:\program files (x86)\microsoft directx sdk (june 2010)\include\d3d10.h(609): warning C4005: 'D3D10_ERROR_FILE_NOT_FOUND': macro redefinition (compiling source file test.cpp) 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\winerror.h(54103): note: see previous definition of 'D3D10_ERROR_FILE_NOT_FOUND' (compiling source file test.cpp) It pops up for various things, but the reasons are all the same. Something is already defined.....
      I have DXSDK June2010 and referencing the .lib and .h set correctly (otherwise I wouldn't get this, I'd get errors)
      Is there a way to correct this issue or do I just have to live with it?
       
      Also (a little off-topic) the compiler doesn't like to compile my code if I make very small changes.... What's up with that? Can I change it? Google is no help.
    • By lonewolff
      Hi guys,
      I am having problems with trying to perform a basic 'shift left' on a char.
      char temp[1]; temp[0] = buffer[0] << 1; // buffer[0] is 0xff After this I have temp[0] writing to a file. Instead of being the expected 0x7F it is written as 0xF8.
      Any guidance on what I am doing wrong would be awesome.
      Thanks in advance
    • By sergio2k18
      Hi all
      this is my first post on this forum.
      First of all i want to say you that i've searched many posts on this forum about this specific argument, without success, so i write another one....
      Im a beginner.
      I want use GPU geometry clipmaps algorithm to visualize virtual inifinte terrains. 
      I already tried to use vertex texture fetch with a single sampler2D with success.
       
      Readed many papers about the argument and all speak about the fact that EVERY level of a geometry clipmap, has its own texture. What means this exactly? i have to 
      upload on graphic card a sampler2DArray?
      With a single sampler2D is conceptually simple. Creating a vbo and ibo on cpu (the vbo contains only the positions on X-Z plane, not the heights)
      and upload on GPU the texture containing the elevations. In vertex shader i sample, for every vertex, the relative height to te uv coordinate.
      But i can't imagine how can i reproduce various 2d footprint for every level of the clipmap. The only way i can imagine is follow:
      Upload the finer texture on GPU (entire heightmap). Create on CPU, and for each level of clipmap, the 2D footprints of entire clipmap.
      So in CPU i create all clipmap levels in terms of X-Z plane. In vertex shader sampling these values is simple using vertex texture fetch.
      So, how can i to sample a sampler2DArray in vertex shader, instead of upload a sampler2D of entire clipmap?
       
       
      Sorry for my VERY bad english, i hope i have been clear.
       
    • By mangine
      Hello. I am developing a civ 6 clone set in space and I have a few issues. I am using Lua for the logic and UI of the game and c++ directx 12 for the graphics. I need a way to send information between Lua and c++ occasionally and was wondering what is the best and most flexible (and hopefully fast) way to do this. Don't forget that I also need to send things from c++ back to Lua. I know of a lua extension called "LuaBridge" on github but it is a little old and I am worried that it will not work with directx 12. Has anybody done something similar and knows a good method of sending data back and forth? I am aware that Lua is used more and more in the industry and surely plenty of AAA game programmers know the answer to this. I want a good solution that will hopefully still be viable code in a couple of years...
    • By owenjr
      Hi there.
      I'm pretty new to this and I don't know if it has been asked before, but here I go.
      I'm developing a game using SFML and C++.
      I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess. 
      Anyone knows where can I find good information to make my map loader by myself?
      Thanks in advantage!!
  • Advertisement