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. question on critical section

question on critical section

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Hi. I have a thread that adds values to a vector variable in a class. also main program reads same thread an to display data in it. I havn't used critical section or other sync methods but program works ok, is it necessary to use critical section ? thanks.

    A 1 Reply Last reply
    0
    • A Anonymous

      Hi. I have a thread that adds values to a vector variable in a class. also main program reads same thread an to display data in it. I havn't used critical section or other sync methods but program works ok, is it necessary to use critical section ? thanks.

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      main program reads same vector variable not thread.

      M 1 Reply Last reply
      0
      • A Anonymous

        main program reads same vector variable not thread.

        M Offline
        M Offline
        Mohammad A Gdeisat
        wrote on last edited by
        #3

        Hi, Because u r using multi-threading application you SHOULD sync ur work using critical sections, events, semaphores,... your program runs well now, but when the thread writes to the variable at the same time as the main thread reads it, the value read by the main thread will be out-of-date, it is not the current value of the variable. Furthermore, if you don't use the keyword volatitle in declaring shared variables, the compiler may perform wrong optimizations to speed the code up, and you may get wrong results. -Always take the safest way Regards, Mohammad Gdeisat

        A 1 Reply Last reply
        0
        • M Mohammad A Gdeisat

          Hi, Because u r using multi-threading application you SHOULD sync ur work using critical sections, events, semaphores,... your program runs well now, but when the thread writes to the variable at the same time as the main thread reads it, the value read by the main thread will be out-of-date, it is not the current value of the variable. Furthermore, if you don't use the keyword volatitle in declaring shared variables, the compiler may perform wrong optimizations to speed the code up, and you may get wrong results. -Always take the safest way Regards, Mohammad Gdeisat

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          thank you Mohammad

          D 1 Reply Last reply
          0
          • A Anonymous

            thank you Mohammad

            D Offline
            D Offline
            digwizfox
            wrote on last edited by
            #5

            Another concern is a possible program crash. When vectors are changed, all iterators are invalidated. So if the main function has an interator and is in the process of reading the data, the thread could interrupt and add something to the vector. Now the main function has an invalid iterator. The problem with not using synch methods is that the errors may not be noticed for months. You could think all is well and then one day, your program crashes while doing the same thing it's been doing all along. Shawn

            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