c++ pthread一个小技巧

先上代码:

    void* __thread_new_proc(void *p)
{
    ((GameThread *)p)->run();
    return 0;
}

GameThread::GameThread()
{
    m_bStop = false;
}
GameThread::~GameThread()
{

}
int GameThread::start()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    int ret = pthread_create(&m_thread, &attr, __thread_new_proc, this);
    pthread_attr_destroy(&attr);
    return ret;
}
int GameThread::stop()
{
    //int ret = pthread_kill(m_thread, SIGINT);
    int ret = pthread_cancel(m_thread);
    return ret;
}
int GameThread::join()
{
    int ret = pthread_join(m_thread, NULL);
    return ret;
}
void GameThread::run()
{
    while (m_bStop == false)
    {
        GameTask *pTask = THREAD_POOL->getNextTask();
        if ( pTask )
        {
            pTask->process();
            delete pTask;
            pTask = NULL;
        }
    }
}

 

start() 方法中    int ret = pthread_create(&m_thread, &attr, __thread_new_proc, this);
传入this  然后在 __thread_new_proc方法中 调用run (特别类似java)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。