Serious extern(C) bug

Walter Bright newshound2 at digitalmars.com
Tue Sep 24 21:38:27 PDT 2013


On 9/24/2013 9:13 PM, "Luís Marques" <luis at luismarques.eu>" wrote:
> Have you seen this one before? Do you know a workaround? (DMD v2.063.2, on OX X
> 10.9)
>
> file.d:
>
>      extern(C)
>      {
>          int x = 0;

This x is put in thread local storage.

>          void setx();
>          void printx();
>      }
>
>      void main()
>      {
>          setx(); // sets x = 42
>          writeln(x); // prints x = 0
>          printx(); // prints x = 42
>          x = 7;
>          printx(); // prints x = 42
>      }
>
>
>
> file.c:
>
>      #include <stdio.h>
>
>      extern int x;

This x is put in thread global storage.

>
>      void setx()
>      {
>          x = 42;
>      }
>
>      void printx()
>      {
>          printf("%d\n", x);
>      }
>
> Output:
>
>      0
>      42
>      42

So you've got two different x's here.

Declare the first one as:

     __gshared int x = 0;


More information about the Digitalmars-d mailing list