Jump to page: 1 2
Thread overview
Acess variable that was set by thread
Aug 08, 2022
vc
Aug 08, 2022
Emanuele Torre
Aug 08, 2022
vc
Aug 08, 2022
ag0aep6g
Aug 08, 2022
bauss
Aug 08, 2022
ag0aep6g
Aug 08, 2022
bauss
Aug 08, 2022
kdevel
Aug 08, 2022
ag0aep6g
Aug 08, 2022
ag0aep6g
Aug 08, 2022
ag0aep6g
Aug 08, 2022
ag0aep6g
Aug 08, 2022
ag0aep6g
Aug 08, 2022
frame
Aug 08, 2022
Ali Çehreli
August 08, 2022
Hello, i have the following code, the flora contains a boolean zeus
in the DerivedThread the boolean zeus was set to true; but when i'm trying to access it
outside the thread in main it returns me false; any thoughts ?

import flora;

class DerivedThread : Thread
{
    this()
    {
        super(&run);
    }

private:
    void run()
    {
        // Derived thread running.
        zeus = true;

        while(true)
        {

        }

    }
}




void main()
{

    auto derived = new DerivedThread().start();

    writeln(zeus);

}
August 08, 2022
On Monday, 8 August 2022 at 01:36:45 UTC, vc wrote:
> Hello, i have the following code, the flora contains a boolean zeus
> in the DerivedThread the boolean zeus was set to true; but when i'm trying to access it
> outside the thread in main it returns me false; any thoughts ?
>
> import flora;
>
> class DerivedThread : Thread
> {
>     this()
>     {
>         super(&run);
>     }
>
> private:
>     void run()
>     {
>         // Derived thread running.
>         zeus = true;
>
>         while(true)
>         {
>
>         }
>
>     }
> }
>
>
>
>
> void main()
> {
>
>     auto derived = new DerivedThread().start();
>
>     writeln(zeus);
>
> }

When `writeln(zeus)` runs, `zeus = true` probably was not evaluated yet.
August 07, 2022

On 8/7/22 9:36 PM, vc wrote:

>

Hello, i have the following code, the flora contains a boolean zeus
in the DerivedThread the boolean zeus was set to true; but when i'm trying to access it
outside the thread in main it returns me false; any thoughts ?

is zeus declared just as:

bool zeus;

Because if so, it is in thread local storage. This is different per thread. This means, each thread gets its own copy, and writing to the copy in one thread doesn't affect any other threads.

Note that Emanuele is also right that you have a race condition in any case. So you likely have 2 problems going on.

-Steve

August 08, 2022

On Monday, 8 August 2022 at 02:49:06 UTC, Steven Schveighoffer wrote:

>

On 8/7/22 9:36 PM, vc wrote:

>

Hello, i have the following code, the flora contains a boolean zeus
in the DerivedThread the boolean zeus was set to true; but when i'm trying to access it
outside the thread in main it returns me false; any thoughts ?

is zeus declared just as:

bool zeus;

Because if so, it is in thread local storage. This is different per thread. This means, each thread gets its own copy, and writing to the copy in one thread doesn't affect any other threads.

Note that Emanuele is also right that you have a race condition in any case. So you likely have 2 problems going on.

-Steve

yes it is declared as

bool zeus;

it seems change it to working is working

 __gshared bool zeus;

but as I'm new in to D, i will like to hear thoughts even if it works for me

August 08, 2022

On Monday, 8 August 2022 at 07:14:33 UTC, vc wrote:

>

it seems change it to working is working

 __gshared bool zeus;

but as I'm new in to D, i will like to hear thoughts even if it works for me

Never ever use __gshared ever. It's a glaring safety hole. Use shared instead.

If you're running into compilation errors with shared, that's the compiler trying to keep you from shooting your foot off. You're supposed to think hard about thread-safety and only then cast shared away in the right spot.

