<1>.下面的代码输出结果;
<2>.下面的代码的问题。
文件test.c
#include <stdio.h>
#include <pthread.h>
unsigned long sum_var=0;
void * sum(void * para)
{
int count= (int)para;
for (;count;count--)
sum_var++;
}
int main()
{
int i;
pthread_t tid;
for(i = 0;i<4;i++)
{
pthread_create(&tid,NULL,sum,(void*)10000);
}
sleep(3);
printf ("sum=%ld\n",sum_var);
}
编译:
gcc -lpthread test.c -o test
输出结果:
[root@ c-prog]# ./test
sum=17091
[root@ c-prog]# ./test
sum=12031
[root@ c-prog]#
全局变量的并没有保护。