illinois-cs241 / coursebook

Open Source Introductory Systems Programming Textbook for the University of Illinois
http://cs241.cs.illinois.edu/coursebook
682 stars 85 forks source link

Race Conditions section typo #170

Closed RidhwaanDev closed 4 years ago

RidhwaanDev commented 4 years ago

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

aneeshdurg commented 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.

aneeshdurg commented 4 years ago

Either way, this seems like a pretty small fix, you should make a patch to fix it and submit a PR!

RidhwaanDev commented 4 years ago

Finally got around to doing it lol.