先來(lái)講說(shuō)線程內(nèi)存相關(guān)的東西,主要有下面幾條:
- 進(jìn)程中的所有的線程共享相同的地址空間。
- 任何聲明為static/extern的變量或者堆變量可以被進(jìn)程內(nèi)所有的線程讀寫(xiě)。
- 一個(gè)線程真正擁有的唯一私有儲(chǔ)存是處理器寄存器。
- 線程棧可以通過(guò)暴露棧地址的方式與其它線程進(jìn)行共享。
有大數(shù)據(jù)量處理的應(yīng)用中,有時(shí)我們有必要在??臻g分配一個(gè)大的內(nèi)存塊或者要分配很多小的內(nèi)存塊,但是線程的??臻g的最大值在線程創(chuàng)建的時(shí)候就已經(jīng)定下來(lái)了,如果棧的大小超過(guò)個(gè)了個(gè)值,系統(tǒng)將訪問(wèn)未授權(quán)的內(nèi)存塊,毫無(wú)疑問(wèn),再來(lái)的肯定是一個(gè)段錯(cuò)誤??墒菦](méi)辦法,你還是不得不分配這些內(nèi)存,于是你開(kāi)會(huì)為分配一個(gè)整數(shù)值而動(dòng)用malloc這種超級(jí)耗時(shí)的操作。當(dāng)然,在你的需求可以評(píng)估的情況下,你的需求還是可以通過(guò)修改線程的棧空間的大小來(lái)改變的。
下面的我們用pthread_attr_getstacksize和pthread_attr_setstacksize的方法來(lái)查看和設(shè)置線程的??臻g。
注意:
下面的測(cè)試代碼在我自己的機(jī)子上(ubuntu6.06,ubuntu6.10,redhat 9,
gentoo)通過(guò)了測(cè)試,但是很奇怪的是在我同事的機(jī)子上,無(wú)論是改變環(huán)境,還是想其它方法都不能正常的運(yùn)行
。在網(wǎng)上查了一下,很多人也存在同樣的問(wèn)題,至今不知道為何。
linux線程的實(shí)現(xiàn)方式?jīng)Q定了對(duì)進(jìn)程的限制同樣加在了線程身上:)所以,有問(wèn)題,請(qǐng)參見(jiàn)<pthread之線程棧空間(2)(進(jìn)行棧)
直接看代碼吧,只有一個(gè)C文件(thread_attr.c)
#include <limits.h>
#include <pthread.h>
#include "errors.h"
//線程體,在棧中分配一個(gè)大小為15M的空間,并進(jìn)行讀寫(xiě)
void *thread_routine (void *arg)
{
printf ("The thread is here\n");
//棧大小為16M,如果直接分配16M的??臻g,會(huì)出現(xiàn)段錯(cuò)誤 ,因?yàn)闂V羞€有其它的
//變量要放署
char p[1024*1024*15];
int i=1024*1024*15;
//確定內(nèi)存分配的確成功了
while(i--)
{
p[i] = 3;
}
printf( "Get 15M Memory!!!\n" );
//分配更多的內(nèi)存(如果分配1024*1020*512的話就會(huì)出現(xiàn)段錯(cuò)誤)
char p2[ 1024 * 1020 + 256 ];
memset( p2, 0, sizeof( char ) * ( 1024 * 1020 + 256 ) );
printf( "Get More Memory!!!\n" );
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t thread_id;
pthread_attr_t thread_attr;
size_t stack_size;
int status;
status = pthread_attr_init (&thread_attr);
if (status != 0)
err_abort (status, "Create attr");
status = pthread_attr_setdetachstate (
&thread_attr, PTHREAD_CREATE_DETACHED);
if (status != 0)
err_abort (status, "Set detach");
//通常出現(xiàn)的問(wèn)題之一,下面的宏沒(méi)有定義
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
//得到當(dāng)前的線程棧大小
status = pthread_attr_getstacksize (&thread_attr, &stack_size);
if (status != 0)
err_abort (status, "Get stack size");
printf ("Default stack size is %u; minimum is %u\n",
stack_size, PTHREAD_STACK_MIN);
//設(shè)置當(dāng)前的線程的大小
status = pthread_attr_setstacksize (
&thread_attr, PTHREAD_STACK_MIN*1024);
if (status != 0)
err_abort (status, "Set stack size");
//得到當(dāng)前的線程棧的大小
status = pthread_attr_getstacksize (&thread_attr, &stack_size);
if (status != 0)
err_abort (status, "Get stack size");
printf ("Default stack size is %u; minimum is %u\n",
stack_size, PTHREAD_STACK_MIN);
#endif
int i = 5;
//分配5個(gè)具有上面的屬性的線程體
while(i--)
{
status = pthread_create (
&thread_id, &thread_attr, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
}
getchar();
printf ("Main exiting\n");
pthread_exit (NULL);
return 0;
}
看看執(zhí)行過(guò)程:
dongq@DongQ_Lap
~/workspace/test/pthread_attr $ make
cc -pthread -g -DDEBUG -lrt -o
thread_attr thread_attr.c
dongq@DongQ_Lap ~/workspace/test/pthread_attr $
./thread_attr
Default stack size is 8388608; minimum is 16384
//默認(rèn)的棧大小為8M
Default stack size is 16777216; minimum is 16384
//設(shè)置后的結(jié)果為16M
The thread is here
The thread is here
The thread is
here
The thread is here
The thread is here
Get 15M Memory!!!
Get
More Memory!!!
Get 15M Memory!!!
Get More Memory!!!
Get 15M
Memory!!!
Get 15M Memory!!!
Get More Memory!!!
Get More
Memory!!!
Get 15M Memory!!!
Get More Memory!!!
Main exiting