线程的堆栈
——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;
}
分享到:
相关推荐
分享Perl_学习手札书籍,供大家学习、参考及交流perl。
打造魅力组合——在iBook 上安装Linux手札.pdf
perl学习手札,共同学习,共同进步。希望你能很快掌握perl。
关于作者: 简信昌 “傲尔网”专案经理 博仲法律事务所资讯部门 台北Perl推广组 (Taipei.pm) 召集人 Newzilla召集人 目前专案: Open Source Foundry (OSSF) Newzilla 线上杂志 ...“Perl学习手札”
Perl学习手札.chmPerl学习手札.chmPerl学习手札.chm
1-高级perl编程(黑豹书).pdf 2-Perl_学习手札.pdf
Perl 学习手札,这本书介绍了perl的基本语法及perl的一些用法,想了解perl的就看一看
hibernate学习手札.z03
Spring 注解学习手札,个人学习Spring mvc的知识总结
Perl 学习手札,好东西
hibernate学习手札.z01
NULL 博文链接:https://snowolf.iteye.com/blog/1628861
本资料完全来源于网上收集,对于作者并没有做过多的考证,考虑到作者本身,所以保持原作品不变只是略加整理,为用户提供一个方便的浏览方式。
非常著名的perl学习手札(初学者有福了),本文档是精美的PDF版,排版精美,是网络上到现在为止能找到的最好的。 全书共分17个小节,通过讲解实例来学习perl,你可以复制这些代码,然后执行。学习非常方便!
8086汇编学习手札及各章节检测题详细答案。 配套书籍 王爽的汇编
这个事单片机资源开发过程的学习手记,自己的学习心得希望对广大学员有好处
(数据科学学习手札139)geopandas 0.11版本重要新特性一览.doc
Perl是由Larry Wall设计的,并由他不断更新和维护的编程语言。 .Perl具有高级语言(如C)的强大能力和灵活性。事实上,你将看到,它的许多特性是从C语言中借用来的。 .与脚本语言一样,Perl不需要编译器和链接器...
使用盟威Access快速开发平台可以快速...本文档是学习“[张志MVP]Access快速开发一日通”视频教程做的笔记,主要记录教程中的关键点以及学习过程中的感悟。这个教程就是教你如何使用这个开发平台快速开发出一款应用。