www.digitalmars.com         C & C++   DMDScript  

D - Threading

reply Paul Stanton <Paul_member pathlink.com> writes:
can anyone provide a SIMPLE example of thread use in D? thanks.
Jan 20 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Paul Stanton wrote:
 can anyone provide a SIMPLE example of thread use in D? thanks.

import thread; int thread1 (void *ptr) { printf ("1: Start\n"); Thread.yield (); printf ("1: Middle\n"); Thread.yield (); printf ("1: End\n"); return 0; } void main () { Thread t = new Thread (&thread1, null); t.start (); printf ("0: Thread started\n"); Thread.yield (); printf ("0: Middle\n"); while (t.isRunning ()) Thread.yield (); } Should print: 0: Thread started 1: Start 0: Middle 1: Middle 1: End
Jan 20 2003
next sibling parent reply Paul Stanton <Paul_member pathlink.com> writes:
thanks for your time.
wont compile: no property 'isRunning' for type 'Thread'.
fixed it like so....

1. added just below import : "enum TS {INITIAL,RUNNING,TERMINATED}" (copied form
thread.d)
2. changed "while(t.isRunning())" to "while (t.getState() == TS.RUNNING)"

not sure why thread.d doesnt expose constants for thread states, kind of means
users have to guess... oh well, any better way to do this?

In article <3E2C82CA.20806 users.sourceforge.net>, Burton Radons says...
Paul Stanton wrote:
 can anyone provide a SIMPLE example of thread use in D? thanks.

import thread; int thread1 (void *ptr) { printf ("1: Start\n"); Thread.yield (); printf ("1: Middle\n"); Thread.yield (); printf ("1: End\n"); return 0; } void main () { Thread t = new Thread (&thread1, null); t.start (); printf ("0: Thread started\n"); Thread.yield (); printf ("0: Middle\n"); while (t.isRunning ()) Thread.yield (); } Should print: 0: Thread started 1: Start 0: Middle 1: Middle 1: End

Jan 20 2003
parent Burton Radons <loth users.sourceforge.net> writes:
Paul Stanton wrote:
 thanks for your time.
 wont compile: no property 'isRunning' for type 'Thread'.
 fixed it like so....

Whoops, accidentally used DLI code, sorry.
 1. added just below import : "enum TS {INITIAL,RUNNING,TERMINATED}" (copied
form
 thread.d)
 2. changed "while(t.isRunning())" to "while (t.getState() == TS.RUNNING)"

That would have to be Thread.TS.RUNNING or t.TS.RUNNING, wouldn't it?
 not sure why thread.d doesnt expose constants for thread states, kind of means
 users have to guess... oh well, any better way to do this?

The properties should absolutely be in the API, so that should be amended soon.
Jan 20 2003
prev sibling parent reply Mark Evans <Mark_member pathlink.com> writes:
You might be interested to know that Oz supports literally thousands of
concurrent threads with no sweat.  D could learn some things from Oz.

Mark
Jan 20 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
more than the underlying OS will allow ?
and does it allow easy acces to thread locals ?

"Mark Evans" <Mark_member pathlink.com> wrote in message
news:b0it8a$2a0r$1 digitaldaemon.com...
 You might be interested to know that Oz supports literally thousands of
 concurrent threads with no sweat.  D could learn some things from Oz.

 Mark

Jan 20 2003