Sentinal
2022-05-26 12:37
采纳率: 100%
浏览 115

c++ 11并行运算结果不对问题,求解答

使用下述代码判断矩阵的性质,对下述矩阵中每个为BLANK的位置依次填入g_padVal中的值,从而寻找是否有某个矩阵满足对应的性质。

正常情况应该验证5^9 = 1953215次。但多线程场景下运行次数异常(每次运行后显示的执行次数不太一样)。修改为单线程执行结果就是好的。麻烦哪位帮忙指点下

int g_matrix[RANK_NUM][COL_NUM] = {
{ 0, 1, -1, 2, -2},
{ 1, 1, 1, 0, blank},
{ -1, 1, -1, bank, 0},
{ 2, 0, bank, bank, NK},
{ -2, BNK, BAN, ANK, K}
};
//原始矩阵,blank为待填入值的位置

int g_padVal[VAL_NUM] = {0, 1, -1, 2, -2};
//待填入的值范围

//uncertainValPosVec为待填入元素的下标列表,Pos的定义为行下标和列下标的结构体
void PadMatrixValMutiThread(int oriMatrix[RANK_NUM][COL_NUM], vector uncertainValPosVec)
{
vector<future> threadPool;
Pos firstPos = uncertainValPosVec[0];
uncertainValPosVec.erase(uncertainValPosVec.begin());
Pos secondPos = uncertainValPosVec[0];
uncertainValPosVec.erase(uncertainValPosVec.begin());
cout << "PadMatrixValMutiThread uncertainValPosVec size = " << uncertainValPosVec.size() << endl;
int num = 0;
while (true)
{
for(; num < VAL_NUM * VAL_NUM; num++)
{
int matrix[RANK_NUM][COL_NUM];
memcpy(matrix, oriMatrix, sizeof(int) * RANK_NUM * COL_NUM);
matrix[firstPos.rank][firstPos.col] = g_padVal[num / VAL_NUM];
matrix[secondPos.rank][secondPos.col] = g_padVal[num % VAL_NUM];
vector tmp = uncertainValPosVec;
//PadMatrixValByRule(matrix, (void *)(&tmp));
ThreadLoad(threadPool, PadMatrixValByRule, matrix, (void *)(&tmp)); //此处拉起多线程,使用单线程时正常
}
if (IsThreadFinish(threadPool) && (num == VAL_NUM * VAL_NUM))
{
break;
}
}
}

void PadMatrixValByRule(void *matrix, void *pUncertainValPosVec)
{
CHECK_RET_RETURN_NULL(matrix == NULL)
CHECK_RET_RETURN_NULL(pUncertainValPosVec == NULL)
int oriMatrix[RANK_NUM][COL_NUM] = {{0}};
memcpy(oriMatrix, matrix, sizeof(int) * RANK_NUM * COL_NUM);
vector uncertainValPosVec = *(vector *)(pUncertainValPosVec);
//cout << "PadMatrixValByRule uncertainValPosVec = " << uncertainValPosVec.size() << endl;
if(uncertainValPosVec.empty())
{
BOOLEAN sentinal = IsMatrixAssociation(oriMatrix);
if(TRUE == sentinal)
{
cout << "find one matrix = " << endl;
PrintMatrix(oriMatrix);
}
}
else
{
Pos pos;
pos.rank = uncertainValPosVec[0].rank;
pos.col = uncertainValPosVec[0].col;
uncertainValPosVec.erase(uncertainValPosVec.begin());
int matrix[RANK_NUM][COL_NUM];
memcpy(matrix, oriMatrix, sizeof(int) * RANK_NUM * COL_NUM);
for(int i = 0; i < VAL_NUM; i++)
{
matrix[pos.rank][pos.col] = g_padVal[i];
PadValFuncInputStr subInput;
PadMatrixValByRule(matrix, &uncertainValPosVec); //此处使用递归,留意
}
}
}

BOOLEAN IsMatrixAssociation(int matrix[RANK_NUM][COL_NUM])
{
g_mutex.lock();
g_countNum++;
if(0 == g_countNum % 10000)
{
cout << "g_countNum = " << g_countNum << endl;
}
g_mutex.unlock();

return TRUE;

}

void ThreadLoad(vector<future>& threadPool, ThreadFuncType func, void* matrix, void* vec)
{
while (true)
{
if (threadPool.size() < MAX_THREAD_SIZE)
{
future tempFuture = async(launch::async, func, matrix, vec);
threadPool.push_back(move(tempFuture));
break;
}
else
{
vector<future>::iterator iterThreadPool = threadPool.begin();
for (; iterThreadPool != threadPool.end(); ++iterThreadPool)
{
if (future_status::ready == (*iterThreadPool).wait_for(milliseconds(0)))
{
threadPool.erase(iterThreadPool);
break;
}
}
}
}
}

bool IsThreadFinish(vector<future>& threadPool)
{
vector<future>::iterator iterThreadPool = threadPool.begin();
for (; iterThreadPool != threadPool.end(); iterThreadPool++)
{
if (future_status::ready == (*iterThreadPool).wait_for(milliseconds(0)))
{
threadPool.erase(iterThreadPool);
break;
}
}
if (0 == threadPool.size())
{
return true;
}
return false;
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题