【发布时间】:2014-01-24 09:36:04
【问题描述】:
我有一个使用 TIdTCPServer 的服务器程序和一个客户端程序。 我在一台计算机上运行我的客户端程序,例如 3 次。每次连接客户端时,我都会尝试在备忘录中添加一些东西。这是问题所在。由于 3 个客户端同时运行并尝试连接到服务器,因此当我运行我的服务器应用程序时。两个客户端同时连接,并且由于 TIdTCPServer 在单独的线程上处理客户端连接,它会导致死锁(或类似的东西)。我尝试使用互斥锁
// Creation of mutex.Inside the constructor of TCPConnection class
ListViewMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
//我的代码中的其他地方
void __fastcall TCPConnection::OnConnect(TIdContext *AContext)
{
DWORD dwWaitResult;
// Request ownership of mutex.
dwWaitResult = WaitForSingleObject(
ListViewMutex, // handle to mutex
7000L); // five-second time-out interval
Adapter->AddToMemo("OnConnect release");
ReleaseMutex(ListViewMutex);
return;
}
就是这样。当我运行我的服务器并且客户端连接时,我的服务器应用程序冻结。它甚至无法到达 'RelaseMutex(...)' 行 3 次(之前假设连接了 3 个客户端) 当我删除 Adapter->AddToMemo() 行时,它可以到达 ReleaseMutex(...) 行 3 次(但当然该代码什么都不做)
我是否以错误的方式使用互斥锁,或者这里有什么问题?
【问题讨论】:
标签: c++ multithreading c++builder