C , C++ unsynchronized threads returning a strange result

后端 未结 4 1477
执念已碎
执念已碎 2020-12-07 02:54

Okay, i have this question in one regarding threads.

there are two unsynchronized threads running simultaneously and using a global resource \"int num\" 1st:

4条回答
  •  借酒劲吻你
    2020-12-07 03:08

    The possible values for num include all possible int values, plus floating point values, strings, and jpegs of nasal demons. Once you invoke undefined behavior, all bets are off.

    More specifically, modifying the same object from multiple threads without synchronization results in undefined behavior. On most real-world systems, the worst effects you see will probably be missing or double increments or decrements, but it could be much worse (memory corruption, crashing, file corruption, etc.). So just don't do it.

    The next upcoming C and C++ standards will include atomic types which can be safely accessed from multiple threads without any synchronization API.

提交回复
热议问题