std.concurrency and immutable
Antoche
a at a.a
Sun Nov 24 22:03:27 PST 2013
The following code compiles but doesn't work as expected:
import std.stdio;
import std.concurrency;
class A
{
this() immutable {}
}
void main()
{
auto tid = spawn( &fooBar, thisTid );
while(true)
{
receive(
(Variant any) {
writeln( "Received a variant" );
writeln( "Received ", any );
}
);
}
}
void fooBar( Tid masterTid )
{
scope(failure) writeln( "fooBar failed" );
scope(success) writeln( "fooBar succeeded" );
scope(exit) writeln( "fooBar exiting" );
try
{
immutable A b = new immutable A();
masterTid.send( 42 ); // This works
masterTid.send( b ); // This doesn't
}
catch( Exception e )
{
writeln( "Exception received" );
}
}
I see this in the console:
fooBar exiting
fooBar failed
Received a variant
Received 42
(then it just hangs)
I'm especially puzzled by:
* Sending an int as a message works but not an immutable object.
Wasn't this (safely sharing objects across threads) one of the
basic use cases for the immutable type qualifier?
* scope(failure) failed but my exception handler didn't catch
anything. How is this possible? What could cause that?
Assertions/abort?
There seemed to be a 3-year-old ticket on this issue
(http://d.puremagic.com/issues/show_bug.cgi?id=5538) with very
little activity, which is a bit surprising given how much
emphasis is given to this feature (the D homepage mentions "D
offers an innovative approach to concurrency, featuring true
immutable data, message passing, no sharing by default, and
controlled mutable sharing across threads", and TDPL devotes a
whole chapter on it). This thread
(http://forum.dlang.org/thread/kgk8hc$12fa$1@digitalmars.com)
also says std.concurrency is "very buggy".
If I can't use std.concurrency, is there any other safe
alternative for multithreaded programming with D?
Thanks,
A.
More information about the Digitalmars-d-learn
mailing list