Process and Thread Function ...


SwitchToThread Function

Causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the next thread to be executed.

Syntax

C++
BOOL WINAPI SwitchToThread(void);

Parameters

This function has no parameters.

Return Value

If calling the SwitchToThread function causes the operating system to switch execution to another thread, the return value is nonzero.

If there are no other threads ready to execute, the operating system does not switch execution to another thread, and the return value is zero.

Remarks

The yield of execution is in effect for up to one thread-scheduling time slice. After that, the operating system reschedules execution for the yielding thread. The rescheduling is determined by the priority of the yielding thread and the status of other threads that are available to run.

Note  The yield of execution is limited to the processor of the calling thread. The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority.

To compile an application that uses this function, define _WIN32_WINNT as 0x0400 or later. For more information, see Using the Windows Headers.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

Process and Thread Functions
SuspendThread
Suspending Thread Execution
Threads

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags :


Community Content

george_5154
Windows 2003 warning!
If you plan to use SwitchToThread inside a function that is going to be called heavily, and your program is going to run on Windows 2003 server (whatever flavor), you better be careful. SwitchToThread executes painfully slow on w2003. My benchmarks show that a program running under Windows XP can call a function that performs a useful job and has SwitchToTherad on it, 40 million times in less than 2 minutes, while the same process (running on the same hardware) will take almost 40 minutes on Windows 2003.

george_5154
Different Kernels???
By the way... the warning above also applies for Sleep(0).

Page view tracker