Thread syncronization Win32
-
Hi for all I've created a program that triggers some threads. I don't get to use the syncronization. Some threads are triggered before others. I created a class called ResourceObject. This is a simple class with a boolean value and a CRITICAL_SECTION value:
ResourceObject::ResourceObject() { m_bLocked = false; InitializeCriticalSection(&m_ResourceSection); } ResourceObject::~ResourceObject() { DeleteCriticalSection(&m_ResourceSection); } void ResourceObject::LockResource(unsigned int uiTimeToSleep) { EnterCriticalSection(&m_ResourceSection); m_bLocked = true; Sleep(uiTimeToSleep); LeaveCriticalSection(&m_ResourceSection); } void ResourceObject::ReleaseResource() { EnterCriticalSection(&m_ResourceSection); if(m_bLocked)m_bLocked = false; LeaveCriticalSection(&m_ResourceSection); }
In another class I have this struct that have a pointer to a ResourceObjecttypedef struct LineText { FloatText * Line; bool bShow; float fTimeWait; ResourceObject * resource; }TextLine;
In the same class I have the following codes: - the first is how I create a thread;idThread[m_iLineCount] = CreateThread(NULL, 0, Helper::TextThread, (void*)&Lines[m_iLineCount], CREATE_SUSPENDED, NULL);
- the second is the thread:DWORD WINAPI Helper::TextThread(void * vData) { LineText * temp = (LineText *)vData; temp->resource->LockResource(2000); temp->bShow = true; temp->resource->ReleaseResource(); return 0; }
- the third is when I Resume the threads and show some text;void Helper::ShowImages() { for(int i = 0 ; i < m_iLineCount ; i++) { ResumeThread(idThread[i]); if(Lines[i].bShow)Lines[i].Line->PrintText(); } }
The threads must have to follow the sequence in that they are resumed, but they don't. Even the first thread is started with the resource, it is not the first to show the text. Anyone can help me to solve this problem? Thanks -
Hi for all I've created a program that triggers some threads. I don't get to use the syncronization. Some threads are triggered before others. I created a class called ResourceObject. This is a simple class with a boolean value and a CRITICAL_SECTION value:
ResourceObject::ResourceObject() { m_bLocked = false; InitializeCriticalSection(&m_ResourceSection); } ResourceObject::~ResourceObject() { DeleteCriticalSection(&m_ResourceSection); } void ResourceObject::LockResource(unsigned int uiTimeToSleep) { EnterCriticalSection(&m_ResourceSection); m_bLocked = true; Sleep(uiTimeToSleep); LeaveCriticalSection(&m_ResourceSection); } void ResourceObject::ReleaseResource() { EnterCriticalSection(&m_ResourceSection); if(m_bLocked)m_bLocked = false; LeaveCriticalSection(&m_ResourceSection); }
In another class I have this struct that have a pointer to a ResourceObjecttypedef struct LineText { FloatText * Line; bool bShow; float fTimeWait; ResourceObject * resource; }TextLine;
In the same class I have the following codes: - the first is how I create a thread;idThread[m_iLineCount] = CreateThread(NULL, 0, Helper::TextThread, (void*)&Lines[m_iLineCount], CREATE_SUSPENDED, NULL);
- the second is the thread:DWORD WINAPI Helper::TextThread(void * vData) { LineText * temp = (LineText *)vData; temp->resource->LockResource(2000); temp->bShow = true; temp->resource->ReleaseResource(); return 0; }
- the third is when I Resume the threads and show some text;void Helper::ShowImages() { for(int i = 0 ; i < m_iLineCount ; i++) { ResumeThread(idThread[i]); if(Lines[i].bShow)Lines[i].Line->PrintText(); } }
The threads must have to follow the sequence in that they are resumed, but they don't. Even the first thread is started with the resource, it is not the first to show the text. Anyone can help me to solve this problem? ThanksIf you want multiple threads to pass through a single function in predetermined order you're going to need a master thread to manage them and each one will have to wait to be signalled. I would add an Event to your one-per-thread structure, have each worker wait on its event and then have the main thread signal each event and wait for it to be reset by the signalled thread before singalling the next one. Have the worker reset its event after it has executed your controlled function. I can't see why you'd want to do this as it seems to negate any real benefit of threading but it's your project :)
Nothing is exactly what it seems but everything with seems can be unpicked.
-
Hi for all I've created a program that triggers some threads. I don't get to use the syncronization. Some threads are triggered before others. I created a class called ResourceObject. This is a simple class with a boolean value and a CRITICAL_SECTION value:
ResourceObject::ResourceObject() { m_bLocked = false; InitializeCriticalSection(&m_ResourceSection); } ResourceObject::~ResourceObject() { DeleteCriticalSection(&m_ResourceSection); } void ResourceObject::LockResource(unsigned int uiTimeToSleep) { EnterCriticalSection(&m_ResourceSection); m_bLocked = true; Sleep(uiTimeToSleep); LeaveCriticalSection(&m_ResourceSection); } void ResourceObject::ReleaseResource() { EnterCriticalSection(&m_ResourceSection); if(m_bLocked)m_bLocked = false; LeaveCriticalSection(&m_ResourceSection); }
In another class I have this struct that have a pointer to a ResourceObjecttypedef struct LineText { FloatText * Line; bool bShow; float fTimeWait; ResourceObject * resource; }TextLine;
In the same class I have the following codes: - the first is how I create a thread;idThread[m_iLineCount] = CreateThread(NULL, 0, Helper::TextThread, (void*)&Lines[m_iLineCount], CREATE_SUSPENDED, NULL);
- the second is the thread:DWORD WINAPI Helper::TextThread(void * vData) { LineText * temp = (LineText *)vData; temp->resource->LockResource(2000); temp->bShow = true; temp->resource->ReleaseResource(); return 0; }
- the third is when I Resume the threads and show some text;void Helper::ShowImages() { for(int i = 0 ; i < m_iLineCount ; i++) { ResumeThread(idThread[i]); if(Lines[i].bShow)Lines[i].Line->PrintText(); } }
The threads must have to follow the sequence in that they are resumed, but they don't. Even the first thread is started with the resource, it is not the first to show the text. Anyone can help me to solve this problem? Thanks -
Not sure I understand, you want to use multiple threads to perform sequential operations? :confused:
More or less. Like you've seen in my code, I have a thread that receives a structure. I have about 10 structures that have different values of text. And I need to show these texts in some moment. At the moment I need to show these values sequentially, but in any other time I would need to show these texts in any other sequence. Just like that, I create the ResourceObject class. I have a pointer to an object of this type in my structs. The first started thread access this object and the others wait. I called the threads sequentially thinking that the calling order have been respected, with one thread at time access the resource and release it.
-
More or less. Like you've seen in my code, I have a thread that receives a structure. I have about 10 structures that have different values of text. And I need to show these texts in some moment. At the moment I need to show these values sequentially, but in any other time I would need to show these texts in any other sequence. Just like that, I create the ResourceObject class. I have a pointer to an object of this type in my structs. The first started thread access this object and the others wait. I called the threads sequentially thinking that the calling order have been respected, with one thread at time access the resource and release it.
Alex Cutovoi wrote:
More or less.
What? You don't know? How are we supposed to help you if you can't specify exactly what your requirements are.
Alex Cutovoi wrote:
I need to show these texts in some moment.
I have no idea what that means. I am not familiar with "Moment Based Processing". Look the bottom line is that trying to use multiple threads to execute sequential operations is like putting a square peg in a round hole. It doesn't seem your approach is the appropriate solution to your problem, whatever that is.
-
Alex Cutovoi wrote:
More or less.
What? You don't know? How are we supposed to help you if you can't specify exactly what your requirements are.
Alex Cutovoi wrote:
I need to show these texts in some moment.
I have no idea what that means. I am not familiar with "Moment Based Processing". Look the bottom line is that trying to use multiple threads to execute sequential operations is like putting a square peg in a round hole. It doesn't seem your approach is the appropriate solution to your problem, whatever that is.
Look, I'll explain better. What I really want is the threads access the resource in the order that they started. What that is not occur at the moment. Example, I have 4 thread(thread1, thread2, thread3, thread4). If I start the threads in a sequence like thread4, thread2, thread1, thread3, the threads must access the resource in this sequence. Even with the CriticalSection, this is not occur. The threads access the resource in an arbitrary order. The only thing that works is that the resource is accessed for one thread at a time. I've tried to put a mutex to guarantee this type of access but I didn't get the expected results. This is EXACTLY what I want.
-
Look, I'll explain better. What I really want is the threads access the resource in the order that they started. What that is not occur at the moment. Example, I have 4 thread(thread1, thread2, thread3, thread4). If I start the threads in a sequence like thread4, thread2, thread1, thread3, the threads must access the resource in this sequence. Even with the CriticalSection, this is not occur. The threads access the resource in an arbitrary order. The only thing that works is that the resource is accessed for one thread at a time. I've tried to put a mutex to guarantee this type of access but I didn't get the expected results. This is EXACTLY what I want.
Alex Cutovoi wrote:
Look, I'll explain better.
Alex Cutovoi wrote:
This is EXACTLY what I want.
No that is not an explanation of the "requirements". What "you want" has nothing to do with what "the requirements" are. Do you have requirements for sequential operations? Yes or no? If yes then threads are not an appropriate solution even if you "want" to use threads. Note: You should never "want" to use threads. They should be used as a last resort, only to satisfy specific requirements.
-
Alex Cutovoi wrote:
Look, I'll explain better.
Alex Cutovoi wrote:
This is EXACTLY what I want.
No that is not an explanation of the "requirements". What "you want" has nothing to do with what "the requirements" are. Do you have requirements for sequential operations? Yes or no? If yes then threads are not an appropriate solution even if you "want" to use threads. Note: You should never "want" to use threads. They should be used as a last resort, only to satisfy specific requirements.
You're right, but unhapilly I solved my problem. Thanks