Pthreads程序实现任务队列

news/2024/7/5 7:08:02

主线程启动用户指定数量的线程,这些线程进入条件等待状态。
主线程生成一些任务(一定计算量),每生成一个新的任务,就用条件变量唤醒一个线程,当这个唤醒线程执行完任务时,回到条件等待状态。
当主线程生成完所有任务,设置全局变量表示再没有要生成的任务了,并用一个广播唤醒所有线程。为了清晰起见,建议任务采用链表操作。

#include <iostream>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
const int N=1e4,N1=1e6;
struct Node {
    int id;
    int a[N1],b[N1];
    Node* next;
};
struct Queue {
    Node* front;
    Node* tail;
};
int InitQueue(Queue* T);
int Add(Queue* T, int e);
int DoTask(Queue* T, int* ep);
int thread_count, finished = 0;
pthread_mutex_t mutex,actprint;
pthread_cond_t cond;
void* task(void* rank);
Queue Q;
int main(int argc, char* argv[])
{   
    srand(0);
    InitQueue(&Q);
    pthread_t *thread_handles;
    thread_count = strtol(argv[1], NULL, 10);//从命令行读取线程数
    thread_handles = new pthread_t[thread_count];//分配长度

    pthread_mutex_init(&mutex, NULL);
    pthread_mutex_init(&actprint, NULL);
    pthread_cond_init(&cond, NULL);

    int n;
    printf("Please input the task number:");
    cin>>n;

    for (int i = 0; i < thread_count; i++)
        pthread_create(&thread_handles[i], NULL, task, (void*)i);

    for (int i = 0; i < n; i++) {
        pthread_mutex_lock(&mutex);
        Add(&Q, i);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    finished = 1;
    pthread_cond_broadcast(&cond);
    for (int i = 0; i < thread_count; i++)
        pthread_join(thread_handles[i], NULL);
    pthread_mutex_destroy(&mutex);
    pthread_mutex_destroy(&actprint);
    pthread_cond_destroy(&cond);
    free(thread_handles);
    return 0;
}
int InitQueue(Queue* T) {
    T->front = (Node*)malloc(sizeof(Node));
    T->tail = T->front;
    T->front->id = 0;
    T->front->next = NULL;
    return 0;
}
int Add(Queue* T, int e) {
    Node* newnode = (Node*)malloc(sizeof(Node));
    newnode->id = e;
    newnode->next = 0;
    int t,k=0;
    for(int i=0;i<N1;i++){
        t=rand()%N;
        newnode->a[k++]=t;
    }
    k=0;
    for(int i=0;i<N;i++){
        t=rand()%N;
        newnode->b[k++]=t;
    }
    T->tail->next = newnode;
    T->tail = newnode;
    return 0;
}
int DoTask(Queue* T, int* e) {
    Node* dest;
    if (T->tail == T->front)return 0;//队列中已经没有任务
    dest = T->front->next;//从头取出一个任务
    if (dest == 0) return 0;
    for(int i=0;i<N1;i++){//做任务
        dest->a[i]=dest->a[i]*dest->b[i];
    }
    *e = dest->id;
    T->front->next = dest->next;
    free(dest);
    return 0;
}
void* task(void* rank) {
    long long my_rank = (long long)rank;
    pthread_mutex_lock(&actprint);
    cout<<"process "<<my_rank<<" has been activated"<<endl;
    pthread_mutex_unlock(&actprint);
    int my_task;
    Node** p = &(Q.front->next);
    while (1) {
       pthread_mutex_lock(&mutex);
        if (finished) {
            if (*p == NULL) {
                pthread_mutex_unlock(&mutex);
                break;
            }
            DoTask(&Q, &my_task);
            pthread_mutex_unlock(&mutex);
            printf("Thread %ld: Task no.%d\n", my_rank, my_task);
        }else {
            while(pthread_cond_wait(&cond, &mutex)!=0);
            DoTask(&Q, &my_task);
            pthread_mutex_unlock(&mutex);
            printf("Thread %ld: Task no.%d\n", my_rank, my_task);
        }
    }
}

参考资料
Pthreads实现任务队列


http://lihuaxi.xjx100.cn/news/1359563.html

相关文章

RocketMQ环境搭建

环境搭建 环境准备 下载地址: https://downloads.apache.org/rocketmq/4.9.5/安装 上传至服务器 mkdir /usr/soft #上传至此目录/usr/softmkdir /usr/soft 解压 cd /usr/soft unzip rocketmq-all-4.9.5-bin-release.zip移动 mkdir /usr/local/rocketmq cd /usr/soft mv r…

瀚高数据库安全版V4.5流复制安装手册(Linux)

目录 瀚高数据库安全版V4.5流复制安装手册&#xff08;Linux&#xff09; 1. 软件安装 2. 主节点配置 2.1修改参数并创建复制槽 3. 备节点配置 瀚高数据库安全版V4.5流复制安装手册&#xff08;Linux&#xff09; 1. 软件安装 &#xff08;1&#xff09;【数据库软件安装…

Linux软件包管理工具APT

目录 前言 1、更新软件包列表 2、升级已安装的软件包 3、安装软件包 4、删除软件包 5、列出已安装的软件包 6、显示软件包信息 7、搜索软件包 8、清理无用的软件包 前言 Linux系统中&#xff0c;常用的软件包管理工具之一是apt&#xff08;Advanced Package Tool&…

5 | Pandas日期操作教程

文章目录 Pandas日期操作教程1. 导入 Pandas 和准备数据2. 将日期作为索引3. 重采样和时间窗口重采样时间窗口4. 日期偏移5. 时间差计算6. `dt`属性提取日期时间的各个组成部分计算时间差Pandas日期操作教程 在数据分析和数据科学的领域中,经常会遇到时间序列数据。Pandas是P…

java仿拼多多+免费商城搭建+免费小程序商城搭建+手机商城免费搭建+限时秒杀+积分商城

1. 涉及平台 平台管理、商家端&#xff08;PC端、手机端&#xff09;、买家平台&#xff08;H5/公众号、小程序、APP端&#xff08;IOS/Android&#xff09;、微服务平台&#xff08;业务服务&#xff09; 2. 核心架构 Spring Cloud、Spring Boot、Mybatis、Redis 3. 前端框…

MySQL 导入 SQL 时报错,1067 – Invalid default value for ‘字段名

链接&#xff1a;(144条消息) MySQL 导入 SQL 时报错&#xff0c;1067 – Invalid default value for ‘字段名’。_花落十里不如你的博客-CSDN博客

macboock Air mac系统phpstudy安装php8操作

现在mac的phpstudy的php版本只能到7.3&#xff0c;而有的网站需要8.0以上的版本&#xff0c;那就要手动进行安装 安装后的效果 操作一 安装php8.2的版本 brew install php8.2完成后的地址 /usr/local/Cellar/php/8.2.8 进入/usr/local/Cellar/php/&#xff0c;复制整个文件…

关于 PostgreSQL 删除数据库 - 命令行删除,报错数据库不存在,pgadmin 报错存在会话链接 导致无法删除数据库问题

序言 测试环境&#xff1a; Windows 10问题 笔者尝试过在 cmd 命令行&#xff0c;使用PostgreSQL 的 psql 工具登录 postgresql&#xff0c;删除某个有问题的数据库&#xff0c;准备新建重载该数据库时&#xff0c;发现 DROP DATABASE database_name &#xff0c;竟然报错该…