August 07, 2008 multi thread sampl | ||||
---|---|---|---|---|
| ||||
Where Can I find example of multi thread application in D? |
August 07, 2008 Re: multi thread sampl | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zarathustra | Zarathustra wrote:
> Where Can I find example of multi thread application in D?
Heres a simple in Tango
import tango.io.Stdout,
tango.core.Thread,
tango.core.ThreadPool,
tango.core.sync.Mutex;
Mutex myLock; //used to control one thread outputs at a time
//first thread prints 1 -> 30
void count()
{
for(int i = 1; i <= 30; i++)
{
say( i );
//if i is even yield our time slice
if (i & 1) Thread.yield;
}
}
void abc()
{
for(char i = 'A'; i <= 'Z'; i++)
{
say( "" ~ i ); //make sure it gets passed as a char[]
//if i is even yield our time slice
if (i & 1) Thread.yield;
}
}
//sting say
void say( char[] msg )
{
//lock so say(int) cant write at same time
synchronized( myLock ) Stdout(msg).newline;
}
//int say
void say( int msg )
{
//again lock so say(char[]) cant write
synchronized( myLock ) Stdout(msg).newline;
}
void main()
{
//make a pool of 2 threads
auto pool = new ThreadPool!() ( 2 );
//make our lock
myLock = new Mutex;
//add our threads to the pool, assign will start them right away
//out queues them if all threads are busy, assign takes delegates
//so we just wrap our funcs in braces :)
pool.assign( { count; } );
pool.assign( { abc ; } );
//We join all threads! (wait till there finished and clean up
pool.finish;
}
|
Copyright © 1999-2021 by the D Language Foundation