3 out of 3 rated this helpful - Rate this topic

SwitchToThread function

Applies to: desktop apps only

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

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 on 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.

After the yielding thread's time slice elapses, 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 that the operating system will not switch to a thread that is being prevented from running only by concurrency control. For example, an I/O completion port or thread pool limits the number of associated threads that can run. If the maximum number of threads is already running, no additional associated thread can run until a running thread finishes. If a thread uses SwitchToThread to wait for one of the additional associated threads to accomplish some work, the process might deadlock.

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 client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Process and Thread Functions
SuspendThread
Suspending Thread Execution
Threads

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
erroneous
george_5154 : Either your hardware is defective or your code is erroneous - that is for sure. Just look at the relationship of the times : Millions of times / 2 minutes against millions of times / FORTY minutes ... nothing in the WINAPI is this inefficient, even with activated UAC
Different Kernels???
By the way... the warning above also applies for Sleep(0).
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.