1.线程的概念
线程就是一个正在运行的函数。
posix线程是一套标准,而不是实现。
openmp线程。
线程标识:pthread_t (linux环境下是整形数)
就像每个进程有一个进程ID一样,每个线程也有一个线程ID。进程ID在整个系统中是唯一的,但线程ID不同,线程ID只有在它所属的进程上下文中才有意义。
进程ID是用pid_t数据类型来表示的,是一个非负整数。线程ID是用pthread_t 数据类型来表示的,实现的时候可以用一个结构来代表pthread_t数据类型,所以可移植的操作系统实现不能把它作为整数处理。
处理器也是以线程为调度单位。
线程实际是以进程标识来描述的。
pthread_equal() 比较线程标识;
pthread_self(void) 返回当前线程标识。
2.线程的创建/终止/取消,栈的清理
2.1线程创建
pthread_create();
NAME
pthread_create - create a new thread
LIBRARY
POSIX threads library (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void *),
void *restrict arg);
传入的参数可以做成结构体,传给arg。
程序实例
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>
using namespace std;
static void* func(void* p) {
cout << "thread is working" << endl;
return 0;
}
int main() {
cout << "begin" <<endl;
pthread_t tid;
int err;
err = pthread_create(&tid, NULL, func, NULL);
if (err) {
cerr << "thread create:" << strerror(err) << endl;
exit(0);
}
cout << "end" <<endl;
exit(0);
}
[root@iZ2zeirmyddvdjh8g2zyxzZ pthread]# ./create1
begin
end
没有出现thread is working
这种现象是因为:线程的调度取决于调度器的调度策略。
2.2线程终止
3种方式:
1)线程从启动例程中返回,返回值就是线程的退出码;
2)线程可以被同一进程中的其他线程取消;
3)线程调用pthread_exit()
函数。
pthread_exit()
NAME
pthread_exit - terminate calling thread
LIBRARY
POSIX threads library (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
[[noreturn]] void pthread_exit(void *retval);
pthread_join() ----> wait相当于收尸
NAME
pthread_join - join with a terminated thread
LIBRARY
POSIX threads library (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
程序实例,补全刚才程序
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>
using namespace std;
static void* func(void* p) {
cout << "thread is working" << endl;
pthread_exit(NULL);
return 0;
}
int main() {
cout << "begin" <<endl;
pthread_t tid;
int err;
err = pthread_create(&tid, NULL, func, NULL);
if (err) {
cerr << "thread create:" << strerror(err) << endl;
exit(0);
}
//等线程运行完等待收尸
pthread_join(tid, NULL);
cout << "endl" <<endl;
exit(0);
}
[root@iZ2zeirmyddvdjh8g2zyxzZ pthread]# ./create1
begin
thread is working
end
2.3 栈的清理
线程可以安排它退出时需要调用的函数,这与进程在退出时可以用atexit函数安排退出是类似的。这样的函数称为线程清理处理程序(thread cleanup handler)。一个线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说,它们的执行顺序与它们注册时相反。
NAME
pthread_cleanup_push, pthread_cleanup_pop - push and pop thread cancelation clean-up handlers
LIBRARY
POSIX threads library (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
void pthread_cleanup_push(void (*routine)(void *), void *arg);
void pthread_cleanup_pop(int execute);
钩子函数代码实例
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>
using namespace std;
static void cleanup_function(void* arg) {
cout << (const char*)arg << endl;
}
static void* func(void* p) {
cout << "thread is working" << endl;
string s1 = "haha1";
string s2 = "haha2";
// 使用 std::string 的 c_str() 方法来获取 const char* 类型的指针
pthread_cleanup_push(cleanup_function, (void*)s1.c_str());
pthread_cleanup_push(cleanup_function, (void*)s2.c_str());
pthread_exit(NULL);
//必须成对出现
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
return 0;
}
int main() {
cout << "begin" <<endl;
pthread_t tid;
int err;
err = pthread_create(&tid, NULL, func, NULL);
if (err) {
cerr << "thread create:" << strerror(err) << endl;
exit(0);
}
pthread_join(tid, NULL);
cout << "end" <<endl;
exit(0);
}
[root@iZ2zeirmyddvdjh8g2zyxzZ cleanup]# ./create1
begin
thread is working
haha2
haha1
end