Can't Compile Global Semaphores?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 27 13:12:07 PDT 2015


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


More information about the Digitalmars-d-learn mailing list