添加如下一行
具体的例子
cmake_minimum_required(VERSION 3.17)
project(mutex_learn)set(CMAKE_CXX_STANDARD 14)set(BOOST_ROOT "/usr/local/include/boost")
#添加头文件搜索路径
include_directories(/usr/local/include)
#添加库文件搜索路径
link_directories(/usr/local/lib)add_executable(mutex_learn main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lrt")target_link_libraries(mutex_learn boost_thread boost_system rt)
完整代码
#include <sys/shm.h>
#include <iostream>
#include <unistd.h>
#include <pthread.h>void * child1(void *arg){pthread_t tid = pthread_self();printf("1 thread %lu \n",tid);}int main(int argc,char* argv[]) {int result{};pthread_t a_thread{};pthread_t tid = pthread_self();printf("mean thread %lu \n",tid);result = pthread_create(&a_thread, nullptr,child1, nullptr);if (result !=0){printf("Thread create failed!");}return 0;
}