Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 27, 2015 Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Hi, I am currently working through a book on the fundamentals of computer concurrency and I wanted to do all of the exercises in D. But I ran into a problem when I tried to have a global semaphore: /usr/local/Cellar/dmd/2.067.1/include/d2/core/sync/semaphore.di(35): Error: constructor core.sync.semaphore.Semaphore.this core.sync.semaphore.Semaphore cannot be constructed at compile time, because the constructor has no available source code Here is my code: import core.sync.semaphore; import core.thread; import std.string; import std.stdio; shared string data; shared Semaphore sem = new Semaphore(); void read() { data = "From Thread"; sem.notify(); } void write() { sem.wait(); data.writeln; } void main() { Thread reader = new Thread(&read); Thread writer = new Thread(&write); reader.start(); writer.start(); } |
July 27, 2015 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | On 7/27/15 3:10 PM, Jack Stouffer wrote:
> Hi,
>
> I am currently working through a book on the fundamentals of computer
> concurrency and I wanted to do all of the exercises in D. But I ran into
> a problem when I tried to have a global semaphore:
>
> /usr/local/Cellar/dmd/2.067.1/include/d2/core/sync/semaphore.di(35):
> Error: constructor core.sync.semaphore.Semaphore.this
> core.sync.semaphore.Semaphore cannot be constructed at compile time,
> because the constructor has no available source code
>
> Here is my code:
>
> import core.sync.semaphore;
> import core.thread;
> import std.string;
> import std.stdio;
>
> shared string data;
> shared Semaphore sem = new Semaphore();
This tries to create a new Semaphore class instance at compile time.
Instead, do this:
shared Semaphore sem;
shared static this() {
sem = new Semaphore();
}
Which will run during runtime startup.
Or, you can initialize in main().
-Steve
|
July 27, 2015 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 07/27/2015 12:56 PM, Steven Schveighoffer wrote:
> Instead, do this:
>
> shared Semaphore sem;
> shared static this() {
> sem = new Semaphore();
> }
>
> Which will run during runtime startup.
>
> Or, you can initialize in main().
>
> -Steve
I tried that as well but there are tons of issues with shared. :(
Ali
|
July 27, 2015 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 27 July 2015 at 19:56:15 UTC, Steven Schveighoffer wrote: > On 7/27/15 3:10 PM, Jack Stouffer wrote: >> Hi, >> >> I am currently working through a book on the fundamentals of computer >> concurrency and I wanted to do all of the exercises in D. But I ran into >> a problem when I tried to have a global semaphore: >> >> /usr/local/Cellar/dmd/2.067.1/include/d2/core/sync/semaphore.di(35): >> Error: constructor core.sync.semaphore.Semaphore.this >> core.sync.semaphore.Semaphore cannot be constructed at compile time, >> because the constructor has no available source code >> >> Here is my code: >> >> import core.sync.semaphore; >> import core.thread; >> import std.string; >> import std.stdio; >> >> shared string data; >> shared Semaphore sem = new Semaphore(); > > This tries to create a new Semaphore class instance at compile time. > > Instead, do this: > > shared Semaphore sem; > shared static this() { > sem = new Semaphore(); > } > > Which will run during runtime startup. > > Or, you can initialize in main(). > > -Steve Yes, but then core.sync.semaphore doesn't support being shared, so... I don't really understand how https://github.com/D-Programming-Language/druntime/blob/master/src/core/sync/semaphore.d#L356 is managing to avoid this |
July 27, 2015 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote:
> Yes, but then core.sync.semaphore doesn't support being shared, so...
Ok, so I made the code run by using __gshared instead of shared. It seems really odd that a semaphore object doesn't support being shared, this that a bug?
Here is the modified code:
import core.sync.semaphore;
import core.thread;
import std.string;
import std.stdio;
__gshared string data;
__gshared Semaphore sem;
void read() {
data = "From Thread";
sem.notify();
}
void write() {
sem.wait();
data.writeln;
}
void main() {
sem = new Semaphore();
Thread reader = new Thread(&read);
Thread writer = new Thread(&write);
reader.start();
writer.start();
}
|
March 21, 2016 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote:
>
> Yes, but then core.sync.semaphore doesn't support being shared, so...
>
> I don't really understand how https://github.com/D-Programming-Language/druntime/blob/master/src/core/sync/semaphore.d#L356 is managing to avoid this
Since that time is something cleared up? Faced with the same problem
|
July 29, 2016 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to denizzzka | On Monday, 21 March 2016 at 13:19:34 UTC, denizzzka wrote:
> On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote:
>>
>> Yes, but then core.sync.semaphore doesn't support being shared, so...
>>
>> I don't really understand how https://github.com/D-Programming-Language/druntime/blob/master/src/core/sync/semaphore.d#L356 is managing to avoid this
>
> Since that time is something cleared up? Faced with the same problem
And same for me also; I really need shared semaphores, for implementing complex synchronization queues (they have to be nested inside shared classes). That's rather disappointing to offer semaphores without thread sharing mechanisms beyond __gshared. Is there anything we could do to solve this?
best
NB
thanks to all D contributors
|
October 08, 2018 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | On Monday, 27 July 2015 at 20:57:00 UTC, Jack Stouffer wrote:
> On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote:
>> Yes, but then core.sync.semaphore doesn't support being shared, so...
>
> Ok, so I made the code run by using __gshared instead of shared. It seems really odd that a semaphore object doesn't support being shared, this that a bug?
>
> Here is the modified code:
>
> import core.sync.semaphore;
> import core.thread;
> import std.string;
> import std.stdio;
>
> __gshared string data;
> __gshared Semaphore sem;
>
>
> void read() {
> data = "From Thread";
> sem.notify();
> }
>
>
> void write() {
> sem.wait();
> data.writeln;
> }
>
>
> void main() {
> sem = new Semaphore();
> Thread reader = new Thread(&read);
> Thread writer = new Thread(&write);
>
> reader.start();
> writer.start();
> }
I JUST found this after several hours of searching. Thank you, Jack.
Now if sharing a semaphore could be thing, I'd be happy. It's honestly one of (it's actually a long list) the major blocks to us using D primarily.
|
October 09, 2018 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt Richardson | On 09/10/2018 4:43 AM, Matt Richardson wrote:
> On Monday, 27 July 2015 at 20:57:00 UTC, Jack Stouffer wrote:
>> On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote:
>>> Yes, but then core.sync.semaphore doesn't support being shared, so...
>>
>> Ok, so I made the code run by using __gshared instead of shared. It seems really odd that a semaphore object doesn't support being shared, this that a bug?
>>
>> Here is the modified code:
>>
>> import core.sync.semaphore;
>> import core.thread;
>> import std.string;
>> import std.stdio;
>>
>> __gshared string data;
>> __gshared Semaphore sem;
>>
>>
>> void read() {
>> data = "From Thread";
>> sem.notify();
>> }
>>
>>
>> void write() {
>> sem.wait();
>> data.writeln;
>> }
>>
>>
>> void main() {
>> sem = new Semaphore();
>> Thread reader = new Thread(&read);
>> Thread writer = new Thread(&write);
>>
>> reader.start();
>> writer.start();
>> }
>
> I JUST found this after several hours of searching. Thank you, Jack.
>
> Now if sharing a semaphore could be thing, I'd be happy. It's honestly one of (it's actually a long list) the major blocks to us using D primarily.
shared doesn't do much, so if __gshared works, use it (that is what it is there for). So it isn't a blocker.
|
October 08, 2018 Re: Can't Compile Global Semaphores? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | I recently rewrote some of my threading code in cgi.d and used the core.sync.semaphore with core.threads... never once used shared or __gshared. I just passed the handle of the semaphore to the new thread constructor. All seems to work. |
Copyright © 1999-2021 by the D Language Foundation