创建线程
1、Linux线程的创建
pthread_create函数
2、Windows线程的创建
CreateThread函数
3、windows CRT提供的线程创建函数
_beginthreadex函数,其声明位于process.h头文件中
4、C++11提供线程的创建
C++11新标准引入std::thread(头文件)
获取线程ID
1、在Linux系统中有三种方法可以获取1个线程的ID:
方法一:调用pthread_create函数
#include <pthread.h>
pthread tid;
pthread_create(&tid, NULL, thread_proc, NULL);
方法二:调用pthread_self函数
#include <pthread.h>
pthread tid = pthread_self();
方法三:通过系统调用获取线程ID
#include <sys/syscall.h>
#include <unistd.h>
int tid = syscall(SYS_gettid);
2、在windows平台上可以调用GetCurrentThreadID函数获取
3、C++11获取当前线程ID的方法
可以使用std::this_thread类的get_id获取当前线程ID
等待线程结束
1、在Linux下等待线程结束
pthread_join函数
2、在windows下等待线程结束
WaitForSingleObject用于等待1个线