示例#include<pthread.h>#include<stdio.h>int sum;/* this sum data is shared by the thread(s) *//* threads call this function */void *runner(void *param);int main(int argc, char *argv[]){ pthread t tid; /* the thread identifier */ /* set of thread attributes */ pthread attr t attr; if (argc != 2){ fprintf(stderr,"usage: a.out
"); return -1; } if (atoi(argv[1]) < 0){ fprintf(stderr,"%d must be >= 0
",atoi(argv[1])); return -1; } /* get the default attributes */ pthread attr init(&attr); /* create the thread */ pthread create(&tid,&attr,runner,argv[1]); /* wait for the thread to exit */ pthread join(tid,null); printf("sum = %d
",sum);}/* the thread will now begin control in this function */void *runner(void *param){ int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; pthread exit(0);}
使用pthreads api的多线程c程序。
以上就是posix线程库的详细内容。
