CreateThread/WaitForSingleObject frozen problem

Hien Pham 1 Reputation point
2021-04-24T05:54:19.13+00:00

Hello All

I have a problem with CreateThread and WaitForSingleObject.
My program is frozen in the second thread (ThreadDirectIOEvent). Can't back to the main thread.
I don't understand why it is frozen in the second thread (ThreadDirectIOEvent). Please confirm for me.
Thank you so much.

This is my program:

In the main thread
{
CreateThread(NULL, 0, ThreadDirectIOEvent, (PVOID)this, 0, 0);

ucRet = SendTelegram("010B", ulSeq, 0, NULL, 60000); -> msgType=010B
if (RESULT_SEND_OK != ucRet)
{
SendTelegramErr(pOut);
LogWrite(DEBUGLOG, " [%s]:L%d:Send Faild", FUNCTION, LINE);
goto END;
}
ucRet = ReceiveTelegram("110B", ulSeq, usResDataLen, (BYTE*)&outdata, 60000); -> msgType=110B timeout 1min
}

In ThreadDirectIOEvent thread
{
// Wait 110C
ucRet = ReceiveTelegram("110C", seq, usResDataLen, (BYTE*)&outdata); -> msgType=110C No timeout
if (RESULT_RECV_OK != ucRet) {
lResult = FALSE;
LogWrite(DEBUGLOG, " [%s]:L%d:Telegram Receive Faild", FUNCTION, LINE);
goto END;
}
}

In ReceiveTelegram method
{
bRet = client->PopMsg_ByRep((void**)&req, (void**)&rep, timeout, seqno, type); -> type is msgType=110B or 110C
WaitForSingleObject(ISGS_Mutex, INFINITE);
}

TID=8592 is main thread, TID=7012 is ThreadDirectIOEvent thread

TID=8592] UT INF, ->executeCommand[main.cpp:2130] Command=1 pData=0
TID=8592] UT INF, ->SendTelegram msgType=010B seqno=2
TID=7012] UT INF, ->ThreadDirectIOEvent[main.cpp:153]
TID=8592] UT INF, <-SendTelegram ucRet=0
TID=7012] UT INF, ->ReceiveTelegram[Telegram.cpp:775] msgType=110C SeqNo=0 msgLen=6000 timeout=-1
---> Program is frozen at here sometime. I don't understand why it is frozen at here.

Normal, it will run as bellow

TID=8600] UT INF, ->executeCommand[main.cpp:2130] Command=1 pData=0
TID=8600] UT INF, ->SendTelegram msgType=010B seqno=2
TID=8388] UT INF, ->ThreadDirectIOEvent[main.cpp:153]
TID=8600] UT INF, <-SendTelegram ucRet=0
TID=8388] UT INF, ->ReceiveTelegram[Telegram.cpp:775] msgType=110C SeqNo=0 msgLen=6000 timeout=-1
TID=8600] UT INF, ->ReceiveTelegram[Telegram.cpp:775] msgType=110B SeqNo=0 msgLen=6000 timeout=60000
TID=8600] UT INF, <-ReceiveTelegram[Telegram.cpp:836] msgType=110B SeqNo=2 msgLen=12 Ret=0
TID=8388] UT INF, <-ReceiveTelegram[Telegram.cpp:836] msgType=110C SeqNo=0 msgLen=6000 Ret=1
---> Program is run normal

Windows development | Windows API - Win32
Developer technologies | C++
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.