Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #973 from MartinNowak/threadStartMethodChaining
Browse files Browse the repository at this point in the history
return Thread object in start method
  • Loading branch information
AndrejMitrovic committed Oct 5, 2014
2 parents 0b43dc3 + 4495546 commit eb18ee3
Showing 1 changed file with 36 additions and 48 deletions.
84 changes: 36 additions & 48 deletions src/core/thread.d
Expand Up @@ -484,38 +484,6 @@ else
* class, and instances of this class should never be explicitly deleted.
* A new thread may be created using either derivation or composition, as
* in the following example.
*
* Example:
* ----------------------------------------------------------------------------
*
* class DerivedThread : Thread
* {
* this()
* {
* super( &run );
* }
*
* private :
* void run()
* {
* printf( "Derived thread running.\n" );
* }
* }
*
* void threadFunc()
* {
* printf( "Composed thread running.\n" );
* }
*
* // create instances of each type
* Thread derived = new DerivedThread();
* Thread composed = new Thread( &threadFunc );
*
* // start both threads
* derived.start();
* composed.start();
*
* ----------------------------------------------------------------------------
*/
class Thread
{
Expand Down Expand Up @@ -621,7 +589,7 @@ class Thread
* Throws:
* ThreadException if the thread fails to start.
*/
final void start()
final Thread start()
in
{
assert( !next && !prev );
Expand Down Expand Up @@ -726,10 +694,10 @@ class Thread
//
// VERIFY: does this actually also apply to other platforms?
add( this );
return this;
}
}


/**
* Waits for this thread to complete. If the thread terminated as the
* result of an unhandled exception, this exception will be rethrown.
Expand Down Expand Up @@ -1805,16 +1773,41 @@ private:
}
}

///
unittest
{
class DerivedThread : Thread
{
this()
{
super(&run);
}

private:
void run()
{
// Derived thread running.
}
}

void threadFunc()
{
// Composed thread running.
}

// create and start instances of each type
auto derived = new DerivedThread().start();
auto composed = new Thread(&threadFunc).start();
}

unittest
{
int x = 0;

auto t = new Thread(
new Thread(
{
x++;
});
t.start(); t.join();
}).start().join();
assert( x == 1 );
}

Expand All @@ -1826,11 +1819,10 @@ unittest

try
{
auto t = new Thread(
new Thread(
{
throw new Exception( MSG );
});
t.start(); t.join();
}).start().join();
assert( false, "Expected rethrown exception." );
}
catch( Throwable t )
Expand Down Expand Up @@ -2153,8 +2145,7 @@ unittest
auto t = new Thread(
{
Thread.sleep(100.msecs);
});
t.start();
}).start();
thread_detachInstance(t);
foreach (t2; Thread)
assert(t !is t2);
Expand Down Expand Up @@ -3174,9 +3165,8 @@ class ThreadGroup
*/
final Thread create( void function() fn )
{
Thread t = new Thread( fn );
Thread t = new Thread( fn ).start();

t.start();
synchronized( this )
{
m_all[t] = t;
Expand All @@ -3197,9 +3187,8 @@ class ThreadGroup
*/
final Thread create( void delegate() dg )
{
Thread t = new Thread( dg );
Thread t = new Thread( dg ).start();

t.start();
synchronized( this )
{
m_all[t] = t;
Expand Down Expand Up @@ -4996,8 +4985,7 @@ unittest
static void unreferencedThreadObject()
{
static void sleep() { Thread.sleep(dur!"msecs"(100)); }
auto thread = new Thread(&sleep);
thread.start();
auto thread = new Thread(&sleep).start();
}
unreferencedThreadObject();
GC.collect();
Expand Down

0 comments on commit eb18ee3

Please sign in to comment.