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. Threads synchronization problem

Threads synchronization problem

Scheduled Pinned Locked Moved C / C++ / MFC
helplearning
6 Posts 5 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.
  • L Offline
    L Offline
    lisoft
    wrote on last edited by
    #1

    There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.

    P J 2 Replies Last reply
    0
    • L lisoft

      There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      lisoft wrote: m_lock.Lock(); while (!m_lock.IsLocked()); Your will always hang on the while line as you just locked m_lock on the previous line. There is no need to check the lock state as the code will wait on the m_lock.Lock() line until thread one has unlocked the lock. Remove the while and your code should work better.


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

      1 Reply Last reply
      0
      • L lisoft

        There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.

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

        Your

        while (!m_lock.IsLocked());

        will never execute because m_lock will always be locked at that step. Your request cries out for a manual reset event but it looks like you are using something other than an event. John

        L 1 Reply Last reply
        0
        • J John M Drescher

          Your

          while (!m_lock.IsLocked());

          will never execute because m_lock will always be locked at that step. Your request cries out for a manual reset event but it looks like you are using something other than an event. John

          L Offline
          L Offline
          lisoft
          wrote on last edited by
          #4

          What should I do to let thread two waiting until the thread one finish? :confused::confused::confused:

          J R 2 Replies Last reply
          0
          • L lisoft

            What should I do to let thread two waiting until the thread one finish? :confused::confused::confused:

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            If I remember right the lock function does not return until it has managed to lock. So checking it is lock after call lock is redundant. The purpose of locking is to allow a thread to manipulate the data with messing up another threads actions. In other words only one thread can read or modify the data at a given time. You do not want to try to copy a string while anouther thread is modifying that same string. So the answer is YES! Before thread two can work with the data thread one has to stop working with it. Before thead one can work with the data thread two has to stop working with it. The individual threads do not have to stop they just have to stop accessing the shared data long enough for anouther thread to access it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

            1 Reply Last reply
            0
            • L lisoft

              What should I do to let thread two waiting until the thread one finish? :confused::confused::confused:

              R Offline
              R Offline
              Robert Bielik
              wrote on last edited by
              #6

              You do: Somewhere define an event that both threads know about: HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); which is a manual reset event with unsignalled state to begin with. Then in your worker thread: // Do something, stuff, thing or whatever... // Ok Ready, signal the event ::SetEvent(hEvent); Then in thread 2 you just wait for the event // Wait for event to be signalled ::WaitForSingleObject(hEvent, INFINITE); Execution will continue after the ::WaitForSinglObject as soon as the event gets signalled.

              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