简体   繁体   中英

c++ thread function clarification

#include <iostream>
#include <iostream>

static const int num_threads = 5000;

void thread_fun() {
   ...
}

int main() {
    std::thread t[num_threads];

    //Launch threads
    for (int i = 0; i < num_threads; ++i) {
        t[i] = std::thread(thread_fun);
    }

    //Join threads with the main thread
    for (int i = 0; i < num_threads; ++i) {
        t[i].join();
    }

    return 0;
}

When the 5000 threads start to run and execute void thread_fun() , how will the thread_fun() be handled?

  1. Will there only be one copy of thread_fun() in the stack of the main program where 5000 threads access it?

  2. Will 5000 copies of thread_fun() be created on the stack of the main program and each thread will have access to its own copy of thread_fun() ?

  3. Will thread_fun() be loaded into each of the 5000 threads' own stack and not in the main program stack?

I'm starting to learn c++ threads and was wondering on how this situation is handled.

Every thread has it's own stack and every time a thread calls a function, all the local variables (called from the inside of stack) and states will be separately stored inside of thread stack.

在此处输入图片说明

All the global and dynamically allocated variables are stored in separate sections of process memory.Note: Never call it program memory,since the memory is created when a program becomes a process, I mean when a program starts to run only then is this memory structure is created(See Image).

Now three questions may arise:

1) What if I call a global variable from the Thread ?

The answer to this is that all the global data is stored inside of the "Initialized Section" of the program memory.(See Image)

2) What if I dynamically allocate some data ?

The answer to this is that almost all the memory which is dynamically allocated (using malloc , new etc.) is stored inside of "heap" section of the program memory (See Image).This heap section can grow dynamically as it's not fix that how much memory will a program allocate during it's runtime. See the Image. Here our heap can go dynamically upwards.(Note:In some diagrams heap may be shown as growing dynamically downwards but it doesn't matter).

3) How is data stored in stack of Thread and is it dynamically allocated ?

Let's consider a normal function call. As a program calls a function, all the local variables it uses is stored inside of the "stack" portion of the process memory(See Image). This stack grows dynamically! Yes it does , the reason being that a function may execute some recursive calls and it's independent on our program/input how many recursive calls will be executed. Similar procedure is followed for the threads. As a thread is created, some part of the stack is reserved for the thread in form of "thread stack" in process memory(see Image).

Now let's answer your questions: Ans 1) No.Actually the way you asked he question is wrong. There won't be "one copy of thread_fun() in the stack of the main program". All the code of thread_fun() will be stored inside of "text" section of the process memory but will be called 5000 times. And every time it is called by a distinct thread, a new thread stack is created where all the values of the local variables and states are stored. Ans 2) Read Ans 1 Ans 3) Read Ans 2 :P

Please try to memorize and learn the stuff in Image and read books like Linux Programming Interface and http://pages.cs.wisc.edu/~remzi/OSTEP/ to make your concepts stronger. Hope that helps :)

Image is taken from the book Linux Programming Interface.

First of all, C++ threads are very thin (too thin in my own opinion) layer on OS threads - pthreads for Posix systems, or Windows threads for Windows. The question is not really about C++ threads, but about threads in general - and actually hardware architecture in general.

  1. Will there only be one copy of thread_fun() in the stack of the main program where 5000 threads access it?

This depends on what you mean by 'stack'. If you are talking about application stack memory, than the question does not make sense. Functions are not allocated on stack. Functions are code, and code actually can not be allocated this way at all. However, if by 'stack' you mean 'stack trace', than yes, 5000 threads will produce 5000 stack traces (when looked into with routine like pstack) and each of those 5000 stack traces will have thread_fun in it.

  1. Will 5000 copies of thread_fun() be created on the stack of the main program and each thread will have access to its own copy of thread_fun() ?

See above. Code will never be allocated on stack. There is only one instance of thread_fun CODE. There might be multiple instances of different data points manipulated by thread_fun .

  1. Will thread_fun() be loaded into each of the 5000 threads' own stack and not in the main program stack?

I do not understand this question.

Short answer to all those questions is no. Code is not stored on the stack. There is only one copy of thread_fun, it exists in a code segment. The OS will allocate internal data structures it will use to schedule the threads on the cpu (a stack per thread, thread local storage, storage for the registers and instruction pointer, etc).

What you have the the stack of main() is the array of std::thread type.

As you suspect, there will be one copy of thread_fun which will be executed by concurrent threads. The normal way to parameterise a thread function is to use a thread function object (such as the return type from std::bind ).

As for 5000 threads... you'll be lucky! 200 is pushing the limits on most systems. If you need that much concurrency you'll want to research the "asynchronous programming model".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM