6.30.2008

编译多线程

比如一下的多线程程序:

#include
#include

#define CNT 1000

int var;

void * thread_func(){
int i;
for (i=0; i
var=var+2;
}
printf("var=%d\n", var);
return 0;
}


int main(){
pthread_t t1,t2,t3;
int ret;

pthread_create(&t1, NULL, &thread_func, NULL);
pthread_create(&t2, NULL, &thread_func, NULL);
pthread_create(&t3, NULL, &thread_func, NULL);

for(;;){}

return ret;
}

如果编译的话,应该在GCC后面加入-pthread的参数:

gcc -o test thread.o -pthread

输出如下:
var=2000
var=4000
var=6000
...

没有评论: