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

相关推荐
honey ball7 分钟前
R & S的EMI接收机面板
linux·运维·网络
木下-俱欢颜1 小时前
搭建基于chrony+OpenSSL(NTS协议)多层级可信时间同步服务
运维·网络安全·udp·ssl
旧故新长2 小时前
访问 Docker 官方镜像源(包括代理)全部被“重置连接”或超时
运维·docker·容器
GBXLUO2 小时前
如何使用远程桌面控制电脑
服务器
柳如烟@2 小时前
在Rocky Linux 9.5上部署MongoDB 8.0.9:从安装到认证的完整指南
linux·运维·mongodb
搬码临时工3 小时前
电脑怎么远程访问服务器?4种常见的简单方法
运维·服务器·网络·异地访问
QQ2740287563 小时前
Kite AI 自动机器人部署教程
linux·运维·服务器·人工智能·机器人·web3
文牧之3 小时前
PostgreSQL 配置设置函数
运维·数据库·postgresql
.小墨迹3 小时前
Apollo学习——planning模块(3)之planning_base
linux·开发语言·c++·学习·自动驾驶
K龙4 小时前
私有资产测绘&安全流水线Shovel
运维·安全·开发·其它