Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WIN32: Yielding execution to another (given) thread

I am looking for a way to yield the remainder of the thread execution's scheduled time slice to a different thread. There is a SwitchToThread function in WINAPI, but it doesn't let the caller specify the thread it wants to switch to. I browsed MSDN for quite some time and haven't found anything that would offer just that.

For an operating-system-internals layman like me, it seems that yielding thread should be able to specify which thread does it want to pass the execution to. Is it possible or is it just my imagination?

like image 319
Marcin Seredynski Avatar asked Jan 07 '10 18:01

Marcin Seredynski


People also ask

What does thread yield() do in c#?

Causes the calling thread to yield execution to another thread that is ready to run on the current processor.

What is thread yield in Java?

Java Thread yield() method The yield() method of thread class causes the currently executing thread object to temporarily pause and allow other threads to execute.

How do I create a thread in win32?

You can also create a thread by calling the CreateRemoteThread function. This function is used by debugger processes to create a thread that runs in the address space of the process being debugged.

How do you find the thread ID of a thread handle?

If you have a thread identifier, you can get the thread handle by calling the OpenThread function. OpenThread enables you to specify the handle's access rights and whether it can be inherited. A thread can use the GetCurrentThread function to retrieve a pseudo handle to its own thread object.


2 Answers

The reason you can't yield processor time-slices to a designated thread is that Windows features a preemptive scheduling kernel which pretty much places the responsibility and authority of scheduling the processor time in the hands of the kernel and only the kernel.

As such threads don't have any control over when they run, if they run, and even less over which thread is switched to after their time slice is up.

However, there are a few way you may influence context switches:

  • by increasing the priority of a certain thread you may force the scheduler to schedule it more often in the detriment of other threads (obviously the reverse applies as well - you can lower the priority of other threads)

  • you can code your process to place threads in kernel wait mode when they don't have work to do in order to help the scheduler do it's job. When using proper kernel wait constructs such as Critical Sections, Mutexes, Semaphores, and Timers you effectively tell the kernel a certain thread doesn't need to be scheduled until a certain codition is met.

Note: There is rarely a reason you should tamper with task priorities so USE WITH CAUTION

like image 187
Mike Dinescu Avatar answered Oct 19 '22 09:10

Mike Dinescu


You might use 'fibers' instead of 'threads': for example there's a Win32 API named SwitchToFiber which lets you specify the fiber to be scheduled.

like image 29
ChrisW Avatar answered Oct 19 '22 09:10

ChrisW