【c++】c++的一些技术操作

  1. pthread_create 的第三个参数,为函数指针,指向处理线程函数的地址。该函数,要求为静态函数。如果处理线程函数为类成员函数时,需要将其设置为静态成员函数。
  2. C++静态成员函数访问非静态成员
    https://blog.csdn.net/yueguangmuyu/article/details/118390764
  3. C++静态成员函数访问非静态函数
    函数定义
private:
    /*工作线程运行的函数,它不断从工作队列中取出任务并执行之*/
    static void *worker(void *arg);
    void run();

调用

pthread_create(m_threads + i, NULL, worker, this)

通过void *arg指针接受任何类型的指针,再使用强制类型转换运算符创建一个新的实例

void *threadpool<T>::worker(void *arg)
{
    
    
    threadpool *pool = (threadpool *)arg;
    pool->run();
    return pool;
}

https://github1s.com/qinguoyi/TinyWebServer/blob/HEAD/threadpool/threadpool.h#L93

猜你喜欢

转载自blog.csdn.net/weixin_50862344/article/details/131625497