www.digitalmars.com         C & C++   DMDScript  

D - Threads and synchronisation

reply suxx myrealbox.com writes:
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
next sibling parent reply "news.digitalmars.com." <qus go2.pl> writes:
 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?
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
parent suxx myrealbox.com writes:
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
prev sibling next sibling parent "Julio César Carrascal Urquijo" <adnoctum phreaker.net> writes:
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 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
prev sibling next sibling parent reply suxx myrealbox.com writes:
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
parent reply "Walter" <walter digitalmars.com> writes:
<suxx myrealbox.com> wrote in message news:bkaiuk$adp$1 digitaldaemon.com...
 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?
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
next sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
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 message
news:bkaiuk$adp$1 digitaldaemon.com...
 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?
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
parent reply Alix Pexton <Alix thedjournal.com> writes:
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.

Sean

  
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... -- Alix Pexton Webmaster - http://www.theDjournal.com Alix theDjournal.com
Dec 04 2003
next sibling parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
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
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message
news:bqnppo$109o$1 digitaldaemon.com...
 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.
Ok, maybe a good one, but you could pass in a delegate and priority, instead, as you say.
 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
parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
Sean L. Palmer wrote:
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)
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?
 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
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message
news:bqo1de$1bpp$1 digitaldaemon.com...
 Sean L. Palmer wrote:
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)
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?
The library should guarantee you a certain minimal level of functionality. This should fall under the category of "implementation detail".
 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...
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. Sean
Dec 04 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
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:

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.
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
prev sibling parent reply Sean Kelly <sean ffwd.cx> writes:
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
parent reply "Walter" <walter digitalmars.com> writes:
"Sean Kelly" <sean ffwd.cx> wrote in message
news:bvuoh2$1hbo$1 digitaldaemon.com...
 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.
No, they're in D!
Feb 06 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
Walter wrote:
 "Sean Kelly" <sean ffwd.cx> wrote in message
 news:bvuoh2$1hbo$1 digitaldaemon.com...

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.
No, they're in D!
So say I wanted to work on new synchronization methods, this would be changes to the compiler and not Phobos? Sean
Feb 07 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
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
parent Sean Kelly <sean ffwd.cx> writes:
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
prev sibling parent Graham StJack <grahams adpro.com.au> writes:
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