一、C线程库
1. 创建线程
int pthread_create(
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *),
void *arg
);
注意:
- 函数原型中第三个参数的类型为函数指针,指向的线程的处理函数,其参数类型为(void *)。
- 若线程函数为类成员函数,则this指针会作为默认的参数被传进函数中,从而和线程函数的参数(void*)不能匹配,不能通过编译。
- 由于类的静态成员函数没有this指针,不会出现问题。
2. 退出线程
- 一般情况下,针对于主线程,如果想要让线程退出,且不影响到其他线程的正常运行,即不释放虚拟地址空间
void pthread_exit(
void *retval
);
3. 回收线程
- 如果子线程在运行,调用该函数的线程就会阻塞,子线程退出后,函数解除阻塞进行资源回收。
- 函数被调用一次,只能回收一个子线程,如果有多个子线程则需要循环进行回收。
int pthread_join(
pthread_t thread,
void **retval
);
4. 线程分离
- 一般情况下,程序中的主线程有其他任务,如果让主线程负责子线程的资源回收, 只要子线程不退出主线程就会一直被阻塞。
- 子线程与主线程分离之后,子线程退出后其占用的内核资源就被系统的其他进程接管并回收了。
int pthread_detach(pthread_t thread);
二、实现代码
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <list>
#include <cstdio>
#include <exception>
#include <pthread.h>
#include "../lock/locker.h"
#include "../CGImysql/sql_connection_pool.h"
template <typename T>
class threadpool
{
public:
threadpool(int actor_model, connection_pool *connPool, int thread_number = 8, int max_request = 10000);
~threadpool();
bool append(T *request, int state