3

Is the following code is compile time recursive? I would like to know how to confirm this i.e any debugger,profiler etc to understand template programs.

#include <iostream>
#include <vector>
#include <thread>
std::vector<std::thread> vecc;

void thread_fn(){
    std::cout<<"Thread function"<<"\n"; 
}

template <int n>
void create_thread(){
    create_thread<n-1>();
    vecc.push_back(std::thread(thread_fn));
}
template<>
void create_thread<0>(){
    vecc.push_back(std::thread(thread_fn));
}

int main()
{
    create_thread<10>();
    for(auto &a: vecc){
        a.join();
    }
}
Sivaram
  • 155
  • 1
  • 8
  • What is your question? – Passer By Nov 29 '17 at 11:01
  • Despite what you're asking, I don't think threads can be spawned at compile time. – Francisco Gallego Salido Nov 29 '17 at 11:03
  • 1
    The generated code for `create_thread<10>()` has the same meaning as 10 lines of `vecc.push_back(std::thread(thread_fn))`, if that's what you are asking – Caleth Nov 29 '17 at 11:09
  • Related (but more broad) https://stackoverflow.com/questions/7325910/debugging-template-instantiations – grek40 Nov 29 '17 at 11:18
  • The templates are recursive and templates are instantiated at compile-time, so... – molbdnilo Nov 29 '17 at 11:51
  • I wanted to know if the function create_thread will be called recursively in run-time resulting in growth of stack or compiler will generate the code for create_thread 10 times. The answer below clarifies that. – Sivaram Nov 29 '17 at 12:31
  • @SivaramL the answer would be kindof "*both*"... it's not real recursion, but its still 10 nested function calls with growing stack. – grek40 Nov 29 '17 at 13:23
  • @grek40 I could not understand it fully, can you please explain? – Sivaram Dec 01 '17 at 04:36
  • @Sivaram well the compiler generates the code for `create_thread` 10 times, but still this code will contain a nested function call in 9 out of 10 times. The stack grows whenever a nested function is called, not only on recursion. – grek40 Dec 01 '17 at 07:37

1 Answers1

3

For gcc you can use -fdump-tree-original option:

g++ -fdump-tree-original -Wall -pthread 111.cpp

Now you can see how create_thread template is getting instantiated in generated dump:

$ grep create_thread 111.cpp.003t.original
;; Function void create_thread() [with int n = 0] (null)
  create_thread<10> () >>>>>;
;; Function void create_thread() [with int n = 10] (null)
  create_thread<9> () >>>>>;
;; Function void create_thread() [with int n = 9] (null)
  create_thread<8> () >>>>>;
;; Function void create_thread() [with int n = 8] (null)
  create_thread<7> () >>>>>;
;; Function void create_thread() [with int n = 7] (null)
  create_thread<6> () >>>>>;
;; Function void create_thread() [with int n = 6] (null)
  create_thread<5> () >>>>>;
;; Function void create_thread() [with int n = 5] (null)
  create_thread<4> () >>>>>;
;; Function void create_thread() [with int n = 4] (null)
  create_thread<3> () >>>>>;
;; Function void create_thread() [with int n = 3] (null)
  create_thread<2> () >>>>>;
;; Function void create_thread() [with int n = 2] (null)
  create_thread<1> () >>>>>;
;; Function void create_thread() [with int n = 1] (null)
  create_thread<0> () >>>>>;
ks1322
  • 33,961
  • 14
  • 109
  • 164