Thread: Forcing a context switch

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,579

    Forcing a context switch

    I'm aware of sched_yield() & SwitchToThread but they only seem to do thread level, I'd like to do process level prior to a loop that is supposed to lock a value (without functions like pthread_mutex_lock) to ensure the race condition I'm expecting in the loop is the one I actually get, is there any functions that would enable telling the system to switch process context if it needs to? The loop in question is expected to have a short life but a process level context switch during the loop somehow interferes with it's purpose so I though I'd try my tests with a forced switch prior to the loop.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    Any system call will give the OS the opportunity to force a switch. In particular, a blocking call will practically guarantee it. You might consider something like sleep(0); or a write to /dev/null.

    Note that there are layers in the standard library that separate you from system calls, so you may have to do some digging. Calling sleep(0) might be intercepted since it's a no-op. (Check the linux sources). Calling printf and friends will almost certainly be buffered, so you'll have to call write or _write or something.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,579
    Quote Originally Posted by aghast View Post
    Any system call will give the OS the opportunity to force a switch. In particular, a blocking call will practically guarantee it. You might consider something like sleep(0); or a write to /dev/null.

    Note that there are layers in the standard library that separate you from system calls, so you may have to do some digging. Calling sleep(0) might be intercepted since it's a no-op. (Check the linux sources). Calling printf and friends will almost certainly be buffered, so you'll have to call write or _write or something.
    Thanks, I was just about to respond to this thread when I saw your post, I think I finally managed to design a suitable fallback for a lack of system provided mutex, it's slightly slower than the pthread_mutex_t but it seems to work without issue (unless you count annoying dialogues in debuggers). I've attached a copy of my test file for the curious
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with finishing this Context Switch
    By bayw in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2020, 04:02 PM
  2. Measure Time spent in thread context switch
    By n.a.s in forum Linux Programming
    Replies: 3
    Last Post: 03-23-2014, 01:33 PM
  3. Context Switch benchmark for real time linux.
    By odysseas in forum C Programming
    Replies: 5
    Last Post: 05-26-2011, 11:45 AM
  4. Forcing buttons...
    By elfjuice in forum Windows Programming
    Replies: 2
    Last Post: 04-30-2005, 02:27 AM
  5. Forcing an enum to int?
    By Sebastiani in forum C++ Programming
    Replies: 14
    Last Post: 11-03-2002, 12:11 PM

Tags for this Thread