面试题1

来源:互联网 发布:厨房用品知乎 编辑:程序博客网 时间:2024/06/02 02:32

<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]# ./testsum=17091[root@ c-prog]# ./testsum=12031[root@ c-prog]# 

全局变量的并没有保护。

0 0
原创粉丝点击