mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
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:
commit
a7953301bc
1 changed files with 49 additions and 29 deletions
|
@ -429,35 +429,6 @@ private template isSpawnable(F, T...)
|
||||||
* to $(D fn) must either be $(D shared) or $(D immutable) or have no
|
* to $(D fn) must either be $(D shared) or $(D immutable) or have no
|
||||||
* pointer indirection. This is necessary for enforcing isolation among
|
* pointer indirection. This is necessary for enforcing isolation among
|
||||||
* threads.
|
* 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))
|
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);
|
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
|
* Starts fn(args) in a logical thread and will receive a LinkTerminated
|
||||||
* message when the operation terminates.
|
* message when the operation terminates.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue