digitalmars.D.learn - Creating a shared reference type
- "Minas Mina" <minas_mina1990 hotmail.co.uk> Jul 13 2012
- Jonathan M Davis <jmdavisProg gmx.com> Jul 13 2012
- "Minas Mina" <minas_mina1990 hotmail.co.uk> Jul 14 2012
- "David Nadlinger" <see klickverbot.at> Jul 14 2012
- "Minas Mina" <minas_mina1990 hotmail.co.uk> Jul 14 2012
I'm want to "play" a bit with thread syncronization...
So this is a (big) part of my code. The other is the imports, the
thread starting and the printing of x.
shared int x;
shared Semaphore sema;
void main(string[] args)
{
auto t1 = new Thread(&f);
auto t2 = new Thread(&g);
sema = new Semaphore(1); // error here
The error is: Error: cannot implicitly convert expression (new
Semaphore(1u)) of type core.sync.semaphore.Semaphore to
shared(Semaphore)
I understand what the error means, I just don't know how to fix
it (to make it explicit). I tried new shared (Semaphore(1)) but
doesn't work.
Jul 13 2012
On Saturday, July 14, 2012 01:10:46 Minas Mina wrote:I'm want to "play" a bit with thread syncronization... So this is a (big) part of my code. The other is the imports, the thread starting and the printing of x. shared int x; shared Semaphore sema; void main(string[] args) { auto t1 = new Thread(&f); auto t2 = new Thread(&g); sema = new Semaphore(1); // error here The error is: Error: cannot implicitly convert expression (new Semaphore(1u)) of type core.sync.semaphore.Semaphore to shared(Semaphore) I understand what the error means, I just don't know how to fix it (to make it explicit). I tried new shared (Semaphore(1)) but doesn't work.
Try sema = new shared(Semaphore)(1); - Jonathan M Davis
Jul 13 2012
Thanks, I've got another problem:
void f()
{
sema.wait();
++x;
sema.notify();
}
sema is the global shared Semaphore (as above)
main.d(29): Error: function core.sync.semaphore.Semaphore.wait ()
is not callable using argument types () shared
main.d(29): Error: expected 1 function arguments, not 0
main.d(33): Error: function core.sync.semaphore.Semaphore.notify
() is not callable using argument types () shared
Why isn't it working as I am expecting it to? Isn't this the way
shared is used (or should be used)?
Jul 14 2012
On Saturday, 14 July 2012 at 09:15:55 UTC, Minas Mina wrote:Isn't this the way shared is used (or should be used)?
Should be used: probably yes. But functions/methods which are able to act on shared data must be marked so, and unfortunately, the druntime primitives are not yet annotated with shared, so you need to manually cast shared() away first (or just use __gshared instead of shared). David
Jul 14 2012
On Saturday, 14 July 2012 at 09:21:27 UTC, David Nadlinger wrote:On Saturday, 14 July 2012 at 09:15:55 UTC, Minas Mina wrote:Isn't this the way shared is used (or should be used)?
Should be used: probably yes. But functions/methods which are able to act on shared data must be marked so, and unfortunately, the druntime primitives are not yet annotated with shared, so you need to manually cast shared() away first (or just use __gshared instead of shared). David
Thank you, __gshared was actually what I was looking for!
Jul 14 2012









Jonathan M Davis <jmdavisProg gmx.com> 