[cocos2d-x]CCHttpClient的一个bug

    公司的新游戏《我是大官人》马上就要大规模PR了,一切都已经准备就绪,这时测试部门却反馈了一个小问题,打开游戏的时候,偶尔会卡在启动界面,提示:正在连接服务器...然后就没反应了,这个问题发生的概率很低,大概3%左右,而且退出重新打开游戏就好了,“应该是网络不好造成的”,大家并没有太重视这个bug,但是老板不放心,“就算是网络问题,也不应该卡住,如果是新玩家碰到这种情况就直接流失了,这个问题得查一下。” 看来这不是一个小问题,于是这个bug分配给了我。

    花了一些时间重现,我发现问题出在CCHttpClient,熟悉cocos2d-x的同学知道,这是一个异步的网络库,它的工作原理是这样的:HttpClient内部有一个工作线程,游戏主线程调用send()函数,将http请求压入一个队列,然后唤醒工作线程就返回了。工作线程被唤醒后,将http请求从队列中取出,再调用libcurl进行处理,然后再通过回调将结果返回主线程。bug重现的时候,主线程注册的回调没有触发,游戏就卡住了。我估计是libcurl将工作线程卡住了,于是在工作线程中加入了一些打印进行验证,结果却大意料:工作线程根本就没有触发!

    又是一个多线程同步的问题!这是工作线程的代码片段:

// Worker thread
static void* networkThread(void *data)
{    
    CCHttpRequest *request = NULL;
    
    while (true) 
    {
        if (need_quit)
        {
            break;
        }
        
        // step 1: send http request if the requestQueue isn't empty
        request = NULL;
        
        pthread_mutex_lock(&s_requestQueueMutex); //Get request task from queue
        if (0 != s_requestQueue->count())
        {
            request = dynamic_cast<CCHttpRequest*>(s_requestQueue->objectAtIndex(0));
            s_requestQueue->removeObjectAtIndex(0);  
            // request's refcount = 1 here
        }
        pthread_mutex_unlock(&s_requestQueueMutex);
        
        if (NULL == request)
        {
        	// Wait for http request tasks from main thread
        	pthread_cond_wait(&s_SleepCondition, &s_SleepMutex);
            continue;
        }
        
        // step 2: libcurl sync access

工作线程空闲的时候,通过pthread_cond_wait()挂起

send()函数的代码:

//Add a get task to queue
void CCHttpClient::send(CCHttpRequest* request)
{    
    if (false == lazyInitThreadSemphore()) 
    {
        return;
    }
    
    if (!request)
    {
        return;
    }
        
    ++s_asyncRequestCount;
    
    request->retain();
        
    pthread_mutex_lock(&s_requestQueueMutex);
    s_requestQueue->addObject(request);
    pthread_mutex_unlock(&s_requestQueueMutex);
    
    // Notify thread start to work
    pthread_cond_signal(&s_SleepCondition);
}

我仔细的看了一遍,发现这句话很奇怪:

pthread_cond_wait(&s_SleepCondition, &s_SleepMutex);

这里用了一个互斥锁s_SleepMutex,而这个锁没有其他代码使用。这很可能是一个错误:一个互斥锁如果只有一处代码使用,那么只有一种可能,这段代码会在多个线程中执行。而httpClient的线程显然只有一个。google了一番,果然是这个互斥锁用错了。大家可以参考知乎上的一篇文章:http://www.zhihu.com/question/24116967 

里面详细解释了为什么没有正确的加锁会导致信号量丢失。


这里是修改后的代码,不再使用单独的s_SleepMutex, 用requestQueueMutex代替

// Worker thread
static void* networkThread(void *data)
{    
    CCHttpRequest *request = NULL;
    
    while (true) 
    {
        if (need_quit)
        {
            break;
        }
        
        // step 1: send http request if the requestQueue isn't empty
        request = NULL;
        pthread_mutex_lock(&s_requestQueueMutex); //Get request task from queue
        while (0 == s_requestQueue->count()) {
            pthread_cond_wait(&s_SleepCondition, &s_requestQueueMutex);
        }
        request = dynamic_cast<CCHttpRequest*>(s_requestQueue->objectAtIndex(0));
        s_requestQueue->removeObjectAtIndex(0);
                // request's refcount = 1 here
            
        CCLog("s_SleepCondition notified");
        pthread_mutex_unlock(&s_requestQueueMutex);
        

反复测试了一个下午,这个bug没有再复现了。


这是在cocos2d-x 2.1.4版本上改的,cocos2d-x 3.x已经不存在这个问题了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值