-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread.c
More file actions
38 lines (33 loc) · 1.01 KB
/
pthread.c
File metadata and controls
38 lines (33 loc) · 1.01 KB
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
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#define THREAD_NUM 10
void *do_something(void *ptr);
pthread_t tid[THREAD_NUM];
int main(int argc, char *argv[]){
for(int i = 0 ; i < THREAD_NUM; i++){
int *input = (int*)malloc(sizeof(int));
*input = i * 100;
// create a new thread.
// pthread_create (ptr to a pthread_id, NULL, function to run, pointer to the parameter)
pthread_create(&tid[i], NULL, &do_something, (void*)input);
}
for(int i = 0 ; i < THREAD_NUM; i++){
// wait a thread to be finished
// pthread_join(ptr to a pthread_id, NULL)
pthread_join(tid[i], NULL);
printf("[Index %d] join pthread id %ld\n", i, tid[i]);
}
}
void *do_something(void *arg){
pthread_t id = pthread_self();
int input = *(int*)arg;
for(int i = 0; i < THREAD_NUM; i++){
if(pthread_equal(id, tid[i])){
printf("[Index %d] pthread id %ld with input value %d\n", i, tid[i], input);
break;
}
}
pthread_exit((void*)NULL);
}