#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
using namespace std;
pthread_t ntid;
bool shutdown = true;
void
printids(const char * s){
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n",s,(unsigned long) pid,(unsigned long)tid,(unsigned long)tid);
}
void *
thr_fn(void * arg){
while(shutdown){
printids("new thread:");
cout << "in this pthread,transmit data." << endl;
}
return ((void *)0);
}
int
main() {
int err;
err = pthread_create(&ntid,NULL,thr_fn,NULL);
if(err != 0){
cout << "cant't create thread." << endl;
}
printids("main thread:");
sleep(10);
shutdown = false;
cout << "main thread ends here." << endl;
exit(0);
}
cmake_minimum_required(VERSION 3.10)
project(C++test)
set(CMAKE_CXX_STANDARD 11)
add_executable(C++test
main.cpp)
TARGET_LINK_LIBRARIES( C++test
pthread
)