Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c476b9b

Browse files
authored
Create alternate_threads.c
1 parent 2db6dfc commit c476b9b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

‎alternate_threads.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
output 1 - 100
3+
two threads, one prints odd one prints even in order
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <pthread.h>
9+
10+
#define START_NUMBER 1
11+
#define END_NUMBER 100
12+
13+
pthread_mutex_t mtx;
14+
pthread_cond_t cond;
15+
16+
int seq;
17+
int run_thread;
18+
19+
typedef struct range_s {
20+
int start;
21+
int end;
22+
} range_t;
23+
24+
int odd_print_one(const int start, const int end) {
25+
int rc = 0;
26+
27+
pthread_mutex_lock(&mtx);
28+
29+
// wait for cond to be singaled by using mtx and cond
30+
while (run_thread != 1) {
31+
pthread_cond_wait(&cond, &mtx);
32+
}
33+
if (seq > end) {
34+
rc = -1;
35+
} else {
36+
printf("%d ", seq);
37+
seq ++;
38+
}
39+
run_thread = 2;
40+
41+
// siganl cond variable;
42+
pthread_cond_signal(&cond);
43+
pthread_mutex_unlock(&mtx);
44+
45+
return rc;
46+
}
47+
48+
/* most of code is common with odd_print_one(), they can be
49+
incooperated as one function, for readability, I keep them
50+
separated here. */
51+
int even_print_one(const int start, const int end) {
52+
int rc = 0;
53+
pthread_mutex_lock(&mtx);
54+
55+
// wait for cond to be singaled by using mtx and cond
56+
while (run_thread != 2) {
57+
pthread_cond_wait(&cond, &mtx);
58+
}
59+
if (seq > end) {
60+
rc = -1;
61+
} else {
62+
printf("%d ", seq);
63+
seq ++;
64+
}
65+
run_thread = 1;
66+
67+
// siganl cond variable
68+
pthread_mutex_unlock(&mtx);
69+
pthread_cond_signal(&cond);
70+
71+
return rc;
72+
}
73+
74+
void *odd_printer(void *args) {
75+
int rc;
76+
const int start = ((range_t *)args)->start;
77+
const int end = ((range_t *)args)->end;
78+
do {
79+
rc = odd_print_one(start, end);
80+
} while (rc == 0);
81+
82+
return NULL;
83+
}
84+
85+
void *even_printer(void *args) {
86+
int rc;
87+
const int start = ((range_t *)args)->start;
88+
const int end = ((range_t *)args)->end;
89+
do {
90+
rc = even_print_one(start, end);
91+
} while(rc == 0);
92+
93+
return NULL;
94+
}
95+
96+
int main() {
97+
pthread_t odd_thread, even_thread;
98+
int rc;
99+
void *ret;
100+
101+
const range_t range = {START_NUMBER, END_NUMBER};
102+
103+
seq = range.start;
104+
run_thread = 1;
105+
106+
pthread_mutex_init(&mtx, NULL);
107+
pthread_cond_init(&cond, NULL);
108+
109+
rc = pthread_create(&odd_thread, NULL, odd_printer, (void *)&range);
110+
if (rc != 0) {
111+
goto cu1;
112+
}
113+
rc = pthread_create(&even_thread, NULL, even_printer, (void *)&range);
114+
if (rc != 0) {
115+
pthread_kill(odd_thread, 0);
116+
goto cu0;
117+
}
118+
119+
// clean up
120+
pthread_join(even_thread, &ret);
121+
cu0:
122+
pthread_join(odd_thread, &ret);
123+
124+
cu1:
125+
printf("\n");
126+
pthread_mutex_destroy(&mtx);
127+
pthread_cond_destroy(&cond);
128+
129+
return rc;
130+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /