Merge pull request #5872 from wilzbach/std-concurrency-tests-1

Issue 17127 - bad example code for std.concurrency.Generator
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
This commit is contained in:
The Dlang Bot 2017-11-21 18:38:05 +01:00 committed by GitHub
commit a7953301bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -429,35 +429,6 @@ private template isSpawnable(F, T...)
* to $(D fn) must either be $(D shared) or $(D immutable) or have no
* pointer indirection. This is necessary for enforcing isolation among
* threads.
*
* Example:
* ---
* import std.stdio, std.concurrency;
*
* void f1(string str)
* {
* writeln(str);
* }
*
* void f2(char[] str)
* {
* writeln(str);
* }
*
* void main()
* {
* auto str = "Hello, world";
*
* // Works: string is immutable.
* auto tid1 = spawn(&f1, str);
*
* // Fails: char[] has mutable aliasing.
* auto tid2 = spawn(&f2, str.dup);
*
* // New thread with anonymous function
* spawn({ writeln("This is so great!"); });
* }
* ---
*/
Tid spawn(F, T...)(F fn, T args) if (isSpawnable!(F, T))
{
@ -465,6 +436,55 @@ Tid spawn(F, T...)(F fn, T args) if (isSpawnable!(F, T))
return _spawn(false, fn, args);
}
///
@system unittest
{
static void f(string msg)
{
assert(msg == "Hello World");
}
auto tid = spawn(&f, "Hello World");
}
/// Fails: char[] has mutable aliasing.
@system unittest
{
string msg = "Hello, World!";
static void f1(string msg) {}
static assert(!__traits(compiles, spawn(&f1, msg.dup)));
static assert( __traits(compiles, spawn(&f1, msg.idup)));
static void f2(char[] msg) {}
static assert(!__traits(compiles, spawn(&f2, msg.dup)));
static assert(!__traits(compiles, spawn(&f2, msg.idup)));
}
/// New thread with anonymous function
@system unittest
{
spawn({
ownerTid.send("This is so great!");
});
assert(receiveOnly!string == "This is so great!");
}
@system unittest
{
import core.thread : thread_joinAll;
__gshared string receivedMessage;
static void f1(string msg)
{
receivedMessage = msg;
}
auto tid1 = spawn(&f1, "Hello World");
thread_joinAll;
assert(receivedMessage == "Hello World");
}
/**
* Starts fn(args) in a logical thread and will receive a LinkTerminated
* message when the operation terminates.