I defined critSectionPosition as a pointer and create the object with new in the CToto constructor. I also tried to create it in the stack and I get the same problem. I define my CCriticalSection objects as members of CToto because I have a GetPosition and a SetPosition fonction so I need the same CCriticalSection object to lock the position variable which is accessed by those two function independentely. I juste tried now replacing my CCriticalSection by CMutex and it is working... I know that CMutex are not well suited for my case so I would really like to manage to do that with CCriticalSection. Thanks for your time
TV
Posts
-
Multithreading in C++ -
Multithreading in C++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() { /...other inits here.../ fastThread = AfxBeginThread((AFX_THREADPROC)ObjectFastThreadRoutine, this); slowThread = AfxBeginThread((AFX_THREADPROC)ObjectSlowThreadRoutine, this); }
(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) { lpParam->FastThread(); return 0; }
UINT ObjectSlowThreadRoutine(CToto* lpParam) { lpParam->SlowThread(); return 0; }
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) { CSingleLock singleLock(critSectionPosition); //critSectionPosition is also a member variable of the class singleLock.Lock(); for(int i=0; i<4; i++) { positionMeasured[i] = pos[i]; } singleLock.Unlock(); }
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) { ASSERT(pObject != NULL); ----> ASSERT(pObject->IsKindOf(RUNTIME_CLASS(CSyncObject))); m_pObject = pObject; m_hObject = pObject->m_hObject; m_bAcquired = FALSE; if (bInitialLock) Lock(); } BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const { ASSERT(this != NULL); // it better be in valid memory, at least for CObject size ------> ASSERT(AfxIsValidAddress(this, sizeof(CObject))); // simple SI case CRuntimeClass* pClassThis = GetRuntimeClass(); return pC
-
fast line drawingHello, Actually I am using a timer to redraw every 20 milliseconds my drawing because I am aquiring point from some sensors. I agree with your remark about vertical or horizontal lines, but it is rarely (never...) the case, I have always non-horizontal nor vertical lines.
-
fast line drawingHello, I am writing a program that draws a lot of lines (up to about 30000 lines/seconds) and some text (it is a kind of chart). In order to do this, I am using GDI. I draw everything in a backbuffer and I use BitBlt to copy it to the front buffer (which is the device context of a CDialog on a dialog based VC++ 6 project). The result is that my program is very slow, use a lot of cpu time and slow down when I drag the mouse over it! Since i want to increase the speed of my code, I would like to use DirectX, OpenGL or something else. Does anybody know wich one would be the fastest to draw a lot of lines and some text with double buffering, and what would be the approximative gain of speed? Thanks for your attention (and sorry for my bad english!). TV
-
Arrays of controlHello, I'am doing a mfc dialog based program. In this one, I have a settings dialog box that should allow the user to select somethings through check boxes. The problem is that I've a lot of checkbox in this dialog (18 check box even more later!). I wanted to do array of controls in my settings dialog box (just like in VB). In order to do that I've short-circuited Class wizard to create an array of CButton member variables but I still have to enumerate each variable for each ID of each control with the DDX_Control(...) macro. There is also another problem. If the member variable is defined like this: CButton m_variable1; I can use the following statement: m_variable1.SetCheck(); But if I use the following declaration: CButton m_variable[18]; m_variable[0].SetCheck(); I'have a assertion failed. Can somebody give me a smart way to do such array of control? Thanks a lot. T. Varidel