Closed RidhwaanDev closed 4 years ago
I think it was originally supposed to be int x = *(int*) p
, but without reading the section in context I'm not sure if your version is more accurate. In your version, the last stmt is unnecessary, and the value of *x in the sum is unpredictable. In version above with my proposed fix, x will be the value in the pointer at the time the thread first dereferences the ptr.
Either way, this seems like a pretty small fix, you should make a patch to fix it and submit a PR!
Finally got around to doing it lol.
In Threads under the subsection Race Condition the example code of a race condition is:
void *thread_main(void *p) { int x = *p; x += x; *p = x; return NULL; }
The following line is incorrect and won't compile with gcc file.c -o c
int x = *p; x += x; *p = x;
It should be:int *x = (int*) p; *x = *x + *x; ptr = x;
Correct me if I am wrong