-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiThread.c
54 lines (39 loc) · 1.29 KB
/
multiThread.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
49
50
51
52
53
54
#include <stdio.h>
#include <pthread.h>
#define THREAD_COUNT 4
int sharedData = 0;
pthread_mutex_t mutex;
pthread_barrier_t barrier;
void* threadFunction(void* arg) {
int n = *(int*)arg;
// Wait until all threads are ready to start
pthread_barrier_wait(&barrier);
// Critical section (protected by a mutex)
pthread_mutex_lock(&mutex);
for (int i = 0; i < n; i++) {
// Your thread's logic here
}
printf("%d times looped with multithreading.\n", n);
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t threads[THREAD_COUNT];
int threadData[THREAD_COUNT] = {1000, 100, 10, 1};
pthread_mutex_init(&mutex, NULL);
pthread_barrier_init(&barrier, NULL, THREAD_COUNT + 1); // Additional +1 for the main thread
printf("-----------------\n");
for (int i = 0; i < THREAD_COUNT; i++) {
pthread_create(&threads[i], NULL, threadFunction, &threadData[i]);
}
// Wait for all threads to be created
pthread_barrier_wait(&barrier);
// Release the barrier to start all threads simultaneously
pthread_barrier_destroy(&barrier);
// Wait for all threads to finish
for (int i = 0; i < THREAD_COUNT; i++) {
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&mutex);
return 0;
}