Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Thread syncronization Win32

Thread syncronization Win32

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncareerlearning
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Cutovoi
    wrote on last edited by
    #1

    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 ResourceObject typedef 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

    M L 2 Replies Last reply
    0
    • A Alex Cutovoi

      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 ResourceObject typedef 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

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      If 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.

      1 Reply Last reply
      0
      • A Alex Cutovoi

        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 ResourceObject typedef 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

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Not sure I understand, you want to use multiple threads to perform sequential operations? :confused:

        A 1 Reply Last reply
        0
        • L led mike

          Not sure I understand, you want to use multiple threads to perform sequential operations? :confused:

          A Offline
          A Offline
          Alex Cutovoi
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • A Alex Cutovoi

            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.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            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.

            A 1 Reply Last reply
            0
            • L led mike

              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.

              A Offline
              A Offline
              Alex Cutovoi
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • A Alex Cutovoi

                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.

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • L led mike

                  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.

                  A Offline
                  A Offline
                  Alex Cutovoi
                  wrote on last edited by
                  #8

                  You're right, but unhapilly I solved my problem. Thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups