首页 > 编程语言 > 详细

c++ pthread一个小技巧

时间:2014-12-24 11:20:55      阅读:343      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!