I have introduced multi-thread method in Game, however it is not work wel. Sometimes it may lead to exe crash. Why?
The CPU load implies that it acutal use 2 cores to work. But the data is not update in GET_PLAYER((PlayerTypes)i)
If I write GET_PLAYER((PlayerTypes)i).doTurn() , it will crash.
The CPU load implies that it acutal use 2 cores to work. But the data is not update in GET_PLAYER((PlayerTypes)i)
If I write GET_PLAYER((PlayerTypes)i).doTurn() , it will crash.
C++:
# CvGlobals.cpp
#include <stdio.h>
#include <time.h>
#include <Windows.h>
#include <iostream>
// 线程池数量
HANDLE threadPools[1];
// CVGLOBAL全局数据锁
int* CvGlobalNewThread0_isThisGameTurn = new int[100];
DWORD WINAPI CvGlobals::CvGlobalNewThread0(LPVOID lpParam) {
CvGlobals* pThis = (CvGlobals*)lpParam;
int i;
// 线程执行的代码
// pThis->debug();
while (true) {
// 异步更新AI_updateFoundValues
if (CvGlobalNewThread0_isThisGameTurn[0] == 1) {
// pThis->calculatePIandAdd();
// 1为异步调用
if (GC.CVGLOBAL_MULTITHREAD_AI_FOUNDVALUE == 1) {
for (i = 0; i < MAX_PLAYERS; i++) {
GET_PLAYER((PlayerTypes)i).AI_updateFoundValues();
}
}
// it will crash during the game
// GET_PLAYER((PlayerTypes)i).doTurn();
CvGlobalNewThread0_isThisGameTurn[0] = 0;
}
Sleep(100);
}
}
void CvGlobals::initThread() {
// 多线程开关
if (GC.CVGLOBAL_USE_MULTITHREAD > 0) {
threadPools[0] = CreateThread(NULL, 0, CvGlobalNewThread0, this, 0, NULL);
// threadPools[1] = CreateThread(NULL, 0, CvGlobalNewThread1, this, 0, NULL);
}
}
// 游戏每回合执行的函数
void CvGlobals::doTurn() const {
CvGlobalNewThread0_isThisGameTurn[0] = 1;
CvGlobalNewThread0_isThisGameTurn[1] = 1;
doTurnCore();
}
Last edited: