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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Thread synchronization

Thread synchronization

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorial
5 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    singh_nav
    wrote on last edited by
    #1

    Hi all, I am having a list(queue)of tasks. My two threads are using the same list. First thread is to insert the task into it. (appending the list) Second thread is to process on each task . ( reading the list) My aim is to give the maximum availability to Thread one without hanging thread two. How to synchronize both thread so that, second thread should not wait for long and First thread keep inserting the item into list. Thanks in advance

    T 1 Reply Last reply
    0
    • S singh_nav

      Hi all, I am having a list(queue)of tasks. My two threads are using the same list. First thread is to insert the task into it. (appending the list) Second thread is to process on each task . ( reading the list) My aim is to give the maximum availability to Thread one without hanging thread two. How to synchronize both thread so that, second thread should not wait for long and First thread keep inserting the item into list. Thanks in advance

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      critical section! look for CCriticalSection class in MFC!

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
      Never mind - my own stupidity is the source of every "problem" - Mixture

      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

      R 1 Reply Last reply
      0
      • T ThatsAlok

        critical section! look for CCriticalSection class in MFC!

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
        Never mind - my own stupidity is the source of every "problem" - Mixture

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        A piece of good advice is to stay clear of the MFC synchronization objects because they are not working properly. They may fail under certain circumstances. E.g. you cannot recursively lock a CCriticalSection from the same thread because it will cause a deadlock. Use the Win32 API with ::EnterCriticalSection() and ::LeaveCriticalSection() instead. Read more here[^].

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        C 1 Reply Last reply
        0
        • R Roger Stoltz

          A piece of good advice is to stay clear of the MFC synchronization objects because they are not working properly. They may fail under certain circumstances. E.g. you cannot recursively lock a CCriticalSection from the same thread because it will cause a deadlock. Use the Win32 API with ::EnterCriticalSection() and ::LeaveCriticalSection() instead. Read more here[^].

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Roger Stoltz wrote:

          E.g. you cannot recursively lock a CCriticalSection from the same thread because it will cause a deadlock.

          :omg: Wow, thanks for the info Roger. I've never used the MFC sync objects before but I don't think I will ever use them now.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          R 1 Reply Last reply
          0
          • C Cedric Moonen

            Roger Stoltz wrote:

            E.g. you cannot recursively lock a CCriticalSection from the same thread because it will cause a deadlock.

            :omg: Wow, thanks for the info Roger. I've never used the MFC sync objects before but I don't think I will ever use them now.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            Yup, pretty amazing, isn't it? But the evidence is right there in the code. X| I've never used the MFC sync classes since I read Joe's advice many years ago in a much earlier essay of his. Even though CCriticalSection may work if it's only locked once in a call chain, you will eventually find yourself in a situation where you should perform a second lock due to encapsulation and at that time you're smoked; a re-write using the Win32 primitives is essential before it has a chance of working.

            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            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