c++多线程调用函数问题
C++ multiple threads calling function issue
这段代码需要一些帮助。我创建了一个函数来在我的应用程序中打印一些调试文本("debug"函数)。2个线程(FD_READ和incDataProcess)正在使用这个函数在调试窗口内打印一些信息。当我运行以下代码时,数据显示不好,可能是因为2个线程同时访问函数(数据竞争)。当我尝试使用互斥锁一次限制一个线程对函数的访问时,程序会挂起。我不明白为什么当我用互斥锁保护函数时程序会挂起。我的互斥锁怎么了?
static mutex printMutex;
static mutex readQueueMutex;
//....
void debug(LPSTR text1, LPSTR text2, LPSTR text3)
{
// printMutex.lock(); // the combination of lock - unlock
// makes the program hang (solution 1)
// lock_guard<mutex>_(printMutex); // Solution 2, the program
// hangs also
char *Text1 = text1;
char *Text2 = text2;
char *Text3 = text3;
char Text[2048];
strcpy_s(Text, Text1);
strcat_s(Text, Text2);
strcat_s(Text, Text3);
int TextLen = SendMessage(hDebug, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hDebug, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
SendMessage(hDebug, EM_REPLACESEL, FALSE, (LPARAM)Text);
// printMutex.unlock(); // See comment solution 1
}
//.....
void incDataProcess(void *pBuffer)
{
debug("incDataThread launchedn", "", "");
for(;;)
{
while (!readQueue.empty())
{
readQueueMutex.lock();
string sReadBuffer = readQueue.front();
readQueue.pop();
readQueueMutex.unlock();
string var;
char *pVar = nullptr;
char *next_token = nullptr;
istringstream iss(sReadBuffer); // Put into a stream
while (getline(iss, var)) // Default delimiter 'n'
{
pVar = _strdup(var.c_str()); // Cast string to char *
if( strstr(pVar, "Qh57=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pFoFd = strtok_s(NULL, "n", &next_token);
strcpy_s(foFd, pFoFd);
debug("Qh57=", foFd, "n");
}
} //while getline
} // while queue is not empty
} // infinite for loop
}
//.....
case FD_READ:
{
debug("FD_READ eventn", "", "");
int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0);
readQueueMutex.lock();
readQueue.push(readBuffer);
readQueueMutex.unlock();
char bytes[256];
_itoa_s(bytes_recv, bytes, 10);
debug("Received ", bytes, " bytes from servern")
debug("End of FD_READn", "", "");
}
break;
下面是没有同步的调试窗口:
FD_READ event
Received 8 bytes from server
End of FD_READ
Qh57=0
FD_READ eventReceived 49 bytes from serverEnd of FD_READ // Display problem
// when second
// thread calls the function
FD_READ event
Received 127 bytes from server
End of FD_READ
FD_READ event
Received 57 bytes from server
End of FD_READ
Qh57=1
Brandon Kohn的答案是你陷入僵局的原因。作为解决方案,我建议您做以下事情:
static std::queue<std::string> pendingDebugMessages;
void debug(LPSTR text1, LPSTR text2, LPSTR text3)
{
lock_guard<mutex>_(printMutex);
pendingDebugMessages.push(std::string());
std::string& finalString = pendingDebugMessages.back();
finalString = text1;
finalString += text2;
finalString += text3;
}
// Call this regularly from your message pump thread
void PrintPendingMessages()
{
lock_guard<mutex>_(printMutex);
while(!pendingDebugMessages.empty())
{
int TextLen = SendMessage(hDebug, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hDebug, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
SendMessage(hDebug, EM_REPLACESEL, FALSE, (LPARAM)pendingDebugMessages.front().c_str());
pendingDebugMessages.pop();
}
}
SendMessage阻塞,直到窗口处理消息。如果该窗口是在另一个也调用debug的线程上创建的…僵局。
相关文章:
- Visual Studio中的函数声明和函数定义问题
- C++quit()函数中可能存在作用域问题
- 类似于strcat()的函数出现问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 类C++中的函数问题(LNK2019和LNK1120错误)
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 关于简单C++函数(is_palindrome)的逻辑的问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- constexpr构造函数需要常量成员函数时出现问题
- 添加存储在向量中的大整数的函数出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 关于 c++ 函数中指针赋值的简单问题
- 如何在标头中声明(或定义)函数的问题
- 有关 c++ 构造函数的问题
- 模板类转换问题 - 无法推断调用的函数
- 从 C++ 中的函数返回数组地址问题
- 关于复制构造函数的一个棘手问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题