On Monday, 13 May 2013 at 19:41:48 UTC, Heinz wrote:
Hi,
I'm looking for an object in "core.sync" whose internal counter
can be 0 or 1 (signaled/not signaled), just like Event Objects
in Win32
(http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396%28v=vs.85%29.aspx).
The object must be waitable and able to notify waiters. A
semaphore is the most similar thing but its internal counter
can range from 0 to x. I can perfectly create and use an event
under Win32 for my needs but i do not know their counterparts
in FreeVSD, Linux and OSX; that's why i'm trying to use an
already implemented object from the runtime.
This is what i'm trying to do:
void MyFunc()
{
}
Damn, i hit enter and posted before completion, fail! Here's the
code:
/////////////////////////////////////////////////
bool do_loop;
// This function runs in its own thread.
void MyFunc()
{
while(true)
{
if(!do_loop)
myobject.wait(); // Wait for sync object.
// ... Do other stuff. "do_loop" can be set to false again here.
}
}
// This one is called by different threads.
void loop(bool val)
{
do_loop = val;
if(do_loop) // Release sync object if it is waiting.
myobject.notify();
}
/////////////////////////////////////////////////
The idea is that i have a code that loops and do a lot of stuff
but sometimes i don't want it to loop so i set do_loop to false.
When i want it to loop i set do_loop to true and release the
waiting object.
The problem of using a semaphore is that if i call loop() with
true multiple times, my code will perform extra loops until the
internal counter gets to 0, that's why i need a 0-1 sync object.
Any suggestions?
Thank you in advance.