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. Resource Access in Multiple Timers in Single thread

Resource Access in Multiple Timers in Single thread

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelpquestionannouncement
5 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.
  • R Offline
    R Offline
    RCM
    wrote on last edited by
    #1

    Hi, I have a dialog based MFC application which has 2 timers with same interval (say 5 mins) accessing a single resource. A structure instance which is a member variable of the App class is being updated in one timer and the same structure instance is being used in another timer. ( Typically, the memory resource is being accessed for write in one timer callback and for read in another timer callback ). The code snippet something is like this, CMyFrameDlg::TimerCallback1(...) { ... theApp.m_Struct.nVar1 = 10; theApp.m_Struct.szVar2 = "test"; ... ... } CMyFrameDlg::TimerCallback2(...) { ... ... localnVar = theApp.m_Struct.nVar1; localszVar = theApp.m_Struct.szVar2; ... } The 2 timers get called simultaneously. Issue is, that the values dont get reflected properly in timer2 and while the struct is being accessed in timer2 it is being updated simulataneously in timer1. Both timers belong to the same Thread. 1.) I need to update the same resource in one timer and use it for some other purpose in the second timer. Is it possible ? 2.) I also have certain WM_.. messages posted from another thread to this thread on which a callback gets invoked. And this callback also accesses the same structure instance, but the values are not properly updated. Why is it so ? Point is.. accessing the same resource ( memory) in multiple timers of the same thread. And also accessing same resource in multiple callbacks of the same thread (like user defined WM_message callbacks). Is there any restriction or way to do that ? Anxiously Waiting for a reply :~ Cheers Raja

    H 1 Reply Last reply
    0
    • R RCM

      Hi, I have a dialog based MFC application which has 2 timers with same interval (say 5 mins) accessing a single resource. A structure instance which is a member variable of the App class is being updated in one timer and the same structure instance is being used in another timer. ( Typically, the memory resource is being accessed for write in one timer callback and for read in another timer callback ). The code snippet something is like this, CMyFrameDlg::TimerCallback1(...) { ... theApp.m_Struct.nVar1 = 10; theApp.m_Struct.szVar2 = "test"; ... ... } CMyFrameDlg::TimerCallback2(...) { ... ... localnVar = theApp.m_Struct.nVar1; localszVar = theApp.m_Struct.szVar2; ... } The 2 timers get called simultaneously. Issue is, that the values dont get reflected properly in timer2 and while the struct is being accessed in timer2 it is being updated simulataneously in timer1. Both timers belong to the same Thread. 1.) I need to update the same resource in one timer and use it for some other purpose in the second timer. Is it possible ? 2.) I also have certain WM_.. messages posted from another thread to this thread on which a callback gets invoked. And this callback also accesses the same structure instance, but the values are not properly updated. Why is it so ? Point is.. accessing the same resource ( memory) in multiple timers of the same thread. And also accessing same resource in multiple callbacks of the same thread (like user defined WM_message callbacks). Is there any restriction or way to do that ? Anxiously Waiting for a reply :~ Cheers Raja

      H Offline
      H Offline
      Harsha Gopal
      wrote on last edited by
      #2

      Raja, Why dont you consider using the Critical Section to avoid simultaneous access to the same resource... So, when the timers get called simultaneously, only one timer accesses the resource and the other waits till the critical section is released. If the application spans processes, you should be considering Mutex... Hope it helps you... Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------

      H R 2 Replies Last reply
      0
      • H Harsha Gopal

        Raja, Why dont you consider using the Critical Section to avoid simultaneous access to the same resource... So, when the timers get called simultaneously, only one timer accesses the resource and the other waits till the critical section is released. If the application spans processes, you should be considering Mutex... Hope it helps you... Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------

        H Offline
        H Offline
        harish kota
        wrote on last edited by
        #3

        hi Harsha Critical section can be used only when resource is getting accessed between 2 threads. What raja needs a way to share resource within same thread.

        1 Reply Last reply
        0
        • H Harsha Gopal

          Raja, Why dont you consider using the Critical Section to avoid simultaneous access to the same resource... So, when the timers get called simultaneously, only one timer accesses the resource and the other waits till the critical section is released. If the application spans processes, you should be considering Mutex... Hope it helps you... Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------

          R Offline
          R Offline
          RCM
          wrote on last edited by
          #4

          Thanks for your reply. I would like to know if critical sections can be used to lock a section of code within the same thread. Critical Sections within same thread - possible ? Cheers Raja

          H 1 Reply Last reply
          0
          • R RCM

            Thanks for your reply. I would like to know if critical sections can be used to lock a section of code within the same thread. Critical Sections within same thread - possible ? Cheers Raja

            H Offline
            H Offline
            Harsha Gopal
            wrote on last edited by
            #5

            I am sorry that I misunderstood the question... Anyway, I think critical sections can be used within the same thread. Although developed for multithreading, I suppose that critical sections do not understand which thread they should give access; they just know which resource to lock. Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------

            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