Original Post
Are there any advantages to using an unnamed win32 event (CreateEvent) over using a volatile bool?
Quote:
Use Spin Locks
Avoid actual synchronization primitives in favor of a variety of spin locks -- repeatedly sleep then test a (non-volatile) global variable until it meets your criterion. Spin locks are much easier to use and more "general" and "flexible " than the system objects.
Quote:
Original post by quant
Is writing to a bool not an atomic operation?
Quote:
Original post by Enigma
Writing to a bool will be atomic. It's test-and-set which usually isn't.
Σnigma
Quote:
Original post by taby
It is emperically safer to use an atomic API event. Like mentioned above, the CPU could very well be stolen by the OS halfway through a bool assignment. Not a super big problem since it only really relies on one bit.. until you think that there's 8 of them, and the last bit may be the only one that doesn't get filled before the CPU moves on to another thread.
Quote:
Original post by chollida1
It was my understanding that a bool will be writen in one atomic operation.
//shared memoryvolatile bool should_exit = false;volatile bool should_do_something = false;volatile bool done_something = false;//thread 1while(!should_exit){ if(should_do_something) { should_do_something = false; do_something(); done_something = true; } else { Sleep(0); }}//thread 2should_do_something = true;while(!done_something){ Sleep(0);}done_something = false;//shared eventsHANDLE should_exit;HANDLE should_do_something;HANDLE done_something;//thread 1while(!WaitForSingleObject(should_exit, 0)){ WaitForSingleObject(should_do_something, INFINITE); do_something(); SetEvent(done_something);}//thread 2SetEvent(should_do_something);WaitForSingleObject(done_something, INFINITE);//shared memoryvolatile bool should_exit = false;volatile bool should_do_something = false;volatile bool done_something = false;void WaitForVolatileBool(volatile bool& mybool){ while(!mybool) { Sleep(0); } mybool = false;}//thread 1while(!should_exit){ WaitForVolatileBool(should_do_something); do_something(); done_something = true;}//thread 2should_do_something = true;WaitForVolatileBool(done_something);This topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more