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. When to use Critical Sections in Threads?

When to use Critical Sections in Threads?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
8 Posts 6 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
    Aidman
    wrote on last edited by
    #1

    Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

    R T N R A 5 Replies Last reply
    0
    • A Aidman

      Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      Use it when you assign global varibles, reading is safe. Allocated memory chunks and files, don't know. Rickard Andersson Here is my card, contact me later! UIN: 50302279 E-Mail: nikado@pc.nu Interests: C++, ADO, SQL, Winsock, 0s and 1s

      P 1 Reply Last reply
      0
      • A Aidman

        Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

        T Offline
        T Offline
        Ted Ferenc
        wrote on last edited by
        #3

        The best advise I saw was if you think you need a critcal section then you probably do, err on the side of caution. Manipulating, IMHO, means changing a shared resource, if a file is open for reading only by all threads then you don't need it, but if one thread writes to the file, then you probably do. Having a global variable is not a good idea at the best of times, but especially not in a multi threaded application. I would recomend the book "Multithreading applications in Win32" by Jim Beveridge and Rober Wiener.


        If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676

        1 Reply Last reply
        0
        • A Aidman

          Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #4

          Whenever > 1 thread needs read or write access to a resource (memory/disk/database record whatever) you typically need to synchronize the access. Depending on the resource it may be possible for > 1 thread to have simultaneous read access, but there may be a bigger picture issue such as one thread writing to the resource when others are reading it. You then need to go a level higher and see whether transactions are needed etc. Bottom line is you need to go read a good book or two on multithreaded design and implementation and all that entails. Multithreading is a whole world unto itself where deadlocks occur at the blink of an eye. And debuging multithreaded apps is way harder than single threaded apps. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

          1 Reply Last reply
          0
          • A Aidman

            Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

            R Offline
            R Offline
            Ryan_Roberts
            wrote on last edited by
            #5

            You should guard all non constant data that you share between threads with some sort of locking mechanism. 'Manipulating' in this context, probably does mean writing. A critical section is a spin lock, it uses an atomic instruction to repeatedly test for and aquire the lock. This makes critical sections unsuitable for locking resources that may take a long time to become free, the other threads will consume cycles waiting for the lock. You should use a kernel lock in these cases, such as a mutex or event. Critical sections are only suitable for syncronising threads, not processes - they rely on shared memory. Have a look at the boost thread library http://www.boost.org/libs/thread/doc/[^], it makes it a hell of a lot easier to write correct multithreaded programs. Ryan.

            1 Reply Last reply
            0
            • A Aidman

              Hi, all :) When is it necessary to use critical sections in a multithread application? I have read that it should be used when “manipulating shared resources”, but what do those that really mean? Does it mean when manipulating: global variables, allocated memory chunks or files? And does “manipulating” mean: reading (probably not), writing or reallocating? Could anyone please demonstrate some code examples for typical situations when critical sections should be used? Thanks in Advance :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

              A Offline
              A Offline
              Aidman
              wrote on last edited by
              #6

              Thanks mates :) I am beginning to realize the many disadvantages of using multithreads. It’s not as “perfect” as I imagined, but then again nothing ever is :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

              1 Reply Last reply
              0
              • R Rickard Andersson20

                Use it when you assign global varibles, reading is safe. Allocated memory chunks and files, don't know. Rickard Andersson Here is my card, contact me later! UIN: 50302279 E-Mail: nikado@pc.nu Interests: C++, ADO, SQL, Winsock, 0s and 1s

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                Rickard Andersson18 wrote: reading is safe. not really, when it's not an atomic operation - anything above size of 4 is in danger: you might catch the first half before the modification, and the other afterwards.


                "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                sighist | Agile Programming | doxygen

                R 1 Reply Last reply
                0
                • P peterchen

                  Rickard Andersson18 wrote: reading is safe. not really, when it's not an atomic operation - anything above size of 4 is in danger: you might catch the first half before the modification, and the other afterwards.


                  "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                  sighist | Agile Programming | doxygen

                  R Offline
                  R Offline
                  Rickard Andersson20
                  wrote on last edited by
                  #8

                  Oh, okay! I've learned it from CP when I one time also talked about critical sections and threads etc here. Rickard Andersson Here is my card, contact me later! UIN: 50302279 E-Mail: nikado@pc.nu Interests: C++, ADO, SQL, Winsock, 0s and 1s

                  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