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)

 

posted on 2014-12-24 10:54  大大世界  阅读(428)  评论(0编辑  收藏  举报

导航