NT Fiber
A minimal C++ program that converts the main thread to a fiber, creates a second fiber, and switches between them.
The smallest complete fiber example on Windows. ConvertThreadToFiber turns the calling thread into fiber zero so you have something to switch back to; CreateFiber allocates a one-megabyte stack for the new fiber and gives it an entry point; and SwitchToFiber does the cooperative context swap in both directions - here the new fiber immediately switches control back to the original, which then deletes it. Fibers are user-mode coroutines: no kernel involvement, no preemption, just explicit hand-offs. Useful for structured concurrency inside one thread (state machines, simple task interleaving) where threads would be overkill.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define _WIN32_WINNT 0x0500
#include <Windows.H>
#include <WINBase.H>
#include <StdIO.H>
#include <StdLib.H>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VOID CALLBACK MyNTFiberProc(PVOID CallingNTFiber)
{
SwitchToFiber(CallingNTFiber);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
LPVOID NewNTFiberAddress = NULL;
LPVOID ThisNTFiber = NULL;
ThisNTFiber = ConvertThreadToFiber(NULL);
NewNTFiberAddress = CreateFiber(1048576, &MyNTFiberProc, ThisNTFiber);
if(ThisNTFiber == NULL || ThisNTFiber == NULL)
return 0;
SwitchToFiber(NewNTFiberAddress);
DeleteFiber(NewNTFiberAddress);
return 0;
}
Two things to fix before reusing: the null check compares ThisNTFiber twice instead of also checking NewNTFiberAddress, and on 64-bit builds the fiber APIs are ...Ptr variants (ConvertThreadToFiberEx/CreateFiberEx) since the handles are pointers, not 32-bit values.