CmakeList.txt之Linux-pthread

1.cmakelist.txt

复制代码
cmake_minimum_required(VERSION 3.16)
​
project(pthread_linux_test LANGUAGES C)
​
# 查找Threads库(包含pthread支持)
find_package(Threads REQUIRED)
if (Threads_FOUND)
    message(STATUS "成功找到Threads库")
endif()
​
add_executable(pthread_linux_test main.c
    threadpool.c threadpool.h
)
# 链接Threads库到可执行文件
target_link_libraries(pthread_linux_test Threads::Threads)
include(GNUInstallDirs)
install(TARGETS pthread_linux_test
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
复制代码

2.测试代码

复制代码
#include <stdio.h>
#include "threadpool.h"
void taskfunc(void* arg) {
    int num = *(int*)arg;
    printf("thread %ld is working, number=%d\n", pthread_self(), num);
    sleep(1);
}
​
int main()
{
    //创建线程池
    ThreadPool* pool = threadPoolCreate(3, 10, 100);
    for (int i = 0; i < 100; i++) {
        int* num = (int*)malloc(sizeof(int));
        *num = i + 100;
        threadPoolAdd(pool, taskfunc, num);
    }
    sleep(30);
    threadPoolDestroy(pool);
​
    printf("Hello World!\n");
    return 0;
}
复制代码

3.结果

相关推荐
稳联技术老娜3 小时前
DeviceNet主站怎么连接西门子PLC,Profinet网关配置手册(那智机器人)
服务器·网络·数据库
三十..4 小时前
Ceph 三大存储接口深度实践与数据保护指南
运维·ceph
9分钟带帽4 小时前
linux_系统开机自动执行shell脚本
linux·服务器
蝶豆花5 小时前
基于商城系统的功能,自动化,性能-测试报告
运维·自动化
袋鼠云数栈5 小时前
从前端到基础设施,ACOS 如何打通企业全链路可观测
运维·前端·人工智能·数据治理·数据智能
黎阳之光5 小时前
视频孪生智护供水生命线:黎阳之光赋能医疗与园区水务高质量升级
运维·物联网·算法·安全·数字孪生
消失在人海中5 小时前
oracle 数据库多表关联查询
服务器·数据库·oracle
志栋智能5 小时前
AI驱动无代码:降低巡检超自动化的门槛
大数据·运维·网络·人工智能·自动化
嵌入式小能手6 小时前
飞凌嵌入式ElfBoard-进程间的通信之命名管道
linux·服务器·算法
AOwhisky6 小时前
Ceph系列第六期:Ceph 文件系统(CephFS)精讲
linux·运维·网络·笔记·ceph