C语言计算线程负责计算(1到100的累加,每次加一个数后都要打印),结果为偶数由打印线程1打印,奇数由打印线程2打印
时间: 2024-05-03 13:17:54 浏览: 100
下面是一种可能的实现:
```c
#include <stdio.h>
#include <pthread.h>
int sum = 0; // 累加和
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
pthread_cond_t cond_even = PTHREAD_COND_INITIALIZER; // 偶数条件变量
pthread_cond_t cond_odd = PTHREAD_COND_INITIALIZER; // 奇数条件变量
int is_even = 1; // 当前是否为偶数
void* compute_thread(void* arg) {
for (int i = 1; i <= 100; ++i) {
pthread_mutex_lock(&mutex);
sum += i;
printf("Compute: %d\n", i);
is_even = !is_even;
if (is_even) {
pthread_cond_signal(&cond_even);
} else {
pthread_cond_signal(&cond_odd);
}
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void* print_thread1(void* arg) {
for (int i = 1; i <= 100; i += 2) {
pthread_mutex_lock(&mutex);
while (is_even) {
pthread_cond_wait(&cond_odd, &mutex);
}
printf("Print1: %d\n", sum);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void* print_thread2(void* arg) {
for (int i = 2; i <= 100; i += 2) {
pthread_mutex_lock(&mutex);
while (!is_even) {
pthread_cond_wait(&cond_even, &mutex);
}
printf("Print2: %d\n", sum);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t compute_tid, print1_tid, print2_tid;
pthread_create(&compute_tid, NULL, compute_thread, NULL);
pthread_create(&print1_tid, NULL, print_thread1, NULL);
pthread_create(&print2_tid, NULL, print_thread2, NULL);
pthread_join(compute_tid, NULL);
pthread_join(print1_tid, NULL);
pthread_join(print2_tid, NULL);
return 0;
}
```
其中:
- `sum` 是累加和;
- `mutex` 是互斥锁,保证多个线程对 `sum` 的访问不会冲突;
- `cond_even` 和 `cond_odd` 是条件变量,用于线程间的同步;
- `is_even` 表示当前是否为偶数;
- `compute_thread` 是计算线程,每次加一个数后打印并通知等待的打印线程;
- `print_thread1` 和 `print_thread2` 分别是打印线程,根据 `is_even` 判断是否打印,如果不是则等待条件变量通知。
这个程序的输出可能是:
```
Compute: 1
Print2: 1
Compute: 2
Print1: 3
Compute: 3
Print2: 6
Compute: 4
Print1: 10
Compute: 5
Print2: 15
Compute: 6
Print1: 21
Compute: 7
Print2: 28
Compute: 8
Print1: 36
Compute: 9
Print2: 45
Compute: 10
Print1: 55
Compute: 11
Print2: 66
Compute: 12
Print1: 78
Compute: 13
Print2: 91
Compute: 14
Print1: 105
Compute: 15
Print2: 120
Compute: 16
Print1: 136
Compute: 17
Print2: 153
Compute: 18
Print1: 171
Compute: 19
Print2: 190
Compute: 20
Print1: 210
Compute: 21
Print2: 231
Compute: 22
Print1: 253
Compute: 23
Print2: 276
Compute: 24
Print1: 300
Compute: 25
Print2: 325
Compute: 26
Print1: 351
Compute: 27
Print2: 378
Compute: 28
Print1: 406
Compute: 29
Print2: 435
Compute: 30
Print1: 465
Compute: 31
Print2: 496
Compute: 32
Print1: 528
Compute: 33
Print2: 561
Compute: 34
Print1: 595
Compute: 35
Print2: 630
Compute: 36
Print1: 666
Compute: 37
Print2: 703
Compute: 38
Print1: 741
Compute: 39
Print2: 780
Compute: 40
Print1: 820
Compute: 41
Print2: 861
Compute: 42
Print1: 903
Compute: 43
Print2: 946
Compute: 44
Print1: 990
Compute: 45
Print2: 1035
Compute: 46
Print1: 1081
Compute: 47
Print2: 1128
Compute: 48
Print1: 1176
Compute: 49
Print2: 1225
Compute: 50
Print1: 1275
Compute: 51
Print2: 1326
Compute: 52
Print1: 1378
Compute: 53
Print2: 1431
Compute: 54
Print1: 1485
Compute: 55
Print2: 1540
Compute: 56
Print1: 1596
Compute: 57
Print2: 1653
Compute: 58
Print1: 1711
Compute: 59
Print2: 1770
Compute: 60
Print1: 1830
Compute: 61
Print2: 1891
Compute: 62
Print1: 1953
Compute: 63
Print2: 2016
Compute: 64
Print1: 2080
Compute: 65
Print2: 2145
Compute: 66
Print1: 2211
Compute: 67
Print2: 2278
Compute: 68
Print1: 2346
Compute: 69
Print2: 2415
Compute: 70
Print1: 2485
Compute: 71
Print2: 2556
Compute: 72
Print1: 2628
Compute: 73
Print2: 2701
Compute: 74
Print1: 2775
Compute: 75
Print2: 2850
Compute: 76
Print1: 2926
Compute: 77
Print2: 3003
Compute: 78
Print1: 3081
Compute: 79
Print2: 3160
Compute: 80
Print1: 3240
Compute: 81
Print2: 3321
Compute: 82
Print1: 3403
Compute: 83
Print2: 3486
Compute: 84
Print1: 3570
Compute: 85
Print2: 3655
Compute: 86
Print1: 3741
Compute: 87
Print2: 3828
Compute: 88
Print1: 3916
Compute: 89
Print2: 4005
Compute: 90
Print1: 4095
Compute: 91
Print2: 4186
Compute: 92
Print1: 4278
Compute: 93
Print2: 4371
Compute: 94
Print1: 4465
Compute: 95
Print2: 4560
Compute: 96
Print1: 4656
Compute: 97
Print2: 4753
Compute: 98
Print1: 4851
Compute: 99
Print2: 4950
Compute: 100
Print1: 5050
```
阅读全文