www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why cannot spawn function defined in unittest block {}?

reply mw <mingwu gmail.com> writes:
put the code from:

https://tour.dlang.org/tour/en/multithreading/synchronization-sharing

in file, and put unittest block from safePrint() to main()

as here:

https://github.com/mingwugmail/dlang_tour/blob/master/spawn_in_unittest.d#L33

-----------------
$ dmd -unittest spawn_in_unittest.d
spawn_in_unittest.d(87): Error: template std.concurrency.spawn 
cannot deduce function from argument types !()(void 
delegate(shared(SafeQueue!int) queue, shared(int)* queueCounter) 
 system, shared(SafeQueue!int), shared(int)*), candidates are:
/usr/include/dmd/phobos/std/concurrency.d(460):        spawn(F, 
T...)(F fn, T args)
   with F = void delegate(shared(SafeQueue!int), shared(int)*) 
 system,
        T = (shared(SafeQueue!int), shared(int)*)
   must satisfy the following constraint:
        isSpawnable!(F, T)
spawn_in_unittest.d(88): Error: template std.concurrency.spawn 
cannot deduce function from argument types !()(void delegate(Tid 
owner, shared(SafeQueue!int) queue, shared(int)* queueCounter) 
 system, Tid, shared(SafeQueue!int), shared(int)*), candidates 
are:
/usr/include/dmd/phobos/std/concurrency.d(460):        spawn(F, 
T...)(F fn, T args)
   with F = void delegate(Tid, shared(SafeQueue!int), 
shared(int)*)  system,
        T = (Tid, shared(SafeQueue!int), shared(int)*)
   must satisfy the following constraint:
        isSpawnable!(F, T)
-----------------

spawn in unitest: why this compiler error? confusing message
Jun 20 2020
parent reply mw <mingwu gmail.com> writes:
the function defined in unittest become a delegate? how to 
work-around this?
Jun 20 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 20 June 2020 at 17:43:42 UTC, mw wrote:
 the function defined in unittest become a delegate? how to 
 work-around this?
just add the keyword static to the functions
Jun 20 2020
parent reply mw <mingwu gmail.com> writes:
On Saturday, 20 June 2020 at 18:01:51 UTC, Adam D. Ruppe wrote:
 On Saturday, 20 June 2020 at 17:43:42 UTC, mw wrote:
 the function defined in unittest become a delegate? how to 
 work-around this?
just add the keyword static to the functions
https://github.com/mingwugmail/dlang_tour/blob/master/spawn_in_unittest.d#L50 Tried, still doesn't work: $ dmd -unittest spawn_in_unittest.d spawn_in_unittest.d(57): Error: static function spawn_in_unittest.__unittest_L33_C1.threadProducer cannot access frame of function spawn_in_unittest.__unittest_L33_C1 spawn_in_unittest.d(74): Error: static function spawn_in_unittest.__unittest_L33_C1.threadConsumer cannot access frame of function spawn_in_unittest.__unittest_L33_C1
Jun 20 2020
parent mw <mingwu gmail.com> writes:
On Saturday, 20 June 2020 at 18:08:06 UTC, mw wrote:
 On Saturday, 20 June 2020 at 18:01:51 UTC, Adam D. Ruppe wrote:
 On Saturday, 20 June 2020 at 17:43:42 UTC, mw wrote:
 the function defined in unittest become a delegate? how to 
 work-around this?
just add the keyword static to the functions
https://github.com/mingwugmail/dlang_tour/blob/master/spawn_in_unittest.d#L50 Tried, still doesn't work: $ dmd -unittest spawn_in_unittest.d spawn_in_unittest.d(57): Error: static function spawn_in_unittest.__unittest_L33_C1.threadProducer cannot access frame of function spawn_in_unittest.__unittest_L33_C1 spawn_in_unittest.d(74): Error: static function spawn_in_unittest.__unittest_L33_C1.threadConsumer cannot access frame of function spawn_in_unittest.__unittest_L33_C1
oh, need to add static to all the function the spawned function calls. static void safePrint(T...)(T args)
Jun 20 2020