www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - help for example with Condition

reply Mikhail <muaddib1981 mail.ru> writes:
I wrote simple example to learn how the work Conditions.
But program closed with signal, what's wrong?

import std.stdio;
import core.thread;
import core.sync.condition;
import core.sync.mutex;


Condition cond;
Mutex mutex;

void threadFunction()
{
     writeln("This is running in a separate thread.");
     Duration d = dur!"msecs"(100);
     writeln(d);
     cond.wait(d);
}

void main() {
     mutex = new Mutex();
     cond = new Condition(mutex);

     auto t = new Thread(&threadFunction);
     t.start();

     t.join();
     writeln("Main thread finished.");
}
Sep 11
next sibling parent reply novicetoo <sorryno em.ail> writes:
cond and mutex are global variables,
and "Starting with dmd version 2.030, the default storage class 
for statics and globals will be thread local storage (TLS)"
https://dlang.org/articles/migrate-to-shared.html
Sep 11
parent reply Mikhail <muaddib1981 mail.ru> writes:
On Thursday, 11 September 2025 at 09:40:22 UTC, novicetoo wrote:
 cond and mutex are global variables,
 and "Starting with dmd version 2.030, the default storage class 
 for statics and globals will be thread local storage (TLS)"
 https://dlang.org/articles/migrate-to-shared.html
I don't understand what I should do? Define global variables as shared?
Sep 11
next sibling parent Mikhail <muaddib1981 mail.ru> writes:
On Thursday, 11 September 2025 at 09:51:55 UTC, Mikhail wrote:
 On Thursday, 11 September 2025 at 09:40:22 UTC, novicetoo wrote:
 cond and mutex are global variables,
 and "Starting with dmd version 2.030, the default storage 
 class for statics and globals will be thread local storage 
 (TLS)"
 https://dlang.org/articles/migrate-to-shared.html
I don't understand what I should do? Define global variables as shared?
I don't read to end. Thank you for your reply, this work.
Sep 11
prev sibling parent novicetoo <sorryno em.ail> writes:
On Thursday, 11 September 2025 at 09:51:55 UTC, Mikhail wrote:
 I don't understand what I should do? Define global variables as 
 shared?
Andrea Fontana reply more good than my. But I hope you read article for new knowledges. Anyway, IMHO, if you want use global variable from two ore more threads, then it should be declared as "shared".
Sep 11
prev sibling parent Andrea Fontana <nospam example.com> writes:
On Thursday, 11 September 2025 at 09:29:29 UTC, Mikhail wrote:
 I wrote simple example to learn how the work Conditions.
 But program closed with signal, what's wrong?

 import std.stdio;
 import core.thread;
 import core.sync.condition;
 import core.sync.mutex;


 Condition cond;
 Mutex mutex;

 void threadFunction()
 {
     writeln("This is running in a separate thread.");
     Duration d = dur!"msecs"(100);
     writeln(d);
     cond.wait(d);
 }

 void main() {
     mutex = new Mutex();
     cond = new Condition(mutex);

     auto t = new Thread(&threadFunction);
     t.start();

     t.join();
     writeln("Main thread finished.");
 }
1) cond & mutex vars are thread local vars. If you init them from the main function, they are not shared with thread. You need to use __gshared o shared. 2) I think you have to do mutex.lock(); and scope(exit) mutex.unlock(); before calling cond.wait(d) or use syncronized(mutex) { } inside thread for example: Here mutex and cond are visible from inside the thread. All local vars are visible. ``` void main() { auto mutex = new Mutex(); auto cond = new Condition(mutex); auto t = new Thread({ writeln("This is running in a separate thread."); Duration d = dur!"msecs"(100); writeln(d); mutex.lock(); scope(exit) mutex.unlock(); cond.wait(d); }); t.start(); t.join(); writeln("Main thread finished."); } ``` ``` void main() { auto mutex = new Mutex(); auto cond = new Condition(mutex); auto t = new Thread({ writeln("This is running in a separate thread."); Duration d = dur!"msecs"(100); writeln(d); synchronized(mutex) { cond.wait(d); } }); t.start(); t.join(); writeln("Main thread finished."); } ``` Andrea
Sep 11