用c语言设计一段代码,需要包含至少三个线程,并且需要使用全局共享数据、线程间通信以及线程同步技术
时间: 2023-06-14 10:04:20 浏览: 71
以下是使用C语言设计的包含三个线程的代码,其中包括全局共享数据、线程间通信和线程同步技术:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 3
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int fill_ptr = 0;
int use_ptr = 0;
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t fill = PTHREAD_COND_INITIALIZER;
void put(int value) {
buffer[fill_ptr] = value;
fill_ptr = (fill_ptr + 1) % BUFFER_SIZE;
count++;
}
int get() {
int tmp = buffer[use_ptr];
use_ptr = (use_ptr + 1) % BUFFER_SIZE;
count--;
return tmp;
}
void *producer(void *arg) {
int i;
for (i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while (count == BUFFER_SIZE)
pthread_cond_wait(&empty, &mutex);
put(i);
pthread_cond_signal(&fill);
pthread_mutex_unlock(&mutex);
printf("Producer produced %d\n", i);
}
pthread_exit(NULL);
}
void *consumer(void *arg) {
int i;
for (i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while (count == 0)
pthread_cond_wait(&fill, &mutex);
int tmp = get();
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
printf("Consumer consumed %d\n", tmp);
}
pthread_exit(NULL);
}
void *printer(void *arg) {
int i;
for (i = 0; i < 20; i++) {
pthread_mutex_lock(&mutex);
printf("Current buffer content: ");
for (int j = 0; j < count; j++) {
printf("%d ", buffer[(use_ptr + j) % BUFFER_SIZE]);
}
printf("\n");
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int ret;
ret = pthread_create(&threads[0], NULL, producer, NULL);
if (ret) {
printf("Error creating producer thread\n");
exit(-1);
}
ret = pthread_create(&threads[1], NULL, consumer, NULL);
if (ret) {
printf("Error creating consumer thread\n");
exit(-1);
}
ret = pthread_create(&threads[2], NULL, printer, NULL);
if (ret) {
printf("Error creating printer thread\n");
exit(-1);
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
pthread_exit(NULL);
}
```
代码中有三个线程:生产者、消费者和打印者。生产者和消费者线程访问全局共享的缓冲区,使用互斥锁来保护共享数据的一致性。当缓冲区满时,生产者线程等待条件变量 empty;当缓冲区为空时,消费者线程等待条件变量 fill。打印者线程每秒钟打印一次当前缓冲区的内容。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)