digitalmars.D.learn - help for example with Condition
- Mikhail (23/23) Sep 11 I wrote simple example to learn how the work Conditions.
- novicetoo (4/4) Sep 11 cond and mutex are global variables,
- Mikhail (3/7) Sep 11 I don't understand what I should do? Define global variables as
- Andrea Fontana (45/68) Sep 11 1) cond & mutex vars are thread local vars. If you init them from
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
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
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.htmlI don't understand what I should do? Define global variables as shared?
Sep 11
On Thursday, 11 September 2025 at 09:51:55 UTC, Mikhail wrote:On Thursday, 11 September 2025 at 09:40:22 UTC, novicetoo wrote:I don't read to end. Thank you for your reply, this work.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.htmlI don't understand what I should do? Define global variables as shared?
Sep 11
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
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









Mikhail <muaddib1981 mail.ru> 