给你个例子你编译试试:
[CODE]
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void task1(int *counter);
void task2(int *counter);
void cleanup(int counter1,int counter2);
int g1=0;
int g2=0;
int main()
{
pthread_t thrd1,thrd2;
int ret;
ret=pthread_create(&thrd1,NULL,(void *)task1,(void*)&g1);
if(ret)
{
perror("pthread_creat : task1");
exit(EXIT_FAILURE);
}
ret=pthread_create(&thrd2,NULL,(void *)task2,(void*)&g2);
if(ret)
{
perror("pthread_creat : task2");
exit(EXIT_FAILURE);
}
pthread_join(thrd2,NULL);
pthread_join(thrd1,NULL);
cleanup(g1,g2);
exit(EXIT_SUCCESS);
}
void task1(int *counter)
{
while(*counter < 5){
printf("task1 count: %d\n",*counter);
(*counter)++;
}
}
void task2(int *counter)
{
while(*counter < 5){
printf("task2 count: %d\n",*counter);
(*counter)++;
}
}
void cleanup(int counter1,int counter2)
{
printf("total iterations : %d",counter1+counter2);
}
[/CODE]
将这个文件命名为thread.c ,然后在shell下这样编译:gcc thread.c -o thread -lpthread
应该不会出错,我已经试过了,编译OK后再执行:./thread
然后,然后你就看到这几个线程的输出了