- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于在大约 64Mb RAM 的嵌入式系统中进行 C 并发编程的问题。特别是,我想减少线程使用的默认内存,所以我定义了:
pthread_attr_t attr_test;
size_t stacksize = 0x186A0; // 100Kbyte
pthread_attr_init(&attr_test);
pthread_attr_setdetachstate(&attr_test, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&attr_test, stacksize);
因此,当线程启动时,它仅使用 100Kbyte 的虚拟内存。但是当线程结束并调用pthread_exit时,进程使用的虚拟内存会迅速增加!......
为什么?我能做什么?谢谢!
更新:
主题 ->
void *thread_test(void *arg1) {
int *param;
param = (int*)arg1;
printf("Thread %d start\n", *param);
pthread_cond_wait(&condition[*param], &mutex[*param]);
printf("Thread %d stop\n",*param);
pthread_exit(0);
}
主要 ->
int main(void) {
pthread_t IDthread[MAX_THREADS];
int param[MAX_THREADS];
int pointer;
int i, keyb;
void *stkaddr;
size_t stacksize;
puts("!!! THREAD TEST !!!");
printf("Process ID %d\n\n", getpid());
for(i=0; i<MAX_THREADS; i++)
{
pthread_cond_init(&condition[i], NULL);
pthread_mutex_init(&mutex[i], NULL);
IDthread[i] = 0;
param[i] = i;
}
stacksize = 0x186A0; // 100Kbyte
pthread_attr_init(&attr_test);
pthread_attr_setdetachstate(&attr_test, PTHREAD_CREATE_DETACHED);
/* setting the size of the stack also */
pthread_attr_setstacksize(&attr_test, stacksize);
pointer = 0;
do {
keyb = getchar();
if (keyb == '1')
{
if (pointer < MAX_THREADS)
{
pthread_create(&IDthread[pointer], &attr_test, thread_test, ¶m[pointer]);
sleep(1);
pointer++;
}
else
puts("MAX Threads Number");
}
if (keyb == '2')
{
if (pointer != 0)
{
pointer--;
pthread_cond_signal(&condition[pointer]);
sleep(1);
}
else
puts("0 Thread is running");
}
} while (keyb != '0');
printf("FINE\n");
return EXIT_SUCCESS;
}
最佳答案
可连接或分离线程存在一个已知问题,引用手册:
Only when a terminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to the system
您可以使用以下方法使线程可拆卸:
pthread_attr_setdetachstate(3)
关于c++ - pthread_attr_setstacksize 和 pthread_exit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590858/
我有一个关于在大约 64Mb RAM 的嵌入式系统中进行 C 并发编程的问题。特别是,我想减少线程使用的默认内存,所以我定义了: pthread_attr_t attr_test; size_t st
我正在尝试调试一些关于堆栈使用的代码。我做了下面的测试程序(只是作为一个例子来弄清楚 pthread 库是如何工作的): #include #include #include #include
我刚才注意到,当我使用 pthread_create() 生成线程时,我为线程设置自定义堆栈大小的请求似乎被忽略了。特别是,如果我从生成的线程中调用 pthread_attr_getstacksize
我是一名优秀的程序员,十分优秀!