How to enable MultiThreading in C++?

cplusplus

#1

Hey guys when i right this code in C++:

#include <iostream>
#include <string>
#include <thread>


void func_1(){
    std::cout << "Thread 1 Exectuting" << std::endl;
}
int main(){
    std::thread t1(func_1);
    
    t1.join();
    /*Main Threads
    **Wait for t1 thread to finish..
    **Just like go
    */
}

And hit Ctrl+shift+b:

This comes up:

terminate called after throwing an instance of 'std::system_error'                                                              
  what():  Enable multithreading to use std::thread: Operation not permitted                                                    
bash: line 12:  9798 Aborted                 $file.o $args  

C9.io: https://ide.c9.io/antihuman/fornoreason#openfile-README.md


#2

How did you compile it?


#3
Ctrl+Shift+B

#5

I use c9 for web development so I have no C++ experience in c9. But I did create a C++ workspace and I tried some threading and it was unable compile.


#7

I haven’t touched C++ in years, but it seems that your main method is missing a return statement. Something like return 1 perhaps.


#8

That dosent work @BigChief45 , these days you dont need to return, if it is the original no-param Main method.


#9

@vasilenko93 , @BigChief45, and anyone facing this problem i gotta solution for yall! :boom:

Do:

g++ main.cpp -o main.out -pthread -std=c++11

Change main.cpp with your file name… and then to run it

./main.out

#10

Yeah did some extra research and the -pthread was missing.

Thanks