i have single core cpu (arm cortex m3, 32bit) 2 threads. assuming following situation:
// thread 1: int16_t = 1; double b = 1.0; // other fancy stuff including starting thread 2 (;;) {std::cout << << "," <<b;} // thread 2: = 2; b = 2.0;
i can handle following outputs:
- 1,1
- 1,2
- 2,1
- 2,2
can output 1 of (1/2) without using mutex
or other locking mechanisms? , more specifically, compiler dependent? , behavior different int16
, double
?
it depends on cpu, mostly, though in theory involving multiple threads on pre-c11 @ best implementation defined , @ worst undefined behavior, compiler might anything.
if can ignore crazy compilers silly things, , assume compiler use cpu's facilities in reasonable way, depends on cpu supports.
cortex-m3 32-bit cpu 32-bit bus , no fpu. reads , writes of 32-bit , smaller values atomic. double
, however, 64 bits, read/write of double involve 2 instructions , non-atomic. if 1 thread reads while other writing might half 1 value , half other.
now in specific example, values 1.0 , 2.0 both 0 lower half, 'mix' innocuous, other values not have behavior.
Comments
Post a Comment