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. Thread synchronization

Thread synchronization

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
4 Posts 2 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.
  • K Offline
    K Offline
    Kamis
    wrote on last edited by
    #1

    Hi there! The thing I would like to know is how to lock and unlock a single variable, such as int or double? All these semaphores and events must be declared within a class, but how can I do it with simple types?

    A 1 Reply Last reply
    0
    • K Kamis

      Hi there! The thing I would like to know is how to lock and unlock a single variable, such as int or double? All these semaphores and events must be declared within a class, but how can I do it with simple types?

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      You still have to use at least a Critical Section to lock against while accessing the simple type. Ant. I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!
      - David Williams (Little Britain)

      K 1 Reply Last reply
      0
      • A Antony M Kancidrowski

        You still have to use at least a Critical Section to lock against while accessing the simple type. Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        K Offline
        K Offline
        Kamis
        wrote on last edited by
        #3

        Hmm, I've found InterlockedExchange and I guess that's what I need. By the way, what will happen if I read variable's value in one thread and write to it from another thread using InterlockedExchange, something like this: bool yes_no; //Common variable void Thread1Func() { bool tmp; tmp=yes_no; } void Thread2Func() { InterlockedExchange((LPLONG)&yes_no,true); } So, if thread 1 get's to yes_no first is it going to work correctly?

        A 1 Reply Last reply
        0
        • K Kamis

          Hmm, I've found InterlockedExchange and I guess that's what I need. By the way, what will happen if I read variable's value in one thread and write to it from another thread using InterlockedExchange, something like this: bool yes_no; //Common variable void Thread1Func() { bool tmp; tmp=yes_no; } void Thread2Func() { InterlockedExchange((LPLONG)&yes_no,true); } So, if thread 1 get's to yes_no first is it going to work correctly?

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          Assuming that Thread 1 has to wait until Thread 2 has updated the data then no it is not guaranteed to work. Have you thought about using events? Wait for the event in Thread 1 (before the read of the variable) then signal the event in Thread 2 after the variable update. This will ensure the data is current when Thread 1 comes to read it. Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Williams (Little Britain)

          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