分类: C/C++
2009-06-17 22:21:53
看下这段代码:
#define _WIN32_WINNT 0x0500
#include
#include
#include
LPVOID P=NULL;
LPVOID V=NULL;
LPVOID M=NULL;
TCHAR chFood[256]={0};
VOID WINAPI Chinese_Foods_Provider(LPVOID Param)
{
__try
{
Param;
for(int i=0;i<10;i++)
{
lstrcpyn(chFood,_T("a heap of shit"),sizeof(chFood));
Sleep(200);
//转到第二个纤程,同样的直到Japanese_Customer调用SwitchToFiber(P)时才会返回
SwitchToFiber(V);
}
}
__except(1)
{
}
//切换为主纤程
SwitchToFiber(M);
}
VOID WINAPI Japanese_Customer(LPVOID Param)
{
__try
{
Param;
for(int i=0;i<10;i++)
{
_tprintf(_T("Japanese_Customer:I'm eating %s\n"),chFood);
memset(chFood,0,sizeof(chFood));
SwitchToFiber(P);//切换到Chinese_Foods_Provider
}
}
__except(1)
{
}
}
int main(void)
{
//创建两个纤程
P=CreateFiber(4096,Chinese_Foods_Provider,NULL);
V=CreateFiber(4096,Japanese_Customer,NULL);
//主线程设置纤程信息
M=ConvertThreadToFiber(NULL);
//转到第一个纤程,这个调用直到有人调用SwitchToFiber(M)时才返回
SwitchToFiber(P);
DeleteFiber(P);
DeleteFiber(V);
return 0;
}