I’m using MFC threading:
both main thread and worker thread are using the same function ReceiveResponse().
and I change myMaster->configDone so that the worker thread executes different work based on myMaster->configDone value.
the problem is that I may change myMaster->configDone value in my main thread, but the worker thread sometimes would receive data even when the myMaster->configDone != tComSts::COM_CFG_DONE. in my opinion because it hasnt finish the loop before that the main thread changed the value of myMaster->configDone.
what should I do in this case please?
#include <afxwin.h>
#include <afxmt.h>
#define THREAD_DELAY_MS 100
class myClass;
WorkerThread::WorkerThread(myClass *pClassSession)
: m_pSession(pSession), m_hThread(nullptr)
{
}
WorkerThread::~WorkerThread()
{
}
BOOL WorkerThread::InitInstance()
{
m_hThread = AfxBeginThread(ThreadFunction, m_pSession, THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED);
m_hThread->m_bAutoDelete = FALSE;
return TRUE;
}
int WorkerThread::ExitInstance()
{
return CWinThread::ExitInstance();
}
using ModeFieldBits = SetDaqListModePacket::ModeFieldBits;
UINT WorkerThread::ThreadFunction(LPVOID pParam)
{
myClass *myMaster = reinterpret_cast<myClass *>(pParam);
if (nullptr != myMaster)
{
while (true)
{
if (tComSts::COM_CFG_DONE == myMaster->configDone)
{
st_msg rmsg = {0};
myMaster->ReceiveResponse(true, TIMEOUT_RECEIVE, &rmsg);
}
else if (tComSts::COM_CFG_NONE == myMaster->configDone)
{
//do some work
}
else
{
//do nothing
}
}
}
return 0;
}