我想用一个例子来说明我的问题。
假设有一组N /*(N>>1)*/
线程被设置为运行这个函数:
void Process() {
//Some thread safe processing which requires in-deterministic computation time
unsigned char byte;
std::cin >> byte;
}
一旦所有这些都同时启动,会发生什么?如何处理并发 std::cin 访问?在控制台上操作的最终用户看到/体验了什么?
编辑:我还想补充一件事。下面的代码是否足够安全,可以放弃仅在一个(可能是主)线程中使用 std:cin 的想法?
void Process() {
//Some thread safe processing which requires in-deterministic computation time
//Mutex lock
unsigned char byte;
std::cin >> byte;
//Mutex unlock
}