线程的堆栈——Windows核心编程学习手札之十六
线程的堆栈
——Windows核心编程学习手札之十六
系统会在进程的地址空间内保存一些区域,同时也会在进程地址空间内为线程的堆栈保留区域。线程都有自己的堆栈,创建时,系统就保留一个堆栈空间区域,并将相应物理存储器提交给这个保留的区域用于线程堆栈。按照默认设置,系统保留1MB的地址空间并提交两个页面的内存,默认值可以在链接应用程序时设定Microsoft的链接程序的/STACK选项:
/STACK:reserve[,commit]
当创建一个线程的堆栈时,系统就会保留一个链接程序/STACK开关指明的地址空间区域。在调用CreateThread或_beginthreadx函数时,可以重载原先提交的内存数量,这两个函数都有一个参数,可以用来重载原先提交给堆栈地址空间的内存数量,如果设定这个参数为0,那么系统将使用/STACK开关指明的已提交的堆栈大小值。
线程堆栈溢栈例子,超过1MB分配空间,SHE提示出错:
// Demo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <process.h>
#define MAX_LOADSTRING 100
#define UINT_MAX 4
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//Function:Sum
//An example of calling Sum for uNum=0 through 9
//uNum:0 1 2 3 4 5 6 7 8 9 ...
//Sum: 0 1 3 6 10 15 21 28 36 45 ...
UINT Sum(UINT nNum)
{
// call Sum recursively.
return ( (nNum ==0)?0:(nNum+Sum(nNum-1)) );
}
//end of Sum
//Function:FilterFunc
LONG WINAPI FilterFunc(DWORD dwExceptionCode)
{
return ((dwExceptionCode==STATUS_STACK_OVERFLOW)?
EXCEPTION_EXECUTE_HANDLER:EXCEPTION_CONTINUE_SEARCH);
}
//end of FilterFunc
//Function:SumThreadFunc
//the separate thread that is responsible for calculating the sum.
unsigned int WINAPI SumThreadFunc(PVOID pvParam)
{
//the parameter pvParam,contains the number of integers to sum.
UINT uSumNum=PtrToUlong(pvParam);
//uSum contains the summation of the numbers from 0 through uSumNum.
//if the sum cannot be calculated,a sum of UINT_MAX is returned.
UINT uSum=UINT_MAX;
__try{
uSum=Sum(uSumNum);
}
__except(FilterFunc(GetExceptionCode())){
}
return(uSum);
}
//end of SumThreadFunc
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DEMO);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DEMO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_DEMO;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_CALC:
{
UINT uSum=44000;
unsigned dwThreadId;
HANDLE hThread=(HANDLE)_beginthreadex(NULL,0,SumThreadFunc,(PVOID)(UINT_PTR)uSum,0,&dwThreadId);
//wait for the thread to terminate.
WaitForSingleObject(hThread,INFINITE);
//the thread's exit code is the resulting summation.
GetExitCodeThread(hThread,(PDWORD)&uSum);
CloseHandle(hThread);
if(uSum==UINT_MAX)
MessageBox(hWnd,"error","warning",0);
else
MessageBox(hWnd,"Success","warning",0);
}
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
线程的堆栈——Windows核心编程学习手札之十六相关推荐
- 堆栈——Windows核心编程学习手札之十八
堆栈 --Windows核心编程学习手札之十八 堆栈可以用来分配许多较小的数据块,可以不考虑分配粒度和页面边界之类的问题,但分配和释放内存块的速度比其他机制要慢,并且无法直接控制物理存储器的提交和回收 ...
- 线程本地存储器——Windows核心编程学习手札之二十一
线程本地存储器 --Windows核心编程学习手札之二十一 C/C++运行期库使用线程本地存储器,运行期库是在多线程应用程序出现前设计的,因此运行期库里的大多数函数是用于单线程应用程序的.函数strt ...
- 用户方式中线程的同步——Windows核心编程学习手札之八
用户方式中线程的同步 --Windows核心编程学习手札之八 系统中所有线程都必须拥有对各种系统资源的访问权,这些资源包括内存堆栈.串口.文件.窗口和许多其他资源.如果一个线程需要独占对资源的访问权, ...
- 线程基础知识——Windows核心编程学习手札系列之六
线程基础知识 --Windows核心编程学习手札系列之六 线程与进程一样由两部分构成:一是线程的内核对象,操作系统用它来对线程实施管理,也是系统用来存放线程统计信息的地方:二是线程堆栈,用于维护线程在 ...
- 在应用程序中使用虚拟内存——Windows核心编程学习手札之十五
在应用程序中使用虚拟内存 --Windows核心编程学习手札之十五 Windows提供了3种进行内存管理的方法: 1) 虚拟内存,最适合用来管理大量对象或结构数组: 2) 内存映射文件,最适合用来 ...
- DLL基础——Windows核心编程学习手札之十九
DLL基础 --Windows核心编程学习手札之十九 Windows API中的所有函数都包含在DLL中,3个最重要的DLL是Kernel32.dll,它包含用于管理内存.进程和线程的各个函数:Use ...
- 虚拟内存——Windows核心编程学习手札之十四
虚拟内存 --Windows核心编程学习手札之十四 系统信息 有些操作系统的值是根据主机而定的,如页面大小.分配粒度大小等,这些值不用硬编码形式,进程初始化时应检索这些值以使用.函数GetSystem ...
- 线程的调度、优先级和亲缘性——Windows核心编程学习手札系列之七
线程的调度.优先级和亲缘性 --Windows核心编程学习手札系列之七 每个线程都拥有一个上下文结构,在线程的内核对象中,记录线程上次运行时该线程的CPU寄存器状态.Windows会每隔20ms左右查 ...
- 线程与内核对象的同步——Windows核心编程学习手札之九
线程与内核对象的同步 --Windows核心编程学习手札之九 用户方式下的线程同步机制具有速度快的特点,但有其局限性,对于许多应用程序来说,并不合适.例如,互锁函数家族只能在单值上运行,根本无法使线程 ...
最新文章
- MIT自然语言处理第五讲:最大熵和对数线性模型(第一部分)
- mongodb安装_MongoDB:GridFS删除方法删除存储桶中的所有文件
- php调http接口,2步教你解决http协议php接口调用
- 点击按钮测试用例标题_功能测试有哪些用例?分享功能测试用例大全
- maven引用公共包_使用github作为maven仓库存放发布自己的jar包依赖 实现多个项目公共部分代码的集中,避免团队中多个项目之间代码的复制粘贴...
- php苹果app微信支付 无法返回,微信支付,php_微信支付APP返回-1怎么解决,微信支付,php,移动app - phpStudy...
- java JDK8 学习笔记——第13章 时间与日期
- java persistence.xml_java – 找不到persistence.xml
- mysql主从同步完整命令
- 黑鹰安全网_育鹰计划_笔记
- APP界面设计规范:如何定义视觉规范
- 发光二极管之—工作原理图解分析
- 关于本号,你想看的都在这里
- lisp画弯箭头_在CAD中直接画箭头的命令的一个方法
- Linux——挂载硬盘
- 2021年中式烹调师(中级)考试题及中式烹调师(中级)找解析
- 获得lazada商品详情
- 投资中的N种知差有款败
- 【引言集】韩愈:师说
- 以下哪一个不属于python语言的特点-【单选题】下面的4个特点,Python不具备的是?...
热门文章
- python pickle反序列化漏洞_Python Pickle反序列化带来的安全问题
- IIS发生意外错误0x8ffe2740
- Codeforces Round #462 (Div. 2)题解
- Storm Trident API
- 你不知道的Javascript之原型
- [国嵌攻略][080][无名管道通讯]
- Opencv学习笔记(六)SURF学习笔记
- 阶段-关系系统-stage1范围界定阶段---学习记录
- Android设备的网络抓包
- MTK radiooptions 分析