mutex usage problem?
Alex Parrill via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 2 06:05:21 PST 2015
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`.
More information about the Digitalmars-d-learn
mailing list