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 2db6dfc

Browse files
authored
Fixed a bug and add comments.
1 parent 3b5df1d commit 2db6dfc

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

‎print_seq

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,62 @@ two threads, one prints odd one prints even in order
1313
pthread_mutex_t mtx;
1414
pthread_cond_t cond;
1515

16-
int seq = START_NUMBER;
17-
18-
int thread_run;
16+
int seq;
17+
int run_thread;
1918

2019
typedef struct range_s {
2120
int start;
2221
int end;
2322
} range_t;
2423

2524
int odd_print_one(const int start, const int end) {
26-
25+
int rc = 0;
26+
2727
pthread_mutex_lock(&mtx);
2828

2929
// wait for cond to be singaled by using mtx and cond
30-
while (thread_run == 1) {
30+
while (run_thread != 1) {
3131
pthread_cond_wait(&cond, &mtx);
3232
}
3333
if (seq > end) {
34-
pthread_mutex_unlock(&mtx);
35-
return -1;
34+
rc = -1;
35+
} else {
36+
printf("%d ", seq);
37+
seq ++;
3638
}
37-
printf("%d ", seq);
38-
seq ++;
39-
thread_run = 2;
39+
run_thread = 2;
40+
4041
// siganl cond variable;
4142
pthread_cond_signal(&cond);
4243
pthread_mutex_unlock(&mtx);
4344

44-
return 0;
45+
return rc;
4546
}
4647

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. */
4751
int even_print_one(const int start, const int end) {
48-
52+
int rc = 0;
4953
pthread_mutex_lock(&mtx);
5054

5155
// wait for cond to be singaled by using mtx and cond
52-
while (thread_run == 2) {
56+
while (run_thread != 2) {
5357
pthread_cond_wait(&cond, &mtx);
5458
}
5559
if (seq > end) {
56-
pthread_mutex_unlock(&mtx);
57-
return -1;
60+
rc = -1;
61+
} else {
62+
printf("%d ", seq);
63+
seq ++;
5864
}
59-
printf("%d ", seq);
60-
seq ++;
61-
thread_run = 1;
65+
run_thread = 1;
66+
6267
// siganl cond variable
63-
pthread_cond_signal(&cond);
6468
pthread_mutex_unlock(&mtx);
69+
pthread_cond_signal(&cond);
6570

66-
return 0;
71+
return rc;
6772
}
6873

6974
void *odd_printer(void *args) {
@@ -94,12 +99,13 @@ int main() {
9499
void *ret;
95100

96101
const range_t range = {START_NUMBER, END_NUMBER};
97-
102+
103+
seq = range.start;
104+
run_thread = 1;
105+
98106
pthread_mutex_init(&mtx, NULL);
99107
pthread_cond_init(&cond, NULL);
100108

101-
thread_run = 1;
102-
103109
rc = pthread_create(&odd_thread, NULL, odd_printer, (void *)&range);
104110
if (rc != 0) {
105111
goto cu1;
@@ -112,9 +118,9 @@ int main() {
112118

113119
// clean up
114120
pthread_join(even_thread, &ret);
115-
116121
cu0:
117-
pthread_join(odd_thread, &ret);
122+
pthread_join(odd_thread, &ret);
123+
118124
cu1:
119125
printf("\n");
120126
pthread_mutex_destroy(&mtx);

0 commit comments

Comments
(0)

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