linux下 多线程的使用

因为最近在看slam的程序,在里面发现了<thread>,所以想了解下多线程,本以为跑通一个demo 很简单的,但是实际的操作还是有点小坑的。
最初是希望使用thread,因为网上资料显示它是对pthread做了封装的一个库,对于我仅仅简单的使用下当然是最好了,但是后来搜索了一圈thread的资料好少。只好先上pthread。
在QT环境下只需要添加上它的库就好了,LIBS += -lpthread
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#define NUM 10
int count;

void* thread_func(void *arg)
{
    count++;
    printf("count %d\n", count);
//    return;
}

int main()
{
    pthread_t tid[NUM];
    int i = 0;

    for (i = 0; i < NUM; i++)
    {
        pthread_create(&tid[i], NULL, thread_func, NULL);
    }

    sleep(1);

    return 0;
}

如果希望直接使用gcc编译也可以,但是在终端编译的时候要加上-lpthread.

gcc Test.cpp -o test -lpthread

最后我希望在cmake 中使用它,所以必须在CMakeLists.txt中加入如下的库:

cmake_minimum_required(VERSION 2.8)
project(thread_test)

add_definitions(-std=c++11)
set (CMAKE_CXX_STANDARD 11)

add_executable(thread Test.cpp)
target_link_libraries(thread pthread
)

这样pthread就可以在多个编译器下进行编译了。
但是我看的ORB_SLAM2里面直接引用的就是#include ,很蛋疼,看它的cmakelists.txt也没看出什么问题。只能再查下资料了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

城墙郭外斜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值