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#
  4. Multi-threading - Parallel Syncronization

Multi-threading - Parallel Syncronization

Scheduled Pinned Locked Moved C#
5 Posts 4 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.
  • C Offline
    C Offline
    caksabre2
    wrote on last edited by
    #1

    Hello, I have a program that opens multiple user controls, each one needing to run 3 parallel processes. It needs to run all 3 simultaneously, and then when all 3 are complete run a process. This needs to occur every second. What I have done, is create 3 threads, one main thread which when it starts running it calls two .Set() methods for manualevent handlers which then sets the other two threads running. I then have a waitall in the main thread (after the point where it has done its work) which waits for the other two threads to complete. At the end of the two threads I have a waitall which waits for a different manualevent to be reset (which happens when the main thread finishes). Now this all seems to work fine, but when running 4 user controls at once, approximetly every minute the threads somehow get confused and one of the waitall's for the two sub threads get stuck. So I have put a timeout parameter here which is a hack, and doesn't really solve why it is locking up. I can't put all the code in but below is an example of it working. Can anyone help come up with why this is occurring, or a better way of doing this? //Thread Ones Run Method public void ThreadOne() { while(true) { ogEvents[0] = new ManualResetEvent(false); ogEvents[1] = new ManualResetEvent(false); //Do a task here //Wait for the other 2 threads to finish bool blSuccess = WaitHandle.WaitAll(ogEvents, 4000, false); //Do some calcs Thread.Sleep(1000); ogThreadTwo[0].Set(); ogThreadThree[0].Set(); } } //Thread Two's Run Method public void ThreadTwo() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadTwo[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadTwo, 4000, false); } } //Thread Three's Run Method public void ThreadThree() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); } }

    L I A 3 Replies Last reply
    0
    • C caksabre2

      Hello, I have a program that opens multiple user controls, each one needing to run 3 parallel processes. It needs to run all 3 simultaneously, and then when all 3 are complete run a process. This needs to occur every second. What I have done, is create 3 threads, one main thread which when it starts running it calls two .Set() methods for manualevent handlers which then sets the other two threads running. I then have a waitall in the main thread (after the point where it has done its work) which waits for the other two threads to complete. At the end of the two threads I have a waitall which waits for a different manualevent to be reset (which happens when the main thread finishes). Now this all seems to work fine, but when running 4 user controls at once, approximetly every minute the threads somehow get confused and one of the waitall's for the two sub threads get stuck. So I have put a timeout parameter here which is a hack, and doesn't really solve why it is locking up. I can't put all the code in but below is an example of it working. Can anyone help come up with why this is occurring, or a better way of doing this? //Thread Ones Run Method public void ThreadOne() { while(true) { ogEvents[0] = new ManualResetEvent(false); ogEvents[1] = new ManualResetEvent(false); //Do a task here //Wait for the other 2 threads to finish bool blSuccess = WaitHandle.WaitAll(ogEvents, 4000, false); //Do some calcs Thread.Sleep(1000); ogThreadTwo[0].Set(); ogThreadThree[0].Set(); } } //Thread Two's Run Method public void ThreadTwo() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadTwo[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadTwo, 4000, false); } } //Thread Three's Run Method public void ThreadThree() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); } }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, why are you creating new ManualResetEvents all the time? I would create them once, and resuse them, i.a. call Set(), Reset() and Wait/WaitAll() as required, no more no less. Replacing one ManualResetEvent by another you are risking signaling or waitinf for one that isn't actually in business any more. Whatever you do, you must allow for any or all of your threads not to get any CPU cycles at all for some time, e.g. because some high-priority thing suddenly happens. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • C caksabre2

        Hello, I have a program that opens multiple user controls, each one needing to run 3 parallel processes. It needs to run all 3 simultaneously, and then when all 3 are complete run a process. This needs to occur every second. What I have done, is create 3 threads, one main thread which when it starts running it calls two .Set() methods for manualevent handlers which then sets the other two threads running. I then have a waitall in the main thread (after the point where it has done its work) which waits for the other two threads to complete. At the end of the two threads I have a waitall which waits for a different manualevent to be reset (which happens when the main thread finishes). Now this all seems to work fine, but when running 4 user controls at once, approximetly every minute the threads somehow get confused and one of the waitall's for the two sub threads get stuck. So I have put a timeout parameter here which is a hack, and doesn't really solve why it is locking up. I can't put all the code in but below is an example of it working. Can anyone help come up with why this is occurring, or a better way of doing this? //Thread Ones Run Method public void ThreadOne() { while(true) { ogEvents[0] = new ManualResetEvent(false); ogEvents[1] = new ManualResetEvent(false); //Do a task here //Wait for the other 2 threads to finish bool blSuccess = WaitHandle.WaitAll(ogEvents, 4000, false); //Do some calcs Thread.Sleep(1000); ogThreadTwo[0].Set(); ogThreadThree[0].Set(); } } //Thread Two's Run Method public void ThreadTwo() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadTwo[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadTwo, 4000, false); } } //Thread Three's Run Method public void ThreadThree() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); } }

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        Purely for organizational purposes, I would actually have a fourth thread controlling the other three, if possible. Threading problems can be difficult to debug, so keeping the code clean can sometimes be a big help... Also, I would probably go with just basic EventWaitHandles

        EventWaitHandle workerThreadStart[] = new EventWaitHandle[3];
        EventWaitHandle workerThreadFinish[] = new EventWaitHandle[3];

        // Controller thread:

        while (true)
        {
        for (int x = 0; x < 3; x++) workerThreadStart[x].Set();
        WaitHandle.WaitAll(workerThreadFinish);

        // Insert a delay if desired
        }

        // Each worker thread (# = 0, 1, or 2 for threads one, two, three):

        while (true)
        {
        workerThreadStart[#].WaitOne();

        try
        {
        // Do some task
        }
        catch (...)
        {
        // Remember, if you get an exception on a background thread, it may not display
        }
        finally
        {
        // Even if we get an error, we want to set the EWH so as not to stall everything
        workerThreadFinish[#].Set();
        }
        }

        (Note: Just typed this out in here, not in a code editor, so might have typos or syntax problems - Just trying to give a rough idea anyway) EDIT: Oh, and when you instantiate the EWH's, bring 'em up unsignaled and AutoReset.

        1 Reply Last reply
        0
        • C caksabre2

          Hello, I have a program that opens multiple user controls, each one needing to run 3 parallel processes. It needs to run all 3 simultaneously, and then when all 3 are complete run a process. This needs to occur every second. What I have done, is create 3 threads, one main thread which when it starts running it calls two .Set() methods for manualevent handlers which then sets the other two threads running. I then have a waitall in the main thread (after the point where it has done its work) which waits for the other two threads to complete. At the end of the two threads I have a waitall which waits for a different manualevent to be reset (which happens when the main thread finishes). Now this all seems to work fine, but when running 4 user controls at once, approximetly every minute the threads somehow get confused and one of the waitall's for the two sub threads get stuck. So I have put a timeout parameter here which is a hack, and doesn't really solve why it is locking up. I can't put all the code in but below is an example of it working. Can anyone help come up with why this is occurring, or a better way of doing this? //Thread Ones Run Method public void ThreadOne() { while(true) { ogEvents[0] = new ManualResetEvent(false); ogEvents[1] = new ManualResetEvent(false); //Do a task here //Wait for the other 2 threads to finish bool blSuccess = WaitHandle.WaitAll(ogEvents, 4000, false); //Do some calcs Thread.Sleep(1000); ogThreadTwo[0].Set(); ogThreadThree[0].Set(); } } //Thread Two's Run Method public void ThreadTwo() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadTwo[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadTwo, 4000, false); } } //Thread Three's Run Method public void ThreadThree() { while(true) { //Do some task //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); } }

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Hi, I'm not sure why you are continually reallocating the event objects. Doing so may have created a situation where you are waiting for a signal on an event object that is no longer referenced in your code. It is quite possible that ThreadOne could set ogThreadTwo or ogThreadThree prior to reallocation in ThreadTwo or ThreadThree. If that happens the WaitAll methods will block as they are waiting for the reallocated events to be signalled. //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); should be reordered ogThreadThree[0] = new ManualResetEvent(false); ogEvents[0].Set(); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); so that ogThreadThree event is rellocated BEFORE you release ThreadOne. Alan.

          C 1 Reply Last reply
          0
          • A Alan N

            Hi, I'm not sure why you are continually reallocating the event objects. Doing so may have created a situation where you are waiting for a signal on an event object that is no longer referenced in your code. It is quite possible that ThreadOne could set ogThreadTwo or ogThreadThree prior to reallocation in ThreadTwo or ThreadThree. If that happens the WaitAll methods will block as they are waiting for the reallocated events to be signalled. //Task Finished ogEvents[0].Set(); ogThreadThree[0] = new ManualResetEvent(false); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); should be reordered ogThreadThree[0] = new ManualResetEvent(false); ogEvents[0].Set(); bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false); so that ogThreadThree event is rellocated BEFORE you release ThreadOne. Alan.

            C Offline
            C Offline
            caksabre2
            wrote on last edited by
            #5

            Thanks for the reply's. I have changed the code to have 4 threads, and have stopped reallocating the event and it seems to have fixed any sync issues. 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