码迷,mamicode.com
首页 > 编程语言 > 详细

c++ pthread一个小技巧

时间:2014-12-24 11:21:08      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

先上代码:

    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)

 

c++ pthread一个小技巧

标签:

原文地址:http://www.cnblogs.com/ziqiongbuxi/p/4181938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!