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. Prevent context switch

Prevent context switch

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestionlearning
12 Posts 6 Posters 4 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.
  • C Cedric Moonen

    As far as I know, this is something impossible on Windows. This is for security reasons: imagine you "lock" at the begining of your code but forget to "unlock". It would mean that your computer is completely frozen (only your app runs, but it can't get any input because they are not processed).

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

    S Offline
    S Offline
    Shay Harel
    wrote on last edited by
    #3

    Yep, pretty much what I had in mind... Thanks.

    1 Reply Last reply
    0
    • S Shay Harel

      Does anyone know of a way to prevent context switch in a block of code? As far as I can tell (unless I am wrong) Critical Sections do not guarantee not to switch context, it simply means threads can't access a shared resource. I am looking for some API that will guarantee an atomic execution of a block of code (if this API even exists) Shay

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #4

      You can get close by temporarily elevate the thread priority to real time. But even then there's no guarantee.

      home

      S _ 2 Replies Last reply
      0
      • S Shay Harel

        Does anyone know of a way to prevent context switch in a block of code? As far as I can tell (unless I am wrong) Critical Sections do not guarantee not to switch context, it simply means threads can't access a shared resource. I am looking for some API that will guarantee an atomic execution of a block of code (if this API even exists) Shay

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #5

        I suppose kernel code can. See, for instance [^]. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • CPalliniC CPallini

          I suppose kernel code can. See, for instance [^]. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          Shay Harel
          wrote on last edited by
          #6

          nice but in kernel I prefer to use more orthodox stuff :) My question was for something I am doing in user space.

          CPalliniC 1 Reply Last reply
          0
          • N Niklas L

            You can get close by temporarily elevate the thread priority to real time. But even then there's no guarantee.

            home

            S Offline
            S Offline
            Shay Harel
            wrote on last edited by
            #7

            That's what I ended up doing.

            1 Reply Last reply
            0
            • S Shay Harel

              nice but in kernel I prefer to use more orthodox stuff :) My question was for something I am doing in user space.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #8

              Well, if it is tricky in kernel space... :rolleyes:

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • S Shay Harel

                Does anyone know of a way to prevent context switch in a block of code? As far as I can tell (unless I am wrong) Critical Sections do not guarantee not to switch context, it simply means threads can't access a shared resource. I am looking for some API that will guarantee an atomic execution of a block of code (if this API even exists) Shay

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                can't be done in user mode, and that is how it has to be in order to protect one user/app from another user/app. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                CPalliniC 1 Reply Last reply
                0
                • N Niklas L

                  You can get close by temporarily elevate the thread priority to real time. But even then there's no guarantee.

                  home

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #10

                  Read this somewhere that calling SetThreadPriority also resets the quantum of the thread. So calling SetThreadPriority repeatedly can infact give exclusive control to the thread, at least in theory. But I would never recommend elevating priority of a thread to real time. Rather you should design your code in such a way that it handles re-entrancy.

                  «_Superman_»
                  I love work. It gives me something to do between weekends.

                  Microsoft MVP (Visual C++)

                  Polymorphism in C

                  N 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    can't be done in user mode, and that is how it has to be in order to protect one user/app from another user/app. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Luc Pattyn wrote:

                    it has to be in order to protect one user/app from another user/app.

                    :confused:

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • _ _Superman_

                      Read this somewhere that calling SetThreadPriority also resets the quantum of the thread. So calling SetThreadPriority repeatedly can infact give exclusive control to the thread, at least in theory. But I would never recommend elevating priority of a thread to real time. Rather you should design your code in such a way that it handles re-entrancy.

                      «_Superman_»
                      I love work. It gives me something to do between weekends.

                      Microsoft MVP (Visual C++)

                      Polymorphism in C

                      N Offline
                      N Offline
                      Niklas L
                      wrote on last edited by
                      #12

                      That was interesting. I had a quick look around, but could only find information that this was a problem in Windows NT. Regarding reentrant code, I don't think the OP's reason was thread safety. Just to prevent a task switch.

                      home

                      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