Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the stacksize with C++11 std::thread

I've been trying to familiarize myself with the std::thread library in C++11, and have arrived at a stumbling block.

Initially I come from a posix threads background, and was wondering how does one setup the stack size of the std::thread prior to construction, as I can't seem to find any references to performing such a task.

Using pthreads setting the stack size is done like this:

void* foo(void* arg); . . . . pthread_attr_t attribute; pthread_t thread;  pthread_attr_init(&attribute); pthread_attr_setstacksize(&attribute,1024); pthread_create(&thread,&attribute,foo,0); pthread_join(thread,0); 

Is there something similar when using std::thread?

I've been using the following reference:

http://en.cppreference.com/w/cpp/thread

like image 936
Zamfir Kerlukson Avatar asked Dec 14 '12 02:12

Zamfir Kerlukson


People also ask

How do you initialize a thread in CPP?

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable.

Does std :: thread use Pthread?

The std::thread library is implemented on top of pthreads in an environment supporting pthreads (for example: libstdc++).

Can a thread spawn another thread C++?

A thread does not operate within another thread. They are independent streams of execution within the same process and their coexistence is flat, not hierarchical. Some simple rules to follow when working with multiple threads: Creating threads is expensive, so avoid creating and destroying them rapidly.

How do you manage threads in CPP?

Basic thread management. Every C++ program has at least one thread, which is started by the C++ runtime: the thread running main() . Your program can then launch additional threads that have another function as the entry point. These threads then run concurrently with each other and with the initial thread.


1 Answers

Initially I come from a posix threads background, and was wondering how does one setup the stack size of the std::thread prior to construction, as I can't seem to find any references to performing such a task.

You can't. std::thread doesn't support this because std::thread is standardized, and C++ does not require that a machine even has a stack, much less a fixed-size one.

pthreads are more restrictive in terms of the hardware that they support, and it assumes that there is some fixed stack size per thread. (So you can configure this)

like image 91
Billy ONeal Avatar answered Sep 20 '22 12:09

Billy ONeal