Writing malformed it?
sdvcn via Digitalmars-d
digitalmars-d at puremagic.com
Sat Nov 15 05:53:06 PST 2014
[code]
import core.sys.windows.windows;
import std.concurrency;
import core.sync.semaphore;
import core.thread;
//struct sPool(T){ //<--- it good
shared struct sPool(T){
T[] _buff;
Semaphore _sp ;
auto pfReadAny()
{
_sp.wait();
auto r = _buff[0];
_buff = _buff[1..$];
return r;
}
void pfPush(T v)
{
_buff ~= v;
_sp.notify();
}
void Init()
{
if(_sp is null)
{
_sp = new Semaphore();
}
}
}
//alias mPool = sPool!int; //<--- it good
alias mPool = shared(sPool!int);
//void Thr(shared(mPool*) t) //<--- it good
void Thr(mPool* t)
{
auto tt = cast(mPool*) t;
auto tid = GetCurrentThreadId();
while(1)
{
auto a = tt.pfReadAny();
writefln("[%d]Rk[%d]",tid,a);
while(a--)
{
Sleep(1);
}
}
}
//void Thw(shared(mPool*) t) //<--- it good
void Thw(mPool* t)
{
auto tt = cast(mPool*) t;
auto tid = thisTid();
while(1)
{
foreach(v;1..100)
{
tt.pfPush(v);
//auto a1 = t.pfLast();
writefln("Wk[%d]",v);
Sleep(50);
}
}
}
void test2()
{
mPool ltsy;
ltsy.Init();
auto tidr = spawn(&Thr,cast(shared(mPool*))<sy);
auto tidr2 = spawn(&Thr, cast(shared(mPool*))<sy);
auto tidw = spawn(&Thw, cast(shared(mPool*))<sy);
auto wasSuccessful = receiveOnly!(bool);
writeln("End.");
}
[code]
auto sem = new shared Semaphore(); // Sentence is wrong? why?
More information about the Digitalmars-d
mailing list