當兩個線程要操作一個全局變量時,會發生什么。
一個線程: 完成20次+1
另一個線程:完成20次+1
結果應該是 : 40
但實際值卻是:20
為什么?當 thread_function() 將 j 的值寫回 myglobal 時, 就覆蓋了主線程所做的修改。
上代碼:
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
//
int myglobal;
void *thread_function(void *arg)
{
int i,j;
for ( i=0; i<20; i++)
{
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) )
{
printf("error creating thread.");
abort();
}
for ( i=0; i<20; i++)
{
myglobal=myglobal+1;
printf("o");
fflush(stdout);
sleep(1);
}
if ( pthread_join ( mythread, NULL ) )
{
printf("error joining thread.");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
exit(0);
}