CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    171

    Is it possible to ensure a piece of code executed without context switch?

    I need to send 256 bytes over serial port without any interrupt, it will take less than 500 ms, but any context switch will fail the task. Is it possible to make this short period of time no context switch?

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Is it possible to ensure a piece of code executed without context switch?

    Not in Windows OS. But, what I don't understand is, how the context switch is affecting your task ? I'd be curious to know more details, if possible.

  3. #3
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: Is it possible to ensure a piece of code executed without context switch?

    As kirants already pointed out, Windows will context-switch whenever it likes, you can't influence that. You will need a hard-realtime OS for that.

    Having said that, you may want to try sending the 256 bytes in one single WriteFile call to the port. I'm not too familiar with the underlying driver layers, but you may find that the complete buffer of 256 bytes is handed to the port driver "as-is" and that the action from there on is completely interrupt driven. If this assumption is true then the driver will not be affected by context switching.

  4. #4
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: Is it possible to ensure a piece of code executed without context switch?

    One 'hack' is to raise the thread priority to Real Time for that portion of the code. But there are no gaurantees even then because

    1. There may be other (system) threads running at that priority.
    2. Your thread may 'wait for something' i.e. enter blocked/not runnable state in the middle of the operation. E.g. during many kind of I/O operations, the thread would be waiting for e.g. things to be send out to port etc.

    But having said that, boosting priority to highest level (for short time) does the trick at times.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured