线程间共享数据的问题
多线程之间共享数据,最大的问题便是数据竞争导致的异常问题。多个线程操作同一块资源,如果不做任何限制,那么一定会发生错误。例如:
[code] 1 int g_nResource = 0; 2 void thread_entry() 3 { 4 for (int i = 0; i < 10000000; ++i) 5 g_nResource++; 6 } 7 8 int main() 9 {10 thread th1(thread_entry);11 thread th2(thread_entry);12 th1.join();13 th2.join();14 cout |