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.结果

相关推荐
蓁蓁啊2 小时前
GIT使用SSH 多账户配置
运维·git·ssh
路由侠内网穿透4 小时前
本地部署开源持续集成和持续部署系统 Woodpecker CI 并实现外部访问
服务器·网络·windows·ci/cd·开源
程序猿小三4 小时前
Linux下基于关键词文件搜索
linux·运维·服务器
虚拟指尖5 小时前
Ubuntu编译安装COLMAP【实测编译成功】
linux·运维·ubuntu
椎4956 小时前
苍穹外卖前端nginx错误之一解决
运维·前端·nginx
刘某的Cloud6 小时前
parted磁盘管理
linux·运维·系统·parted
啊?啊?6 小时前
4 解锁 Linux 操作新姿势:man、grep、tar ,创建用户及添加权限等 10 大实用命令详解
linux·服务器·实用指令
程序员老舅6 小时前
干货|腾讯 Linux C/C++ 后端开发岗面试
linux·c语言·c++·编程·大厂面试题
极验6 小时前
iPhone17实体卡槽消失?eSIM 普及下的安全挑战与应对
大数据·运维·安全
爱倒腾的老唐6 小时前
24、Linux 路由管理
linux·运维·网络