threads stack in linux c

Hi,
I have following doubts about stack.

1. i am able to change stack using pthread_attr_setstacksize. when i try to set 1Gbyte stack size, then it is giving segment fault. why am i getting segment fault.


2. i have seen that i can generate upto 255 threads in a main routine. if i try to generate 256 threads, then it will show more than 256 thread some other figure(381). why it is showing more exact no of thread i am generating.


can i anyone help to clear why is it behaving like this?

----
Learner


Why would you want a 1G stack? Are you familar with the term "scarce resource"?

What is showing 381 threads?
is it possible to increase stack to 1G, if have enough RAM? if yes, how can i increase, as i am understanding, as thread is created inside process, so thread can have max process stack. it means, i need to increase process stack. how can i increase process stack? i am not familar with scarce resource.



I saw using /proc/(pid of process)/status, it shows, no of threads created under task. why it is not showing proper number of threads, when i go beyond 256 threads.


Stacks are per-thread, not per-process, virtual address space is per-process (which is why you won't get anywhere with 1GB stacks on a 32-bit system).

With a 64-bit system, you shouldn't have any problems, do you have a test program?
Here's mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/thread.hpp>

const int N = 300; // 300 threads to create

boost::barrier ready(N+1), done(N+1); // +1 for the main thread

void thread_func()
{
    ready.wait();
    done.wait();
}
int main()
{
    boost::thread::attributes attr;
    attr.set_stack_size(1024*1024*1024); // 1 Gb stack
    std::vector<boost::thread> v;
    for(int n = 0; n < N; ++n) 
        v.emplace_back(attr, thread_func);

    ready.wait(); // wait for all threads to be running

    std::ifstream stat("/proc/self/status");
    for(std::string line; std::getline(stat, line); )
        if(line.find("Threads:") != std::string::npos)
                std::cout << line << '\n';
        else if(line.find("VmSize:") != std::string::npos)
                std::cout << line << '\n';

    done.wait(); // tell the threads they can shut down

    for(auto& t: v)
        t.join();
}

output:
 $ ./test
VmSize: 318761172 kB
Threads:        301


(PS: just noticed this thread is for C.. it's the same way, just more to write)
Last edited on
If you think you need 1G of stack in any context, you should realise you're headed down the wrong path. Remember, you can consider thread local storage for per thread data. Or a thread class will often provide an equivalent facility.

But just because you think you can aks for 1G of stack in any context, doesn't mean you should.
Just to add to Cubbi's point, in a 32 bit system you will get 4 GB of virtual address space in which child threads would be created. And if you increase the stack size for thread to 1 GB then beyond 4 GB of memory space for process will cause problem.
In 64 bit systems we can increase the datasegments and achieve more size in virtual space. In AIX we can increase upto 8 datasegments even though default data segment size is 2. I think it is not possible in 32 bit systems.

1. http://publib.boulder.ibm.com/infocenter/realtime/v2r0/index.jsp?topic=%2Fcom.ibm.softrt.aix32.doc%2Fdiag%2Fproblem_determination%2Faix_mem_32.html

2. http://publib.boulder.ibm.com/infocenter/realtime/v2r0/index.jsp?topic=%2Fcom.ibm.softrt.aix32.doc%2Fdiag%2Fproblem_determination%2Faix_mem_32.html
Topic archived. No new replies allowed.