关于线程堆栈在Linux和Windows中的默认大小

「已注销」 2021-03-24 03:56:11
小弟在学多线程,了解到<pthread.h>中 pthread_attr_setstacksize获取线程堆栈大小,就分别在Linux,Windows下试了下,Linux是8192kb,结果到了windows下成0了。找到POSIX for Win32文档里写win设计的是0 ,但是又找到资料说win32api创建的线程堆栈大小是1M,想问这样设计有什么用意?为什么Windows实现的POSIX标准和win32api的内容不设计成一致的呢?
...全文
417 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2021-03-24
  • 打赏
  • 举报
回复
消化zhao4老师的话ing...
赵4老师 2021-03-24
  • 打赏
  • 举报
回复
/STACK (Stack Allocations) Home | Overview | How Do I | Linker Options The Stack Allocations (/STACK:reserve[,commit]) option sets the size of the stack in bytes. To find this option in the development environment, click Settings on the Project menu. Then click the Link tab, and click Output in the Category box. The Reserve text box (or in the reserve argument on the command line) specifies the total stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds up the specified value to the nearest 4 bytes. The optional value specified in the Commit text box (or in the commit argument on the command line) is subject to interpretation by the operating system. In Windows NT, it specifies the amount of physical memory to allocate at a time. Committed virtual memory causes space to be reserved in the paging file. A higher commit value saves time when the application needs more stack space, but increases the memory requirements and possibly the startup time. Specify the reserve and commit values in decimal or C-language notation. Another way to set the size of the stack is with the STACKSIZE statement in a module-definition (.DEF) file. STACKSIZE overrides the Stack Allocations (/STACK) option if both are specified. You can change the stack after the .EXE file is built by using the EDITBIN tool.
赵4老师 2021-03-24
  • 打赏
  • 举报
