Thread overview | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 13, 2013 What sync object should i use? | ||||
---|---|---|---|---|
| ||||
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() { } |
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | On Mon, 13 May 2013 15:41:47 -0400, Heinz <thor587@gmail.com> 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()
> {
>
> }
core.sync.condition and core.sync.mutex
I'd point at the docs, but they are poor.
Search online for docs on how mutexes and conditions work. It's very similar to Windows events.
-Steve
|
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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. |
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 13 May 2013 at 19:49:51 UTC, Steven Schveighoffer wrote:
> On Mon, 13 May 2013 15:41:47 -0400, Heinz <thor587@gmail.com> 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()
>> {
>>
>> }
>
> core.sync.condition and core.sync.mutex
>
> I'd point at the docs, but they are poor.
>
> Search online for docs on how mutexes and conditions work. It's very similar to Windows events.
>
> -Steve
Thanks Steve for your quick reply. You answered me before i fixed my post.
I'll take a look at what you say and post again here in case i have other questions.
|
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | Ok, here's a summary in case someone else is in the same need: 1) Want to know what "mutex/condition" are, how do they work and when to use them? Here's a good resource: http://stackoverflow.com/a/4742236 2) If you come to the question "Why a condition needs a mutex?": http://stackoverflow.com/questions/2763714/why-do-pthreads-condition-variable-functions-require-a-mutex 3) Need a working example in D? (by Steven Schveighoffer): http://forum.dlang.org/thread/j7sdte$25qm$1@digitalmars.com |
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | On Monday, 13 May 2013 at 20:44:37 UTC, Heinz wrote: > Ok, here's a summary in case someone else is in the same need: > > 1) Want to know what "mutex/condition" are, how do they work and when to use them? Here's a good resource: http://stackoverflow.com/a/4742236 > > 2) If you come to the question "Why a condition needs a mutex?": http://stackoverflow.com/questions/2763714/why-do-pthreads-condition-variable-functions-require-a-mutex > > 3) Need a working example in D? (by Steven Schveighoffer): http://forum.dlang.org/thread/j7sdte$25qm$1@digitalmars.com Here is the absolute minimum of code I could think to try Condition. I tested it on linux 64bits and windows 32bits. There is one thing that should definitely added to the documentation, and that is what happens when one issues a notify while the thread hasn't yet called Condition.wait(). I seem to recall (though I might be wrong) that win32 events can be signalled before the thread calls WaitForSingleObject (or WaitForMultipleObjects), and if it was signalled, the call returns immediately. I think this was very useful. import std.stdio, core.thread, core.sync.condition, core.sync.mutex; //Note: The condition must be __gshared: __gshared Condition condition; __gshared Mutex mutex; private void myThread() { writeln("Started"); synchronized(mutex) { condition.wait(); } writeln("Notified"); } void main() { mutex = new Mutex(); condition = new Condition(mutex); Thread theThread = new Thread(&myThread); theThread.start(); //Note: if one doesn't wait for the thread to //get to condition.wait(), before calling notify(), //the thread is not awaken. (ie: commenting the //sleep makes the thread stuck). Thread.sleep(dur!"msecs"(100)); synchronized(mutex) { condition.notify(); } theThread.join(); writeln("main finished"); } |
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | On Mon, 13 May 2013 16:44:36 -0400, Heinz <thor587@gmail.com> wrote:
> 3) Need a working example in D? (by Steven Schveighoffer): http://forum.dlang.org/thread/j7sdte$25qm$1@digitalmars.com
Well, whaddya know. Glad my former self could help :)
BTW, given recent discussion on memory barriers, I think my previous statement that the mutex does not need to be locked to call notify is probably incorrect.
-Steve
|
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juan Manuel Cabo | On Mon, 13 May 2013 17:04:22 -0400, Juan Manuel Cabo <juanmanuel.cabo@gmail.com> wrote:
> I seem to recall (though I might be wrong) that win32 events can be signalled before the thread calls WaitForSingleObject (or WaitForMultipleObjects), and if it was signalled, the call returns immediately. I think this was very useful.
Right, the equivalent to a windows event is a D Mutex, Condition, and a protected boolean. In other words, the condition aids synchronization of a message, but it is not the message itself! Windows events could serve as the message (but not necessarily).
-Steve
|
May 13, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 13 May 2013 at 21:55:45 UTC, Steven Schveighoffer wrote: > On Mon, 13 May 2013 17:04:22 -0400, Juan Manuel Cabo <juanmanuel.cabo@gmail.com> wrote: > >> I seem to recall (though I might be wrong) that win32 events can be signalled before the thread calls WaitForSingleObject (or WaitForMultipleObjects), and if it was signalled, the call returns immediately. I think this was very useful. > > Right, the equivalent to a windows event is a D Mutex, Condition, and a protected boolean. In other words, the condition aids synchronization of a message, but it is not the message itself! Windows events could serve as the message (but not necessarily). > > -Steve Thanks!!! Your explanation finally made it clear for me. It would be nice if this last paragraph of yours was in the documentation. It would save some headakes for people like me (I first viewed Condition as a drop in replacement of win32 events, didn't know of the pthreads way). And also, after reading the other link posted above: http://stackoverflow.com/questions/2763714/why-do-pthreads-condition-variable-functions-require-a-mutex I think I finally got it. Thanks again! --jm |
May 14, 2013 Re: What sync object should i use? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | > BTW, given recent discussion on memory barriers, I think my previous statement that the mutex does not need to be locked to call notify is probably incorrect.
Haven't had any issues calling notify outside a synchronized statement, even from multiple threads. At least this works under Win32 with my producer thread all wrapped inside synchronized(). Only a single consumer thread is inside synchronized() but then i have 2 more threads making naked calls to notify(). Not a single crash.
|
Copyright © 1999-2021 by the D Language Foundation