Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Windows核心编程:第12章 纤程

Windows核心编程:第12章 纤程

作者头像
gongluck
发布于 2019-02-22 01:49:11
发布于 2019-02-22 01:49:11
83100
代码可运行
举报
文章被收录于专栏:C++C++
运行总次数:0
代码可运行

Github

https://github.com/gongluck/Windows-Core-Program.git

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//第12章 纤程.cpp: 定义应用程序的入口点。
//

#include "stdafx.h"
#include "第12章 纤程.h"

LPVOID g_Covert = nullptr;
DWORD g_index = 0;

VOID WINAPI FiberFun(LPVOID lpFiberParameter)
{
    //纤程参数
    LPVOID pFiberCurrent = GetCurrentFiber();
    BOOL bres = IsThreadAFiber();
    LPVOID pCurrentData = GetFiberData();//获取创建纤程时传递的参数,pCurrentData==lpFiberParameter==555

    //纤程局部存储区
    bres = FlsSetValue(g_index, (PVOID)200);
    PVOID flsvalue = FlsGetValue(g_index);

    SwitchToFiber(g_Covert);
}

VOID NTAPI FlsFun(IN PVOID lpFlsData)
{
    //FlsSetValue多少次,系统调用FlsFun就多少次
    //可以主动调用FlsFree删除Fls槽
    //纤程销毁时也会调用FlsFun
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    //将线程转换为纤程
    LPVOID pFiberCovert = ConvertThreadToFiberEx(nullptr, FIBER_FLAG_FLOAT_SWITCH);
    g_Covert = pFiberCovert;

    //纤程局部存储区
    g_index = FlsAlloc(FlsFun);
    BOOL bres = FlsSetValue(g_index, (PVOID)100);
    PVOID flsvalue = FlsGetValue(g_index);

    //创建纤程
    LPVOID pFiberCreate = CreateFiberEx(0, 0, FIBER_FLAG_FLOAT_SWITCH, FiberFun, (LPVOID)555);

    //切换纤程
    SwitchToFiber(pFiberCreate);

    bres = FlsFree(g_index);
    
    //销毁纤程
    DeleteFiber(pFiberCreate);

    //解除线程的纤程状态
    bres = ConvertFiberToThread();

    system("pause");
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Windows核心编程:第13章 内存体系结构
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
4600
内存Exe加载,PEloder简单原理
而对于我们来说.其实就是 针对PE文件的 NT头 节表数据 重定位表 导入表 等进行操作.
IBinary
2020/06/29
2.3K1
内存Exe加载,PEloder简单原理
Windows核心编程:第11章 Windows线程池
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
1.2K0
Windows核心编程:第3章 内核对象
Github https://github.com/gongluck/Windows-Core-Program.git //第3章 内核对象.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第3章 内核对象.h" int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
gongluck
2018/06/22
3420
Windows核心编程:第2章 字符和字符串处理
Github https://github.com/gongluck/Windows-Core-Program.git //第2章 字符和字符串处理.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第2章 字符和字符串处理.h" #include "StrSafe.h" int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
gongluck
2018/06/22
4670
Windows核心编程:第6章 线程基础
Github https://github.com/gongluck/Windows-Core-Program.git //第6章 线程基础.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第6章 线程基础.h" #include <process.h> //线程函数 DWORD WINAPI ThreadProc(PVOID param) { return 0; } unsigned __stdcall ThreadProc2(void* p
gongluck
2018/06/22
4610
Windows核心编程:第8章 用户模式下的线程同步
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
5500
Windows核心编程:第14章 探索虚拟内存
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
4620
Windows核心编程:第10章 同步设备IO与异步设备IO
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
8490
Windows核心编程:第9章 用内核对象进行线程同步
https://github.com/gongluck/Windows-Core-Program.git
gongluck
2019/02/22
8370
Windows核心编程:第1章 错误处理
Github https://github.com/gongluck/Windows-Core-Program.git //第1章 错误处理.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第1章 错误处理.h" int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _
gongluck
2018/06/22
4070
Windows核心编程:第4章 进程
Github https://github.com/gongluck/Windows-Core-Program.git //第4章 进程.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第4章 进程.h" #include <shellapi.h> #pragma warning(disable:4996)//GetVersionEx int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
gongluck
2018/06/22
6230
Windows核心编程:第5章 作业
Github https://github.com/gongluck/Windows-Core-Program.git //第5章 作业.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第5章 作业.h" int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
gongluck
2018/06/22
8910
Windows核心编程:第7章 线程调度、优先级和关联性
Github https://github.com/gongluck/Windows-Core-Program.git //第7章 线程调度、优先级和关联性.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #include "第7章 线程调度、优先级和关联性.h" //线程函数 DWORD WINAPI ThreadProc(PVOID param) { return 0; } int APIENTRY wWinMain(_In_ HINSTANCE hInst
gongluck
2018/06/22
1.1K0
win32程序之子窗口编程
  在前边我们已经讲解了窗口的本质.以及如何注册窗口类跟创建窗口. 还讲了消息循环.
IBinary
2018/09/28
1.9K0
win32程序之子窗口编程
win32之进程概念
  学习WindowsAPI. 之前.我们必须理解什么是进程. 在windows环境下.进程就是一个运行起来的exe程序
IBinary
2018/09/28
8240
win32之进程概念
WINDOWS核心编程--Windows程序内部运行机制
现代的桌面应用基本上很少使用原始的 Windows API 进行开发了,因为使用原始 API 堆砌出来的应用代码逻辑非常繁琐,特别是窗口消息的处理非常不方便,大多数直接使用 C# 或者 QT 这种跨平台的开发库,而那种直接封装 Windows API 而存在的 MFC 早已半步入土。
20岁爱吃必胜客
2022/11/13
1.6K0
WINDOWS核心编程--Windows程序内部运行机制
使用CEF(二)— 基于VS2019编写一个简单CEF样例
在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进行修改配置和代码编写,并在这个过程中介绍vs使用过程中和C++项目的结合。源码见文章末尾Github链接。
w4ngzhen
2023/10/18
1.5K0
使用CEF(二)— 基于VS2019编写一个简单CEF样例
键盘钩子入门
钩子是操作系统消息处理的一种机制。通过钩子,应用程序可以安装一个钩子回调过程让系统调用,从而监视系统中的消息队列。在这些消息到达目标窗口之前对这些消息进行处理。
全栈程序员站长
2022/08/11
1K0
键盘钩子入门
【Win32】初识Win32编程
编译链接过程:将代码转换为机器语言,将生成的res文件和obj文件加上使用的库链接到一起,整合出一个exe文件,这是用编译器所感受不到的。
半生瓜的blog
2023/05/13
2.2K0
【Win32】初识Win32编程
推荐阅读
相关推荐
Windows核心编程:第13章 内存体系结构
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验