UNIX线程操作

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <pthread.h>
#include <stdio.h >
#include <stdlib.h>
/*Linux Complie Command: gcc thread.c -o thread -plthread */
int nthreads = 1;

void *dowork (void *params)
{
int j = *(int *)params;
int term = (j+1)*(j+1);
*(int *)params = term;
printf("the thread [%d]: term =%d\n",j,term);
}

void main(int argc, char **argv)
{
int i;
pthread_t threads[100];
int pthread_data[100];
int sum = 0;
if (argc==2)
nthreads = atoi (argv[1]);
for (i=0; i<nthreads; i++)
{
pthread_data[i]=i ;
pthread_create(&threads[i], NULL, dowork,&pthread_data[i]);
}
for (i=0; i<nthreads; i++)
{
pthread_join(threads [i], NULL);
sum+=pthread_data[i];
}
printf("The Sum = %d\n", sum);
}