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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值