线程池计算素数:编写一个程序primer_thr3.c,程序运行开始时,创建10个子线程和一个包含20个元素的队列,主线程不断地依次将30000000到30000200之间这200个数放入队列,如果队列满,则主线程等待。
时间: 2024-02-12 21:05:34 浏览: 66
以下是完整的primer_thr3.c程序代码,实现了线程池计算素数的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define QUEUE_SIZE 20
#define THREAD_COUNT 10
typedef struct {
int num;
int is_prime;
} task_t;
int start = 30000000;
int end = 30000200;
int *results;
pthread_mutex_t result_lock = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
task_t *tasks;
int capacity;
int front;
int rear;
pthread_mutex_t lock;
pthread_cond_t not_empty;
pthread_cond_t not_full;
pthread_t *threads;
int thread_count;
int stop;
} thread_pool_t;
void init_thread_pool(thread_pool_t *pool, int capacity, int thread_count);
void *thread_func(void *arg);
int is_prime(int num);
int main() {
results = malloc((end - start + 1) * sizeof(int));
thread_pool_t pool;
init_thread_pool(&pool, QUEUE_SIZE, THREAD_COUNT);
for (int i = start; i <= end; i++) {
task_t task = {i, 0};
pthread_mutex_lock(&pool.lock);
while ((pool.rear + 1) % pool.capacity == pool.front) {
pthread_cond_wait(&pool.not_full, &pool.lock);
}
pool.tasks[pool.rear] = task;
pool.rear = (pool.rear + 1) % pool.capacity;
pthread_cond_signal(&pool.not_empty);
pthread_mutex_unlock(&pool.lock);
}
while (1) {
pthread_mutex_lock(&pool.lock);
if (pool.front == pool.rear) {
pthread_mutex_unlock(&pool.lock);
break;
}
pthread_mutex_unlock(&pool.lock);
}
pool.stop = 1;
pthread_cond_broadcast(&pool.not_empty);
for (int i = 0; i < pool.thread_count; i++) {
pthread_join(pool.threads[i], NULL);
}
pthread_mutex_destroy(&pool.lock);
pthread_cond_destroy(&pool.not_empty);
pthread_cond_destroy(&pool.not_full);
free(pool.tasks);
free(pool.threads);
for (int i = start; i <= end; i++) {
printf("%d %s\n", i, results[i - start] ? "is prime" : "is not prime");
}
free(results);
return 0;
}
void init_thread_pool(thread_pool_t *pool, int capacity, int thread_count) {
pool->tasks = malloc(capacity * sizeof(task_t));
pool->capacity = capacity;
pool->front = 0;
pool->rear = 0;
pthread_mutex_init(&pool->lock, NULL);
pthread_cond_init(&pool->not_empty, NULL);
pthread_cond_init(&pool->not_full, NULL);
pool->threads = malloc(thread_count * sizeof(pthread_t));
pool->thread_count = thread_count;
pool->stop = 0;
for (int i = 0; i < thread_count; i++) {
pthread_create(&pool->threads[i], NULL, thread_func, pool);
}
}
void *thread_func(void *arg) {
thread_pool_t *pool = (thread_pool_t *) arg;
while (1) {
pthread_mutex_lock(&pool->lock);
while (pool->front == pool->rear && !pool->stop) {
pthread_cond_wait(&pool->not_empty, &pool->lock);
}
if (pool->stop) {
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
task_t task = pool->tasks[pool->front];
pool->front = (pool->front + 1) % pool->capacity;
pthread_cond_signal(&pool->not_full);
pthread_mutex_unlock(&pool->lock);
task.is_prime = is_prime(task.num);
pthread_mutex_lock(&result_lock);
results[task.num - start] = task.is_prime;
pthread_mutex_unlock(&result_lock);
}
return NULL;
}
int is_prime(int num) {
if (num < 2) {
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
```
程序首先定义了一个任务结构体task_t,其中包含一个待计算的数值num和一个标志位is_prime,表示该数值是否为素数。然后定义了一个线程池结构体thread_pool_t,包含一个任务队列和一定数量的线程。线程池的初始化函数init_thread_pool中包括创建任务队列、初始化线程和启动线程等步骤。
主函数main中循环向任务队列中添加任务,并通过线程池的线程函数thread_func计算素数并回传计算结果。最后等待所有任务完成后销毁线程池,并通过results数组输出计算结果。
线程函数thread_func中取出任务、计算素数和回传结果等步骤,其中使用了互斥锁和条件变量保证了线程安全。计算素数的函数is_prime使用了较为简单的算法判断是否为素数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)