Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
July 13, 2012 Creating a shared reference type | ||||
---|---|---|---|---|
| ||||
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. |
July 13, 2012 Re: Creating a shared reference type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | 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
|
July 14, 2012 Re: Creating a shared reference type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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)? |
July 14, 2012 Re: Creating a shared reference type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | 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
|
July 14, 2012 Re: Creating a shared reference type | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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!
|
Copyright © 1999-2021 by the D Language Foundation