pthread使用實例

在cocos2d-x裏面用多線程還是乖乖的用pthread吧。應用一啓動時創建線程作爲邏輯線程。

創建方法如下:

extern ResourcePool logicThreadResource;
void *test(void *param){
    while (true) {
        if (logicThreadResource.peekNextDataLength()) {
            printf("有數據需要處理");
            int datasize = 0;
            char *receiveData = new char[512];
            logicThreadResource.popData(receiveData, datasize, 512);
            
            int sigal = *((int32_t *) receiveData);
            printf("the sigal is %d\n", sigal);
            if (sigal == 100) {
                CalWinRate(receiveData+4);
            }
            delete[] receiveData;
            
        }else{
            pthread_mutex_lock(&locklogicthread);
            pthread_cond_wait(&cond, &locklogicthread);
            pthread_mutex_unlock(&locklogicthread);
        }
    }
    
    return NULL;
}

void create_pthread(){
    //    啓動兩個測試的主線程
    pthread_t tid;
    pthread_create(&tid, NULL, test, NULL);
}
pthread_create原型爲:PTW32_DLLPORT int PTW32_CDECL pthread_create(pthread_t *tid, const pthread_attr_t *attr, void*(*start)(void *), void *arg)。create有四個參數,第一個是pthread_t,第二個是創建線程的參數,第三個是線程的入口函數,第四個爲入口函數參數。這裏需要注意的是設置pthread_cond_wait即喚醒函數的時候一定要加鎖。啓動函數如下:

bool ResourcePool::pushData(const char *data, int size){
    AutoReleaseLock myLock = AutoReleaseLock();

//    deal
    int realSize = size + sizeof(int32_t);
    int needMemory = bufferLength + realSize;
    if (needMemory > bufferCapacity) {
//        需要重新分配內存
        if (reMollocMemory(needMemory)) {
        }else{
            return false;
        }
    }
//
    char *pWriteBuff = resPool + bufferLength;
    
    *((int32_t *)pWriteBuff) = size;
    
    pWriteBuff += sizeof(int32_t);
    bufferLength += sizeof(int32_t);
    
    memmove(pWriteBuff, data, size);
    bufferLength += size;
    
//    緩衝區有數據的話就激活線程
    pthread_cond_signal(&cond);
    
    return true;
}

思路還是採用的是我上篇博客的思路,至於cocos2d-x裏面的調用我是這樣的:

local handler
local scheduler = CCDirector:sharedDirector():getScheduler()
handler = scheduler:scheduleScriptFunc(function()
                                -- 每幀檢測一下數據,看看是否是合理的
                                BookUtils:parsePackageToLua()
                                if GLOABLEPACKAGE ~= "" then
                                    -- 解析數據
                                    local package = json.decode(GLOABLEPACKAGE)
                                    print("the GLOABLEPACKAGE is " .. GLOABLEPACKAGE)
                                    GLOABLEPACKAGE = ""

                                    local dataTable = {}
                                    table.walk(package, function(val, key)
                                            if tonumber(key) ~= 1 then
                                                dataTable[#dataTable+1] = val   
                                            end
                                        end)
                                    
                                    if tonumber(package[1]) == 100 then
                                        -- 勝率計算器,更新勝率
                                        CCNotificationCenter:sharedNotificationCenter():postNotification("CAL_WIN_RATE_UPDATE", CCString:create(json.encode(dataTable)))
                                    elseif tonumber(package[1]) == 101 then
                                        -- 勝率計算完畢
                                        CCNotificationCenter:sharedNotificationCenter():postNotification("CAL_WIN_RATE_UPDATE_END")
                                    end

                                end
                                
                            end, 0, false)

即主線程每一幀去檢測緩衝區,如果有數據,則發送消息。
發佈了27 篇原創文章 · 獲贊 0 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章