With __gshared, the compiler just pretends that it doesn't see that the variable is shared. You're pretty much guaranteed to produce race conditions unless you think even harder than you would have with shared.

By the way, is there some resource that recommends __gshared over shared? It seems that many newbies reach for __gshared first for some reason.

August 08, 2022

On Monday, 8 August 2022 at 10:17:57 UTC, ag0aep6g wrote:

>

Never ever use __gshared ever.

I don't agree with this entirely, it just depends on how you use it. In general you should go with shared, but __gshared does have its places. It's only problematic when it can be changed from multiple threads, but if it's only changed from a single thread but read from many then it generally isn't a problem.

To sum it up:

Single-write/Single-read?
__gshared

Single-write/Multi-read?
__gshared

Multi-write/Single-read?
shared

Multi-write/Multi-read?
shared

August 08, 2022

On 8/8/22 6:17 AM, ag0aep6g wrote:

>

On Monday, 8 August 2022 at 07:14:33 UTC, vc wrote:

>

it seems change it to working is working

 __gshared bool zeus;
 ```

but as I'm new in to D, i will like to hear thoughts even if it works for me

Never ever use __gshared ever. It's a glaring safety hole. Use shared instead.

If you are interfacing with C, you need __gshared. But yeah, you should use shared in this case.

-Steve

August 08, 2022
On 8/8/22 00:14, vc wrote:

> i will like to hear thoughts even if it works
> for me

__gshared would work as well but I would consider std.concurrency first. Just a simple example:

import std.stdio;
import std.concurrency;
import core.thread;

struct Result {
  int value;
}

struct Done {
}

void run()
{
  bool done = false;
  while (!done) {
    writeln("Derived thread running.");
    receiveTimeout(1.seconds,
        (Done msg) {
            done = true;
        });
  }

  // Send the result to the owner
  // (I made assumptions; the thread may produce
  // results inside the while loop above.)
  ownerTid.send(Result(42));
}

void main()
{
    auto worker = spawn(&run);
    Thread.sleep(5.seconds);
    worker.send(Done());
    auto result = receiveOnly!Result();
    writeln("Here is the result: ", result);
}

Related, Roy Margalit's DConf 2022 presentation was based on traps related to sequential consistency. The video will be moved to a better place but the following link should work for now:

  https://youtu.be/04gJXpJ1i8M?t=5658

Ali

August 08, 2022

On Monday, 8 August 2022 at 12:45:20 UTC, bauss wrote:

>

On Monday, 8 August 2022 at 10:17:57 UTC, ag0aep6g wrote:

>

Never ever use __gshared ever.
[...]
To sum it up:

Single-write/Single-read?
__gshared

Single-write/Multi-read?
__gshared

Multi-write/Single-read?
shared

Multi-write/Multi-read?
shared

Nope. All of those can be race conditions.

Here's a single-write, single-read one:

align(64) static struct S
{
    align(1):
    ubyte[60] off;
    ulong x = 0;
}
__gshared S s;
void main()
{
    import core.thread: Thread;
    import std.conv: to;
    new Thread(() {
        foreach (i; 0 .. uint.max)
        {
            s.x = 0;
            s.x = -1;
        }
    }).start();
    foreach (i; 0 .. uint.max)
    {
        auto x = s.x;
        assert(x == 0 || x == -1, to!string(x, 16));
    }
}

If you know how to access the variable safely, you can do it with shared.

I maintain: Never ever use __gshared ever.

August 08, 2022

On Monday, 8 August 2022 at 10:17:57 UTC, ag0aep6g wrote:

>

By the way, is there some resource that recommends __gshared over shared? It seems that many newbies reach for __gshared first for some reason.

Would be also good if the specs would tell more about those "guards":

>

Unlike the shared attribute, __gshared provides no safe-guards against data races or other multi-threaded synchronization issues.

The only thing I see is that the compiler bails about type incompatibilities but how does it help in case of synchronization/locking issues?

« First   ‹ Prev
1 2