1 Replies - 708 Views - Last Post: 07 October 2014 - 11:59 PM Rate Topic: -----

#1 UltimaN3rd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-July 12

std::thread works in debug, doesn't execute in release unless I co

Posted 07 October 2014 - 05:58 PM

Hi guys! I've been working on a game for a week or so and decided to multi-thread it now. I've never worked with threads before, having only developed very simple things, but I figured with C++ 11 it'd be worth it to get threads up and running. Here are the important bits of code:

main.cpp
//A bunch of global variables and headers to be tidied up later

int main()
{
	std::thread mainGameThread(mainLoop);
	std::thread doBigUpdate(bigUpdate);
	doBigUpdate.join();
	mainGameThread.join();

	return 0;
}

void mainLoop()
{
	//Contains window setup and draws the game every frame
}

//Other functions

void bigUpdate()
{
	srand((unsigned)time(0));
	float tempTime = 0.0f;
	while (continueGame)
	{
		std::cout << '.';            //THIS LINE RIGHT HERE
		tempTime = deltaTime;
		deltaTime = 0.0f;
		branch.update(tempTime);
		while (liveUpdateTime >= 1.0f)
		{
			branch.bigUpdate(liveUpdateTime);
			liveUpdateTime -= 1.0f;
		}
	}
}

//Other things down here, plus multiple other files



Basically my problem is this: everything works fine in debug mode. Whenever I try to run in release, though, the doBigUpdate thread doesn't execute unless I add that cout line labelled with the "THIS LINE RIGHT HERE" comment. Without that line, the thread loops through the while loop, but doesn't execute any of the code. Then eventually when continueGame becomes false, the thread will quit. It's only after I add the cout line that the code will properly execute.
Again, it works fine with the cout in debug, but not in release. Any idea why?

Thanks, UltimaN3rd

Is This A Good Question/Topic? 0
  • +

Replies To: std::thread works in debug, doesn't execute in release unless I co

#2 UltimaN3rd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-July 12

Re: std::thread works in debug, doesn't execute in release unless I co

Posted 07 October 2014 - 11:59 PM

Still unsure on what the cause is, but another line of code that works other than the std::cout call is:

std::this_thread::sleep_for(std::chrono::milliseconds(0));



I'd guess this is a little faster than cout, so I'll use it as my ugly patch for now. Does anyone have some insight into this issue?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1