#include <stdio.h>
#include <windows.h>
LPVOID fProducer , fConsumer , fMain;
int RawMaterial ;
int Queue ;
int MaxQueue ;
VOID CALLBACK FiberProc_Producer( PVOID lpParameter ){
while ( RawMaterial > 0 ){
while ( Queue < MaxQueue && RawMaterial > 0 ) {
Queue ++ ;
RawMaterial -- ;
puts("produce") ;
}
SwitchToFiber(fConsumer) ;
}
puts("raw material used up") ;
SwitchToFiber(fMain) ;
}
VOID CALLBACK FiberProc_Consumer( PVOID lpParameter ){
while (1){
while ( Queue > 0 ) {
Queue -- ;
puts("consume") ;
}
SwitchToFiber(fProducer) ;
}
}
int main(int argc, char *argv[])
{Queue = 0 ;
MaxQueue = 3 ;
RawMaterial = 10 ;
fProducer = CreateFiber(0 , FiberProc_Producer , NULL) ;
fConsumer = CreateFiber(0 , FiberProc_Consumer , NULL) ;
fMain = ConvertThreadToFiber(NULL) ;
SwitchToFiber(fConsumer) ;
puts("returned to main fiber") ;
getchar();
return 0;
}