-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadfxn.c
61 lines (49 loc) · 1.33 KB
/
threadfxn.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
55
56
57
58
59
60
61
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 project2 threadfxn for part2/3
This is my own work.
*/
#ifndef TFXN_C
#define TFXN_C
#include "threadfxn.h"
void init() {
pthread_mutex_init(&plock, NULL);
pthread_mutex_init(&slock, NULL);
}
void* publisher(void* param) {
/* wait for main thread's flag change. BUSY WAITING */
while (!flag) {
sleep(1);
}
pthread_mutex_lock(&plock);
thread_info* t_info = (thread_info *) param;
int num = t_info->thread_num;
printf("publisher thread %d reading from %s\n", num, t_info->str);
pthread_mutex_unlock(&plock);
free(t_info->str);
return NULL;
}
void* subscriber(void* param) {
/* wait for main thread's flag change. BUSY WAITING */
while(!flag) {
sleep(1);
}
pthread_mutex_lock(&slock);
thread_info* t_info = (thread_info *) param;
int num = t_info->thread_num;
printf("subscriber thread %d reading from %s\n", num, t_info->str);
free(t_info->str);
pthread_mutex_unlock(&slock);
return NULL;
}
void* cleanup(void* param) {
pthread_t tid = pthread_self();
printf("dequeue thread created!\n");
while (!done) {
sleep(1);
}
printf("Current Cleanup Thread ID: %d\n", (int) tid);
return NULL;
}
#endif