If your "debug error" means the compiler error message, you should check if -pthread flag is set. That is compile the code with
$ g++ -std=c++11 main.cpp -pthread -o main
If your "debug error" means the runtime error, you should remember to join() after you create a thread.
source code:
#include <iostream> #include <thread> void print() { std::cout << "PRINT" << std::endl;; std::cout << "PRINT 2" << std::endl;; } void create_thread() { std::thread print_thread(print); print_thread.join(); // remember to join() } int main() { create_thread(); return 0; }
In addition, you may pay attention to 4 additional points:
using namespace std is not recommended.
remember to join() after you create a thread
return 0 for main()
printf() is in stdio.h. use std::cout for iostream
printfin a thread subroutine