Windows 核心编程 12 纤程

上代码:


// FiberThread.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define _WIN32_WINNT _WIN32_WINNT_LONGHORN 
#define WINVER       _WIN32_WINNT_LONGHORN 

#include <windows.h>
#include <stdio.h>
#include <winbase.h>




DWORD gdwOne = 0;

//纤程的执行函数 最后需要切换纤程
VOID CALLBACK FiberProc(  PVOID lpParameter)
{
	PVOID pFiberMain = lpParameter;

	FlsSetValue(gdwOne,pFiberMain);

	SwitchToFiber(pFiberMain);

}

VOID NTAPI FLS_Callback_function ( IN PVOID lpFlsData )
{
	printf("Fls function\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
	DWORD dMain = 100;
	DWORD dOtherFiber = 1000;

	//线程转换为纤程
	PVOID pFibermain = ConvertThreadToFiber(&dMain);

	//局部存储
	gdwOne = FlsAlloc(FLS_Callback_function);

	//创建一个纤程
	PVOID pFiberCounter = CreateFiberEx(0,0,FIBER_FLAG_FLOAT_SWITCH, FiberProc, pFibermain);

	// 纤程之间的局部存储是独立的
	BOOL brs = FlsSetValue(gdwOne,"1111111111");

	SwitchToFiber(pFiberCounter);

	//删除一个纤程
	DeleteFiber(pFiberCounter);

	pFiberCounter = NULL;

	PVOID pFlsData = FlsGetValue(gdwOne);

	// 创建一个纤程
	pFiberCounter = CreateFiber(0, FiberProc, pFibermain);

	SwitchToFiber(pFiberCounter);

	PCTSTR pStr =(PCTSTR) FlsGetValue(gdwOne);

	FlsFree(gdwOne);

	ConvertFiberToThread();

	getchar();

	return 0;
}


你可能感兴趣的:(windows,核心编程)