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. Problems synchronizing multiple threads??

Problems synchronizing multiple threads??

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
17 Posts 10 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.
  • P Offline
    P Offline
    pavanbabut
    wrote on last edited by
    #1

    Hi, I am having an application written in vc++ in which it is having multiple threads (to say 2 threads) running parallel. These threads are created by the user at the same time, but ofcourse one after the other. I want them to be syncronized with each other so that they wont be off even by 33.3msec. Actuallly these two threads generate waveform signals through 2 waveform generator boards which finally generates images. So, the framerate will be at 30Hz. The problem is each thread generates two differnt images at the same frame rate, but while I replay the images they are "sometimes" off by one frame(33.3msec). thanks, -Pavan

    J M V M 4 Replies Last reply
    0
    • P pavanbabut

      Hi, I am having an application written in vc++ in which it is having multiple threads (to say 2 threads) running parallel. These threads are created by the user at the same time, but ofcourse one after the other. I want them to be syncronized with each other so that they wont be off even by 33.3msec. Actuallly these two threads generate waveform signals through 2 waveform generator boards which finally generates images. So, the framerate will be at 30Hz. The problem is each thread generates two differnt images at the same frame rate, but while I replay the images they are "sometimes" off by one frame(33.3msec). thanks, -Pavan

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      Have you implemented any synchronization mechanism between the two threads, or just created them and let them go? Best, Jun

      1 Reply Last reply
      0
      • P pavanbabut

        Hi, I am having an application written in vc++ in which it is having multiple threads (to say 2 threads) running parallel. These threads are created by the user at the same time, but ofcourse one after the other. I want them to be syncronized with each other so that they wont be off even by 33.3msec. Actuallly these two threads generate waveform signals through 2 waveform generator boards which finally generates images. So, the framerate will be at 30Hz. The problem is each thread generates two differnt images at the same frame rate, but while I replay the images they are "sometimes" off by one frame(33.3msec). thanks, -Pavan

        M Offline
        M Offline
        mikeorama12345
        wrote on last edited by
        #3

        mswin does not provide us with much control of threads. They do not even give us a simple control to temporarily disable switching of threads. Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between. It could prevent a mess of control flags. Anyway, I always suggest... DO it in one thread! Then you do not have these problem! Just livin a dream.. dont wake me!

        D L Z 3 Replies Last reply
        0
        • M mikeorama12345

          mswin does not provide us with much control of threads. They do not even give us a simple control to temporarily disable switching of threads. Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between. It could prevent a mess of control flags. Anyway, I always suggest... DO it in one thread! Then you do not have these problem! Just livin a dream.. dont wake me!

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          mikeorama12345 wrote:

          Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between.

          Isn't this what a critical section does?


          "The largest fire starts but with the smallest spark." - David Crow

          "Judge not by the eye but by the heart." - Native American Proverb

          M 1 Reply Last reply
          0
          • P pavanbabut

            Hi, I am having an application written in vc++ in which it is having multiple threads (to say 2 threads) running parallel. These threads are created by the user at the same time, but ofcourse one after the other. I want them to be syncronized with each other so that they wont be off even by 33.3msec. Actuallly these two threads generate waveform signals through 2 waveform generator boards which finally generates images. So, the framerate will be at 30Hz. The problem is each thread generates two differnt images at the same frame rate, but while I replay the images they are "sometimes" off by one frame(33.3msec). thanks, -Pavan

            V Offline
            V Offline
            valikac
            wrote on last edited by
            #5

            depends cpu count waitt0 t0 ++ signalglobal waitt1 t1 ++ signalglobal waitglobal if () signal t0,t1 Kuphryn

            1 Reply Last reply
            0
            • M mikeorama12345

              mswin does not provide us with much control of threads. They do not even give us a simple control to temporarily disable switching of threads. Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between. It could prevent a mess of control flags. Anyway, I always suggest... DO it in one thread! Then you do not have these problem! Just livin a dream.. dont wake me!

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

              mikeorama12345 wrote:

              mswin does not provide us with much control of threads.

              Compared to what, Linux? X|

              1 Reply Last reply
              0
              • M mikeorama12345

                mswin does not provide us with much control of threads. They do not even give us a simple control to temporarily disable switching of threads. Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between. It could prevent a mess of control flags. Anyway, I always suggest... DO it in one thread! Then you do not have these problem! Just livin a dream.. dont wake me!

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                That is what Mutex's, Semaphores/Critical Sections are for. Almost forgot ... and events. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -- modified at 15:31 Tuesday 27th June, 2006

                M 1 Reply Last reply
                0
                • P pavanbabut

                  Hi, I am having an application written in vc++ in which it is having multiple threads (to say 2 threads) running parallel. These threads are created by the user at the same time, but ofcourse one after the other. I want them to be syncronized with each other so that they wont be off even by 33.3msec. Actuallly these two threads generate waveform signals through 2 waveform generator boards which finally generates images. So, the framerate will be at 30Hz. The problem is each thread generates two differnt images at the same frame rate, but while I replay the images they are "sometimes" off by one frame(33.3msec). thanks, -Pavan

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  Windows is not a real-time OS, it does not give you control over when your threads will run. You can try increasing the threads' priority, but still, if some higher-priority task comes along, it will get CPU time and there's nothing you can do about it.

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                  P 1 Reply Last reply
                  0
                  • Z Zac Howland

                    That is what Mutex's, Semaphores/Critical Sections are for. Almost forgot ... and events. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -- modified at 15:31 Tuesday 27th June, 2006

                    M Offline
                    M Offline
                    mikeorama12345
                    wrote on last edited by
                    #9

                    mutex, boraphores, and critical sectionz are a bunch of clutter and a lot of work. This is America. I dont want to work so hard.. I want to live! I hope some day they invent simple macros: LockThread () blah blah blah // YEY! I have 100% CPU unless timeout UnLockThread () blah blah blah // ok go ahead and switch threads on me now, i dont care Thread switching probably requires interrupts. maybe we can use asm sti and cli ? (set interrupt and clr interrupt) Just livin a dream.. dont wake me!

                    Z J 2 Replies Last reply
                    0
                    • M mikeorama12345

                      mutex, boraphores, and critical sectionz are a bunch of clutter and a lot of work. This is America. I dont want to work so hard.. I want to live! I hope some day they invent simple macros: LockThread () blah blah blah // YEY! I have 100% CPU unless timeout UnLockThread () blah blah blah // ok go ahead and switch threads on me now, i dont care Thread switching probably requires interrupts. maybe we can use asm sti and cli ? (set interrupt and clr interrupt) Just livin a dream.. dont wake me!

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      You mean something like this: Main Thread struct myData { shared_ptr ptr; }; void main() { shared_ptr mySection(new CCriticalSection); myData data; data.ptr = mySection; HANDLE hThreads[2]; hThreads[0] = CreateThread(..., &data); // Thread 1 hThreads[1] = CreateThread(..., &data); // Thread 2 while (WaitForMultipleObjects(...) == WAIT_TIMEOUT) { // do something } } Threads 1 and 2 int Thread1(LPARAM pParam) { myData data = *(myData*)pParam; shared_ptr mySection = data.ptr; while (IDoSomething()) { mySection->Lock(); // I do stuff mySection->Unlock(); } return 0; } Yes, sooo much more clutter ... If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      1 Reply Last reply
                      0
                      • M mikeorama12345

                        mutex, boraphores, and critical sectionz are a bunch of clutter and a lot of work. This is America. I dont want to work so hard.. I want to live! I hope some day they invent simple macros: LockThread () blah blah blah // YEY! I have 100% CPU unless timeout UnLockThread () blah blah blah // ok go ahead and switch threads on me now, i dont care Thread switching probably requires interrupts. maybe we can use asm sti and cli ? (set interrupt and clr interrupt) Just livin a dream.. dont wake me!

                        J Offline
                        J Offline
                        John M Drescher
                        wrote on last edited by
                        #11

                        This was how it used to work with windows3.1 but a bad side affect of this is that with this policy any application could steal 100% of the cpu for as long as it wants and if it crashed during this time down went the os.

                        1 Reply Last reply
                        0
                        • D David Crow

                          mikeorama12345 wrote:

                          Just imagine if we could disable switching momentarily so that we could execute a couple of lines of code and guarantee that no other threads were run in between.

                          Isn't this what a critical section does?


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #12

                          All a critical section does is prevent another thread from running the protected section of code. He was asking about totally preventing other threads from running, which isn't possible.

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                          G J 2 Replies Last reply
                          0
                          • M Michael Dunn

                            All a critical section does is prevent another thread from running the protected section of code. He was asking about totally preventing other threads from running, which isn't possible.

                            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                            G Offline
                            G Offline
                            Graham Bradshaw
                            wrote on last edited by
                            #13

                            It is, but I think you have to be running Windows 3.1 :-D

                            1 Reply Last reply
                            0
                            • M Michael Dunn

                              All a critical section does is prevent another thread from running the protected section of code. He was asking about totally preventing other threads from running, which isn't possible.

                              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                              J Offline
                              J Offline
                              Jun Du
                              wrote on last edited by
                              #14

                              Michael Dunn wrote:

                              He was asking about totally preventing other threads from running,

                              I don't think so, but I'd like to hear what the originator says.

                              Michael Dunn wrote:

                              which isn't possible.

                              Why not? Best, Jun

                              J 1 Reply Last reply
                              0
                              • J Jun Du

                                Michael Dunn wrote:

                                He was asking about totally preventing other threads from running,

                                I don't think so, but I'd like to hear what the originator says.

                                Michael Dunn wrote:

                                which isn't possible.

                                Why not? Best, Jun

                                J Offline
                                J Offline
                                John M Drescher
                                wrote on last edited by
                                #15

                                Jun Du wrote:

                                I don't think so, but I'd like to hear what the originator says.

                                Isn't this the only way both threads can remain time synchronized? I mean the only way I can see this to work is he has to have a dual processor dual core machine with the two threds on their own cpu and that the operating system will never schedule any other threads (or yield execution) on either of the cpus that are runing the two threads.

                                J 1 Reply Last reply
                                0
                                • J John M Drescher

                                  Jun Du wrote:

                                  I don't think so, but I'd like to hear what the originator says.

                                  Isn't this the only way both threads can remain time synchronized? I mean the only way I can see this to work is he has to have a dual processor dual core machine with the two threds on their own cpu and that the operating system will never schedule any other threads (or yield execution) on either of the cpus that are runing the two threads.

                                  J Offline
                                  J Offline
                                  Jun Du
                                  wrote on last edited by
                                  #16

                                  My reply "I don't think so" is to Michael's comment that the originator wanted to prevent other threads from running. I think what the originator wanted (or needed) is sychronizing the tweo threads so that they output the same frame at a given time. I see two issues mixed with the originator's question: real-time and synchronization. By real-time, I mean that the simulation time your program complete goes hand-in-hand with your wall clock, no faster no slower. Lets say you're simulating the traffic on a highway. You want your program to complete the simulation of one sencond traffic within one second time. Only this way you can acheive real-time. Multiple CPUs can help to acheive real-time through better parallelism. But if the computation is very simple, one CPU can do the job too. As you may notice now, parallelism is not a necessity in pursuing real-time. Synchroniztion is a different issue. It is about the collaboration between two threads. It has nothing to do with how fast your CPU is. You don't need multiple CPUs to acheive synchronization. One of the major purposes for synchronization is protecting resources shared by more than one thread. Best, Jun

                                  1 Reply Last reply
                                  0
                                  • M Michael Dunn

                                    Windows is not a real-time OS, it does not give you control over when your threads will run. You can try increasing the threads' priority, but still, if some higher-priority task comes along, it will get CPU time and there's nothing you can do about it.

                                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                                    P Offline
                                    P Offline
                                    pavanbabut
                                    wrote on last edited by
                                    #17

                                    Thanks for all your replies. Yep, I think my question should state as 'Parallel/simultaneous processing of multiple threads'. Synchronization is more related to resource sharing between threads. The problem is to run both threads exactly at same speed and at the same time (which assures the frame rate). I have tried several ways by defining the priority classes to both threads to HIGH_PRIORITY_CLASS and setting their priority to PRORITY_TIME_CRITICAL, but still 'sometimes' I am getting a difference of one frame between 2 threads. I am running my application on a Dell Precision 670 (Dual xeon processors) with 2 threads on each core. Here is how my application works- I am having 2 waveform generating boards on 2 of my 64-bit PCI slots. In my application I am generating waveforms on both of these boards and each thread generates waveforms on each of these boards. These boards will have on-board buffers where we can store the data to be generated as waveforms. Since these buffers are to be in multiples of 1024 and my data is not in multiples of 1024, as usual, I am using rotating buffers. The board buffers, data buffers are completely independent for both threads (each thread is having its own buffers), so no data is shared between these threads except the intial flag variable which initiates the starting loop in the thread. Neither of these threads change the flag. They just use it to start their wave form generating loop (just like as a trigger signal). At last I would like to know how we can make sure these threads run at the same rate and speed and at any given instant of time they both should be at the same point of execution (one more thing is both these threads have exact and same number of instructions). thanks, -Pavan

                                    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