-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_thread.c
48 lines (38 loc) · 1.01 KB
/
test_thread.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define NUM_THREADS 5
struct thrd_args {
int tid;
char *message;
};
void *printHello(void *thrd_args){
struct thrd_args *myarg;
myarg = (struct thrd_args *) thrd_args;
int my_tid = myarg->tid;
char *my_messsage = myarg->message;
printf("%s", my_messsage);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc, t, tids[NUM_THREADS];
struct thrd_args *args;
args = (struct thrd_args *)malloc(NUM_THREADS*sizeof(struct thrd_args));
for (t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %d\n", t);
args[t].tid = t;
args[t].message = "Hello World\n";
rc = pthread_create(&threads[t], NULL, printHello, (void*)&args[t]);
if(rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}