1 |
edhill |
1.1 |
/* |
2 |
|
|
* $Header: $ |
3 |
|
|
* $Name: $ |
4 |
|
|
* |
5 |
|
|
* |
6 |
|
|
* A PTHREADS convenience wrapper intended primarily for use with |
7 |
|
|
* gcc/g77 which does not support an "automatic" (here, meant as a |
8 |
|
|
* compiler-supported) threading mechanism. |
9 |
|
|
* |
10 |
|
|
*/ |
11 |
|
|
|
12 |
|
|
#ifdef HAVE_PTHREADS |
13 |
|
|
|
14 |
|
|
#include <stdlib.h> |
15 |
|
|
#include <unistd.h> |
16 |
|
|
#include <sys/stat.h> |
17 |
|
|
#include <sys/types.h> |
18 |
|
|
#include <sys/wait.h> |
19 |
|
|
#include <errno.h> |
20 |
|
|
#include <pthread.h> |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
void ptreentry_(int* myThid); |
24 |
|
|
|
25 |
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
26 |
|
|
int num_threads = 0; |
27 |
|
|
|
28 |
|
|
void * |
29 |
|
|
threadfunc(void *parm) |
30 |
|
|
{ |
31 |
|
|
int myThid; |
32 |
|
|
int rc; |
33 |
|
|
|
34 |
|
|
rc = pthread_mutex_lock(&mutex); |
35 |
|
|
num_threads++; |
36 |
|
|
myThid = num_threads; |
37 |
|
|
rc = pthread_mutex_unlock(&mutex); |
38 |
|
|
ptreentry_(&myThid); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
void |
42 |
|
|
ptinit_(int* nthreads) |
43 |
|
|
{ |
44 |
|
|
int i,j; |
45 |
|
|
int rc; |
46 |
|
|
pthread_t threadids[10]; |
47 |
|
|
pthread_attr_t pta; |
48 |
|
|
|
49 |
|
|
pthread_attr_init(&pta); |
50 |
|
|
pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE); |
51 |
|
|
|
52 |
|
|
/* start the threads */ |
53 |
|
|
for (i=0; i<(*nthreads); i++) |
54 |
|
|
rc = pthread_create(&threadids[i], &pta, threadfunc, NULL); |
55 |
|
|
|
56 |
|
|
/* wait on thread termination */ |
57 |
|
|
for (i=0; i<(*nthreads); i++) |
58 |
|
|
rc = pthread_join(threadids[i], NULL); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
#endif /* HAVE_PTHREADS */ |