December 02, 2015 mutex usage problem? | ||||
---|---|---|---|---|
| ||||
The following code does core dump (compiled with gdc). Pointers will be appreciated. import std.stdio; import std.conv; import std.math; import std.concurrency; import core.thread; import core.sync.mutex; enum count = 5; __gshared double rslt = 0.0; __gshared Mutex mutex; void term(immutable(int)* nterm) { double r; auto n = to!double(*nterm); r = 4.0*pow(-1.0, *nterm)/(2.0*n + 1); writefln("%s: %6.6f.", *nterm, r); mutex.lock(); rslt = rslt + r; mutex.unlock(); } void main() { foreach (i; 0 .. count) { int* jm = new int; *jm = i; immutable int *j = cast(immutable) jm; Tid tid = spawn(&term, j); } thread_joinAll(); writefln("Result: %6.9f.", rslt); } -ish |
December 02, 2015 Re: mutex usage problem? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ish | On Wednesday, 2 December 2015 at 13:55:02 UTC, Ish wrote:
> The following code does core dump (compiled with gdc). Pointers will be appreciated.
> import std.stdio;
> import std.conv;
> import std.math;
> import std.concurrency;
> import core.thread;
> import core.sync.mutex;
>
> enum count = 5;
> __gshared double rslt = 0.0;
> __gshared Mutex mutex;
>
> void term(immutable(int)* nterm) {
> double r;
> auto n = to!double(*nterm);
> r = 4.0*pow(-1.0, *nterm)/(2.0*n + 1);
> writefln("%s: %6.6f.", *nterm, r);
> mutex.lock();
> rslt = rslt + r;
> mutex.unlock();
> }
>
>
> void main() {
>
> foreach (i; 0 .. count) {
> int* jm = new int;
> *jm = i;
> immutable int *j = cast(immutable) jm;
> Tid tid = spawn(&term, j);
> }
>
> thread_joinAll();
> writefln("Result: %6.9f.", rslt);
> }
>
> -ish
You never initialize Mutex, so it is a null pointer when you call `mutex.lock`.
Also, no point in passing a pointer here; just pass `i` instead of `j`.
|
Copyright © 1999-2021 by the D Language Foundation