回复
_beginthread, _beginthreadex Create a thread. unsigned long _beginthread( void( __cdecl *start_address )( void * ), unsigned stack_size, void *arglist ); unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr ); Routine Required Header Compatibility _beginthread <process.h> Win 95, Win NT _beginthreadex <process.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version To use _beginthread or _beginthreadex, the application must link with one of the multithreaded C run-time libraries. Return Value If successful, each of these functions returns a handle to the newly created thread. _beginthread returns –1 on an error, in which case errno is set to EAGAIN if there are too many threads, or to EINVAL if the argument is invalid or the stack size is incorrect. _beginthreadex returns 0 on an error, in which case errno and doserrno are set. Parameters start_address Start address of routine that begins execution of new thread stack_size Stack size for new thread or 0 arglist Argument list to be passed to new thread or NULL security Security descriptor for new thread; must be NULL for Windows 95 applications initflag Initial state of new thread (0 for running or CREATE_SUSPEND for suspended) thrdaddr Points to a 32-bit variable that receives the thread identifier Remarks The _beginthread function creates a thread that begins execution of a routine at start_address. The routine at start_address must use the __cdecl calling convention and should have no return value. When the thread returns from that routine, it is terminated automatically. _beginthreadex resembles the Win32 CreateThread API more closely than does _beginthread. _beginthreadex differs from _beginthread in the following ways: _beginthreadex has three additional parameters: initflag, security, threadaddr. The new thread can be created in a suspended state, with a specified security (Windows NT only), and can be accessed using thrdaddr, which is the thread identifier. The routine at start_address passed to _beginthreadex must use the __stdcall calling convention and must return a thread exit code. _beginthreadex returns 0 on failure, rather than –1. A thread created with _beginthreadex is terminated by a call to _endthreadex. You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread. _endthread automatically closes the thread handle (whereas _endthreadex does not). Therefore, when using _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32 CloseHandle API. This behavior differs from the Win32 ExitThread API. Note For an executable file linked with LIBCMT.LIB, do not call the Win32 ExitThread API; this prevents the run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then call ExitThread. The operating system handles the allocation of the stack when either _beginthread or _beginthreadex is called; you do not need to pass the address of the thread stack to either of these functions. In addition, the stack_size argument can be 0, in which case the operating system uses the same value as the stack specified for the main thread. arglist is a parameter to be passed to the newly created thread. Typically it is the address of a data item, such as a character string. arglist may be NULL if it is not needed, but _beginthread and _beginthreadex must be provided with some value to pass to the new thread. All threads are terminated if any thread calls abort, exit, _exit, or ExitProcess. Example /* BEGTHRD.C illustrates multiple threads using functions: * * _beginthread _endthread * * * This program requires the multithreaded library. For example, * compile with the following command line: * CL /MT /D "_X86_" BEGTHRD.C * * If you are using the Visual C++ development environment, select the * Multi-Threaded runtime library in the compiler Project Settings * dialog box. * */ #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h> void Bounce( void *ch ); void CheckKey( void *dummy ); /* GetRandom returns a random integer between min and max. */ #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) BOOL repeat = TRUE; /* Global repeat flag and video variable */ HANDLE hStdOut; /* Handle for console window */ CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */ void main() { CHAR ch = 'A'; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); /* Get display screen's text row and column information. */ GetConsoleScreenBufferInfo( hStdOut, &csbi ); /* Launch CheckKey thread to check for terminating keystroke. */ _beginthread( CheckKey, 0, NULL ); /* Loop until CheckKey terminates program. */ while( repeat ) { /* On first loops, launch character threads. */ _beginthread( Bounce, 0, (void *) (ch++) ); /* Wait one second between loops. */ Sleep( 1000L ); } } /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */ void CheckKey( void *dummy ) { _getch(); repeat = 0; /* _endthread implied */ } /* Bounce - Thread to create and and control a colored letter that moves * around on the screen. * * Params: ch - the letter to be moved */ void Bounce( void *ch ) { /* Generate letter and color attribute from thread argument. */ char blankcell = 0x20; char blockcell = (char) ch; BOOL first = TRUE; COORD oldcoord, newcoord; DWORD result; /* Seed random number generator and get initial location. */ srand( _threadid ); newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 ); newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 ); while( repeat ) { /* Pause between loops. */ Sleep( 100L ); /* Blank out our old position on the screen, and draw new letter. */ if( first ) first = FALSE; else WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result ); WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result ); /* Increment the coordinate for next placement of the block. */ oldcoord.X = newcoord.X; oldcoord.Y = newcoord.Y; newcoord.X += GetRandom( -1, 1 ); newcoord.Y += GetRandom( -1, 1 ); /* Correct placement (and beep) if about to go off the screen. */ if( newcoord.X < 0 ) newcoord.X = 1; else if( newcoord.X == csbi.dwSize.X ) newcoord.X = csbi.dwSize.X - 2; else if( newcoord.Y < 0 ) newcoord.Y = 1; else if( newcoord.Y == csbi.dwSize.Y ) newcoord.Y = csbi.dwSize.Y - 2; /* If not at a screen border, continue, otherwise beep. */ else continue; Beep( ((char) ch - 'A') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); } Process and Environment Control Routines See Also _endthread, abort, exit
flying_music 2021-03-24
  • 打赏
  • 举报
回复
0肯定不是实际值,要不线程怎么跑啊,应该是不支持这个接口吧,所以0值没啥意义 至于为啥不一致。。。既然可以修改,那默认值也没必要一致吧,自己根据自己的系统来就行,标准一般不会规定固定的值,最多给个下限
赵4老师 2021-03-24
  • 打赏
  • 举报
回复
理解讨论之前请先学会如何观察! 计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 多用小脑和手,少用大脑、眼睛和嘴,会更快地学会编程! 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步Debug版对应汇编一行! 单步Debug版对应汇编千行不如单步Release版对应汇编一行! 不会单步Release版对应汇编?在你想单步Release版C/C++代码片断的前面临时加一句DebugBreak();重建所有,然后在IDE中运行。(一般人我不告诉他!单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
赵4老师 2021-03-24
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
赵4老师 2021-03-24
  • 打赏
  • 举报
回复
微软为什么要Niao POSIX标准?

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