Click here to Skip to main content
16,011,702 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem with static and std::map Pin
inter8ection4-Feb-04 4:01
inter8ection4-Feb-04 4:01 
GeneralRe: Problem with static and std::map Pin
Diddy4-Feb-04 4:07
Diddy4-Feb-04 4:07 
GeneralRe: Problem with static and std::map Pin
inter8ection4-Feb-04 4:40
inter8ection4-Feb-04 4:40 
GeneralRe: Problem with static and std::map Pin
Diddy4-Feb-04 4:47
Diddy4-Feb-04 4:47 
GeneralProblem with Kernel Object Security Pin
particle2k4-Feb-04 1:15
particle2k4-Feb-04 1:15 
GeneralRe: Problem with Kernel Object Security Pin
Alexander M.,4-Feb-04 3:09
Alexander M.,4-Feb-04 3:09 
GeneralRe: Problem with Kernel Object Security Pin
particle2k4-Feb-04 3:31
particle2k4-Feb-04 3:31 
GeneralMultithreading in C++ Pin
TV4-Feb-04 0:11
TV4-Feb-04 0:11 
Hello,

I am trying to do a object oriented multithreaded program with MFC.

In order to do that, I have a class CToto whose contructor creates 2 threads with AfxBeginThread. As an argument to AfxBeginThread I pass the this pointer.

CToto::CToto()<br />
{<br />
   /...other inits here.../<br />
    fastThread = AfxBeginThread((AFX_THREADPROC)ObjectFastThreadRoutine, this);<br />
    slowThread = AfxBeginThread((AFX_THREADPROC)ObjectSlowThreadRoutine, this);<br />
}


(The CToto instance is created on the heap with the new operator in the OnInitDialog of a CDialog class.)

The thread proc are as follow:
UINT ObjectFastThreadRoutine(CToto* lpParam) <br />
{ <br />
    lpParam->FastThread();<br />
    return 0;<br />
}

UINT ObjectSlowThreadRoutine(CToto* lpParam) <br />
{ <br />
    lpParam->SlowThread();<br />
    return 0;<br />
}

I have therefore two thread running in two members functions of the CToto class. The goal of this approach is to acces to the variables of this class with both threads.

To be thread safe, I read and write in the member variables that I want to share with functions which are protected with CCriticalSection objects, here is one of those functions used to write to the positionMeasured member variable of CToto:

void CToto::SetPosition(double *pos)<br />
{<br />
    CSingleLock singleLock(critSectionPosition); //critSectionPosition is also a member variable of the class<br />
    singleLock.Lock();<br />
<br />
    for(int i=0; i<4; i++)<br />
    {<br />
        positionMeasured[i] = pos[i];<br />
    }<br />
<br />
    singleLock.Unlock();<br />
}


I have three differents member variables such as positionMeasured that are protected like that. All those three variables have Get and Set functions to read and write.

My problem occurs when the SlowThread tries to access to the SetPosition function. I have a debug assertion failed at the CSingleLock singleLock(critSectionPosition); line. The assertion is the following:

CSingleLock::CSingleLock(CSyncObject* pObject, BOOL bInitialLock)<br />
{<br />
     ASSERT(pObject != NULL);<br />
---->     ASSERT(pObject->IsKindOf(RUNTIME_CLASS(CSyncObject)));<br />
<br />
     m_pObject = pObject;<br />
     m_hObject = pObject->m_hObject;<br />
     m_bAcquired = FALSE;<br />
<br />
     if (bInitialLock)<br />
          Lock();<br />
}<br />
BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const<br />
{<br />
     ASSERT(this != NULL);<br />
     // it better be in valid memory, at least for CObject size<br />
------>     ASSERT(AfxIsValidAddress(this, sizeof(CObject)));<br />
<br />
     // simple SI case<br />
     CRuntimeClass* pClassThis = GetRuntimeClass();<br />
     return pClassThis->IsDerivedFrom(pClass);<br />
}


I really don't know what to do. I am not an expert in multithreading and maybe (sure) I am doing something false with memory access and memory zone of threads. So if anybody can help me on how to solve this problem or can propose me another program structure for one class with shared ressources and two threads accesing them concurently (those threads must be able to access the private members of the class if possible)

Thank a lot for your answers.

Regards

TV
GeneralRe: Multithreading in C++ Pin
Diddy4-Feb-04 2:11
Diddy4-Feb-04 2:11 
GeneralRe: Multithreading in C++ Pin
TV4-Feb-04 3:58
TV4-Feb-04 3:58 
GeneralRe: Multithreading in C++ Pin
Diddy4-Feb-04 4:30
Diddy4-Feb-04 4:30 
QuestionHow do i get handle to window in IE Pin
User 2155973-Feb-04 23:41
User 2155973-Feb-04 23:41 
AnswerRe: How do i get handle to window in IE Pin
Amr Abdel-Mohsen4-Feb-04 9:20
Amr Abdel-Mohsen4-Feb-04 9:20 
Generalprinting a html file Pin
Member 5281553-Feb-04 22:53
Member 5281553-Feb-04 22:53 
GeneralRe: printing a html file Pin
Anonymous4-Feb-04 11:22
Anonymous4-Feb-04 11:22 
Generalsingleton problem Pin
yccheok3-Feb-04 22:19
yccheok3-Feb-04 22:19 
GeneralRe: singleton problem Pin
Cristian Teodorescu3-Feb-04 23:01
Cristian Teodorescu3-Feb-04 23:01 
QuestionWhy slower draws on Screen with HW Accel. set to FULL? Pin
uus993-Feb-04 21:46
uus993-Feb-04 21:46 
Generallib/dll file question Pin
bryce3-Feb-04 21:45
bryce3-Feb-04 21:45 
GeneralRe: lib/dll file question Pin
bryce3-Feb-04 22:15
bryce3-Feb-04 22:15 
GeneralRe: lib/dll file question Pin
goozmo3-Feb-04 22:37
goozmo3-Feb-04 22:37 
GeneralDatabase environment Pin
JensB3-Feb-04 21:38
JensB3-Feb-04 21:38 
GeneralRe: Database environment Pin
Roger Wright4-Feb-04 4:00
professionalRoger Wright4-Feb-04 4:00 
GeneralCan't not read string correctly from resource Pin
Paul.Huang3-Feb-04 20:26
Paul.Huang3-Feb-04 20:26 
GeneralRe: Can't not read string correctly from resource Pin
Diddy4-Feb-04 2:23
Diddy4-Feb-04 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.