I have to program a simple threaded program with MFC/C++ for a uni assignment.
I have a simple scenario in wich i have a worker thread which executes a function along the lines of :
UINT createSchedules(LPVOID param)
{
genProgThreadVal* v = (genProgThreadVal*) param;
// v->searcherLock is of type CcriticalSection*
while(1)
{
if(v->searcherLock->Lock())
{
//do the stuff, access shared object , exit clause etc..
v->searcherLock->Unlock();
}
}
PostMessage(v->hwnd, WM_USER_THREAD_FINISHED , 0,0);
delete v;
return 0;
}
In my main UI class, i have a CListControl that i want to be able to access the shared object (of type std::List). Hence the locking stuff. So this CList has an handler function looking like this :
void Ccreationprogramme::OnLvnItemchangedList5(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
if((pNMLV->uChanged & LVIF_STATE)
&& (pNMLV->uNewState & LVNI_SELECTED))
{
searcherLock.Lock();
// do the stuff on shared object
searcherLock.Unlock();
// do some more stuff
}
*pResult = 0;
}
The searcherLock in both function is the same object. The worker thread function is passed a pointer to the CCriticalSection object, which is a member of my dialog class.
Everything works but, as soon as i do click on my list, and so triggers the handler function, the whole program hangs indefinitely.I tried using a Cmutex. I tried using a CSingleLock wrapping over the critical section object, and none of this has worked. What am i missing ?
EDIT: I found the solution, thanks to the amazing insight of Franci. That'll teach me to not put every bit of the code into the question. Thanks !