D - Threads and synchronisation
- suxx myrealbox.com (12/12) Sep 17 2003 Just started having a look at D...
- news.digitalmars.com. (8/18) Sep 17 2003 be
- suxx myrealbox.com (8/8) Sep 17 2003 As reading my post should make clear, you will note that I have seen and...
- Julio César Carrascal Urquijo (16/28) Sep 17 2003 I haven't tried but from the documentation in
- suxx myrealbox.com (5/5) Sep 17 2003 top
- Walter (5/8) Dec 04 2003 Yes. I am not an expert in threading applications. I've made a stab at i...
- Sean L. Palmer (19/27) Dec 04 2003 We are about to embark on a long threading/multiprocessor nightmare with
- Alix Pexton (15/20) Dec 04 2003 I think programmers should be able to choose between creating a running
- Hauke Duden (15/20) Dec 04 2003 One example, hold the cheese, coming right up!
- Sean L. Palmer (8/27) Dec 04 2003 Ok, maybe a good one, but you could pass in a delegate and priority,
- Hauke Duden (8/17) Dec 04 2003 But what if it gets more complicated? What if you want the thread to be
- Sean L. Palmer (15/31) Dec 04 2003 The library should guarantee you a certain minimal level of functionalit...
- Sean L. Palmer (9/23) Dec 04 2003 You don't have accurate timing with threads anyway, or at least you
- Sean Kelly (5/9) Feb 05 2004 Are the library features implemented in C/C++? I'm still getting up to
- Walter (4/12) Feb 06 2004 it
- Sean Kelly (4/14) Feb 07 2004 So say I wanted to work on new synchronization methods, this would be
- Sean Kelly (6/8) Feb 07 2004 Er, clarification. I meant to ask whether the thread implementation
- Sean Kelly (5/9) Feb 07 2004 Answered my own question. Next time I'll just look at the Phobos source...
- Graham StJack (17/27) Sep 18 2003 What you are describing here is along the lines of the "entry" feature
Just started having a look at D... I can't seem to find anything about synchronizing the actions of threads - something along the lines of a pthread 'condition' variable which can be waited on or signalled. Are we supposed to make use of the underlying thread library of our choice? ie. actually use a 'pthread_cond_t', initialising and using it manually? Are there plans for extending the 'synchronized' interface so that it can be specified to be a read only syncronization allowing multiple threads to access read-only data simultaneously? And just in case the above has been debated to death already, whats the recomended method for performing a search on the contents of this forum? Suxx
Sep 17 2003
I can't seem to find anything about synchronizing the actions of threads - something along the lines of a pthread 'condition' variable which can bewaitedon or signalled. Are we supposed to make use of the underlying threadlibrary ofour choice? ie. actually use a 'pthread_cond_t', initialising and using it manually? Are there plans for extending the 'synchronized' interface so that it canbespecified to be a read only syncronization allowing multiple threads toaccessread-only data simultaneously? And just in case the above has been debated to death already, whats the recomended method for performing a search on the contents of this forum?Well, the best thing is to read the reference manual of D. I can't blame you for not noticing the synchronize{ } statement there, cause I haven't noticed 0b literals either while I was reading it :-) Just read the manual again carefuly :-)
Sep 17 2003
As reading my post should make clear, you will note that I have seen and read the manual and read the section on 'synchronize'. The question I asked was not 'how do I prevent multiple threads running the same code at the same time?' which is what the 'syncronized' statement is for. My question was 'how do I syncronize the action of threads?' - if not in those exact words. By this I mean something along the lines of being able to tell ThreadA to suspend execution until Thread[B,C,D,...] completes some task and signals completion?
Sep 17 2003
I haven't tried but from the documentation in http://www.digitalmars.com/d/phobos.html you can do: myThread.wait(); void wait(); Wait for this thread to terminate. Throws ThreadError if the thread hasn't begun yet or has already terminated or is called on itself. void wait(unsigned milliseconds); Wait for this thread to terminate or until milliseconds time has elapsed, whichever occurs first. Throws ThreadError if the thread hasn't begun yet or has already terminated or is called on itself. <suxx myrealbox.com> wrote in message news:bk9ori$25q3$1 digitaldaemon.com...Just started having a look at D... I can't seem to find anything about synchronizing the actions of threads - something along the lines of a pthread 'condition' variable which can bewaitedon or signalled. Are we supposed to make use of the underlying threadlibrary ofour choice? ie. actually use a 'pthread_cond_t', initialising and using it manually? Are there plans for extending the 'synchronized' interface so that it canbespecified to be a read only syncronization allowing multiple threads toaccessread-only data simultaneously? And just in case the above has been debated to death already, whats the recomended method for performing a search on the contents of this forum? Suxx
Sep 17 2003
top Given the answers so far, can I assume that someone (walter maybe) is looking into a few more threading primitives? Or is it an open field with suggestions/requirements/patches welcome from all comers? Suxx
Sep 17 2003
<suxx myrealbox.com> wrote in message news:bkaiuk$adp$1 digitaldaemon.com...Given the answers so far, can I assume that someone (walter maybe) islookinginto a few more threading primitives? Or is it an open field with suggestions/requirements/patches welcome from all comers?Yes. I am not an expert in threading applications. I've made a stab at it with synchronized and with the functions in std.thread. If you'd like to contribute improvements, by all means!
Dec 04 2003
We are about to embark on a long threading/multiprocessor nightmare with Playstation3 and Xbox 2. I hope we can figure out a way to make it easy. I was thinking more that you'd make a subclass of thread and override the run method to call your function. But thread.d seems pretty sophisticated to me. Has all the features I would want. But I'm no expert either. You could also store the thread object pointer somewhere in thread local storage instead of having to look it up in a table. But then you couldn't easily support pauseall etc. I'm wondering how come class thread doesn't have a destructor that calls wait()? Is it likely that you would want to allow a thread to continue running after its thread object has been destroyed? The wait() functions do not set the thread state to TS.TERMINATED Also I can't see why you would possibly want to make a thread object without actually starting it. So I would have the ctors call start automatically and make start a private function. Sean "Walter" <walter digitalmars.com> wrote in message news:bqmtfe$2p49$1 digitaldaemon.com...<suxx myrealbox.com> wrote in messagenews:bkaiuk$adp$1 digitaldaemon.com...Given the answers so far, can I assume that someone (walter maybe) islookinginto a few more threading primitives? Or is it an open field with suggestions/requirements/patches welcome from all comers?Yes. I am not an expert in threading applications. I've made a stab at it with synchronized and with the functions in std.thread. If you'd like to contribute improvements, by all means!
Dec 04 2003
Sean L. Palmer wrote:Also I can't see why you would possibly want to make a thread object without actually starting it. So I would have the ctors call start automatically and make start a private function. SeanI think programmers should be able to choose between creating a running thread and a dormant one. Though I am sure you are right that creating a thread and never starting it is foolhardy. I can't think of an example, but I'm sure someone some where might have a reason to delay the starting of a thread. Perhaps a thread that is started automatically by the constructor would have a non-deterministic start time, thus making benchmarks in accurate, though perhaps that could be overcome by the use of a subclassed thread type that took care of its own timing... Alix... -- Alix Pexton Webmaster - http://www.theDjournal.com Alix theDjournal.com
Dec 04 2003
Alix Pexton wrote:I think programmers should be able to choose between creating a running thread and a dormant one. Though I am sure you are right that creating a thread and never starting it is foolhardy. I can't think of an example, but I'm sure someone some where might have a reason to delay the starting of a thread.One example, hold the cheese, coming right up! A non-started thread object could be used for passing long-running algorithms to another object. For example, you could pass a bunch of dormant image-filtering threads to some kind of "run all these filters on that image" function. You could also pass delegates and have the function create the threads, but sometimes it can be easier to use ready-made thread objects. For example, if the filters are to be run concurrently with different priorities then you can pre-configure the thread objects before passing them to that function. Oh, speaking of pre-configuring: another example. Maybe you have to set some parameters before the thread is started. For example, for an "idle" priority thread, you'd probably want to change the priority first, before the thread is started. Hauke
Dec 04 2003
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message news:bqnppo$109o$1 digitaldaemon.com...Alix Pexton wrote:Ok, maybe a good one, but you could pass in a delegate and priority, instead, as you say.I think programmers should be able to choose between creating a running thread and a dormant one. Though I am sure you are right that creating a thread and never starting it is foolhardy. I can't think of an example, but I'm sure someone some where might have a reason to delay the starting of a thread.One example, hold the cheese, coming right up! A non-started thread object could be used for passing long-running algorithms to another object. For example, you could pass a bunch of dormant image-filtering threads to some kind of "run all these filters on that image" function. You could also pass delegates and have the function create the threads, but sometimes it can be easier to use ready-made thread objects. For example, if the filters are to be run concurrently with different priorities then you can pre-configure the thread objects before passing them to that function.Oh, speaking of pre-configuring: another example. Maybe you have to set some parameters before the thread is started. For example, for an "idle" priority thread, you'd probably want to change the priority first, before the thread is started.So make a constructor that takes the priority as a parameter. (default parameter would be better, but isn't D) I just don't want to baggage the common case by the exceptional case. Sean
Dec 04 2003
Sean L. Palmer wrote:But what if it gets more complicated? What if you want the thread to be part of a thread group? What if you have to manually allocate TLS space for the thread because your OS doesn't provide any?Oh, speaking of pre-configuring: another example. Maybe you have to set some parameters before the thread is started. For example, for an "idle" priority thread, you'd probably want to change the priority first, before the thread is started.So make a constructor that takes the priority as a parameter. (default parameter would be better, but isn't D)I just don't want to baggage the common case by the exceptional case.That's easy to solve. Just add a boolean (errr, I mean "bit") argument startNow=true to the constructor. That allows you to use it both ways, but the default case is to simply start it right away... Hauke
Dec 04 2003
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message news:bqo1de$1bpp$1 digitaldaemon.com...Sean L. Palmer wrote:The library should guarantee you a certain minimal level of functionality. This should fall under the category of "implementation detail".But what if it gets more complicated? What if you want the thread to be part of a thread group? What if you have to manually allocate TLS space for the thread because your OS doesn't provide any?Oh, speaking of pre-configuring: another example. Maybe you have to set some parameters before the thread is started. For example, for an "idle" priority thread, you'd probably want to change the priority first, before the thread is started.So make a constructor that takes the priority as a parameter. (default parameter would be better, but isn't D)Sure. ;) Now if dtor can be made to wait for the thread to shut down, we can do this: void dowork() { auto thread worker1,worker2; worker1 = new thread(doworkA); worker2 = new thread(doworkB); } And before dowork is finished, you can be sure that all the work is complete. SeanI just don't want to baggage the common case by the exceptional case.That's easy to solve. Just add a boolean (errr, I mean "bit") argument startNow=true to the constructor. That allows you to use it both ways, but the default case is to simply start it right away...
Dec 04 2003
You don't have accurate timing with threads anyway, or at least you shouldn't rely on it. You could always just not make the thread object til you need it to start. I don't like how the current implementation adds extra wordiness, forces you to jump through a few extra hoops just to get the "normal" usage behavior. Sean "Alix Pexton" <Alix thedjournal.com> wrote in message news:bqn3kd$dg$1 digitaldaemon.com...Sean L. Palmer wrote:withoutAlso I can't see why you would possibly want to make a thread objectactually starting it. So I would have the ctors call start automatically and make start a private function.I think programmers should be able to choose between creating a running thread and a dormant one. Though I am sure you are right that creating a thread and never starting it is foolhardy. I can't think of an example, but I'm sure someone some where might have a reason to delay the starting of a thread. Perhaps a thread that is started automatically by the constructor would have a non-deterministic start time, thus making benchmarks in accurate, though perhaps that could be overcome by the use of a subclassed thread type that took care of its own timing... Alix...
Dec 04 2003
Walter wrote:Yes. I am not an expert in threading applications. I've made a stab at it with synchronized and with the functions in std.thread. If you'd like to contribute improvements, by all means!Are the library features implemented in C/C++? I'm still getting up to speed on D but Boost has a decent condvar implementation that might be workable if this is true. Sean
Feb 05 2004
"Sean Kelly" <sean ffwd.cx> wrote in message news:bvuoh2$1hbo$1 digitaldaemon.com...Walter wrote:itYes. I am not an expert in threading applications. I've made a stab atNo, they're in D!with synchronized and with the functions in std.thread. If you'd like to contribute improvements, by all means!Are the library features implemented in C/C++? I'm still getting up to speed on D but Boost has a decent condvar implementation that might be workable if this is true.
Feb 06 2004
Walter wrote:"Sean Kelly" <sean ffwd.cx> wrote in message news:bvuoh2$1hbo$1 digitaldaemon.com...So say I wanted to work on new synchronization methods, this would be changes to the compiler and not Phobos? SeanNo, they're in D!with synchronized and with the functions in std.thread. If you'd like to contribute improvements, by all means!Are the library features implemented in C/C++? I'm still getting up to speed on D but Boost has a decent condvar implementation that might be workable if this is true.
Feb 07 2004
Sean Kelly wrote:So say I wanted to work on new synchronization methods, this would be changes to the compiler and not Phobos?Er, clarification. I meant to ask whether the thread implementation compiles directly or if it ties back to platform library calls on the operating system. Like is there a thread implementation file somewhere calling the Win32 _beginthread function? Sean
Feb 07 2004
Sean Kelly wrote:Er, clarification. I meant to ask whether the thread implementation compiles directly or if it ties back to platform library calls on the operating system. Like is there a thread implementation file somewhere calling the Win32 _beginthread function?Answered my own question. Next time I'll just look at the Phobos source before asking :) So it's in D but ultimately makes Win32 or pthread platofmr calls. Sean
Feb 07 2004
I am a new starter with D too. Like you, I am looking for something that allows useful inter-thread cooperation.I can't seem to find anything about synchronizing the actions of threads - something along the lines of a pthread 'condition' variable which can be waited on or signalled. Are we supposed to make use of the underlying thread library of our choice? ie. actually use a 'pthread_cond_t', initialising and using it manually?What you are describing here is along the lines of the "entry" feature in Ada95 protected objects, which uses a condition vaiable as you describe, with a syntax that would look a bit like this in D: public synchronized void removeFromQueue(...) provided itemCount > 0 { ... } The idea being that a thread trying to execute this method would not only have to be the only thread in the object, but the condition (in this case itemCount > 0) would have to be true too. Some other thread could eventually make the condition true by making a different synchronized call.Are there plans for extending the 'synchronized' interface so that it can be specified to be a read only syncronization allowing multiple threads to access read-only data simultaneously?I would like to see something high-level like my suggestion above, rather than the Java approach of adding wait() and notify() methods in Object. The Java approach is more difficult for the coder to get right, but even that would be a whole lot better than nothing.And just in case the above has been debated to death already, whats the recomended method for performing a search on the contents of this forum?
Sep 18 2003