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. Condition variable question

Condition variable question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
10 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.
  • S Offline
    S Offline
    samzcs
    wrote on last edited by
    #1

    Try to write a small program.almost forgot task sync concept, need some help. task1: if (cx == True) { do task1(); cx = False; } task2: if (cx == False) { do task2(); cx = True; } Is possible the 2 tasks step in a deadlock situation? Thanks

    L 1 Reply Last reply
    0
    • S samzcs

      Try to write a small program.almost forgot task sync concept, need some help. task1: if (cx == True) { do task1(); cx = False; } task2: if (cx == False) { do task2(); cx = True; } Is possible the 2 tasks step in a deadlock situation? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes; see Thread Synchronization for Beginners[^].

      F 1 Reply Last reply
      0
      • L Lost User

        Yes; see Thread Synchronization for Beginners[^].

        F Offline
        F Offline
        focusdoit
        wrote on last edited by
        #3

        can you explain a little more ? thanks

        L 1 Reply Last reply
        0
        • F focusdoit

          can you explain a little more ? thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Explain what?

          F 1 Reply Last reply
          0
          • L Lost User

            Explain what?

            F Offline
            F Offline
            focusdoit
            wrote on last edited by
            #5

            Why the above code could be deadlock. thanks

            L 1 Reply Last reply
            0
            • F focusdoit

              Why the above code could be deadlock. thanks

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Because they are both using the same variable without checking that it is free to update. Follow the link in my response and read about thread synchronisation.

              F 1 Reply Last reply
              0
              • L Lost User

                Because they are both using the same variable without checking that it is free to update. Follow the link in my response and read about thread synchronisation.

                F Offline
                F Offline
                focusdoit
                wrote on last edited by
                #7

                So use While(cx != True) ; similar code could resolve it?

                L 1 Reply Last reply
                0
                • F focusdoit

                  So use While(cx != True) ; similar code could resolve it?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Possibly, but threads do not always behave the way you expect.

                  F 1 Reply Last reply
                  0
                  • L Lost User

                    Possibly, but threads do not always behave the way you expect.

                    F Offline
                    F Offline
                    focusdoit
                    wrote on last edited by
                    #9

                    Sure, the code is embedded software code, no OS, so, do you think there is a better way to implement the condition variable?

                    L 1 Reply Last reply
                    0
                    • F focusdoit

                      Sure, the code is embedded software code, no OS, so, do you think there is a better way to implement the condition variable?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      That's a whole new question.

                      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