Unable to do multithreading in C++ [duplicate] - c++

This question already has answers here:
CodeBlocks c++ - Can't use Thread because the compiler doesn't support it
(1 answer)
Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?
(1 answer)
Closed 4 years ago.
So I started to learn c++ and I come across multithreading, but there is one problem: It gives me the error main.cpp|12|error: 'thread' was not declared in this scope although I have included the <thread> library in my code.
I use Code::Blocks and GNU GCC Complier on Windows.
Here is an example of my code:
#include <iostream>
#include <thread>
using namespace std;
void test(){
cout<<"hello, I am a thread !";
}
int main(){
thread t1(test);
return 0;
}

Related

How to compile C++ application with #include <thread> in windows (MinGW - W64) using G++? [duplicate]

This question already has an answer here:
Why doesn't my compiler recognize #include <thread> (c++)?
(1 answer)
Closed 1 year ago.
I have tried compiling my C++ File with #include <thread> using G++, but it failed. Here is my source code
below:
#include <iostream>
#include <thread>
#include <windows.h>
using namespace std;
void act_1()
{
cout << "Hello World from act_1\n";
return;
}
int main()
{
thread th(act_1);
th.join();
ExitProcess(EXIT_SUCCESS);
}
Now, I have used the flags -std=c++11 and -pthreads, but it is still not working. Here is the console output:
C:\Users\Mayukh\Desktop>g++ -std=c++11 -pthread threaded.cpp -o thread
threaded.cpp: In function 'int main()':
threaded.cpp:15:3: error: 'thread' was not declared in this scope
thread th(act_1);
^~~~~~
threaded.cpp:15:3: note: 'std::thread' is defined in header '<thread>'; did you
forget to '#include <thread>'?
threaded.cpp:4:1:
+#include <thread>
threaded.cpp:15:3:
thread th(act_1);
^~~~~~
threaded.cpp:16:3: error: 'th' was not declared in this scope
th.join();
^~
threaded.cpp:16:3: note: suggested alternative: 'tm'
th.join();
^~
tm
Please help me
I found the answer for now, as I was using x86_x64_win32, and it had no threading implemented, I switched to x86_x64_posix, and it is working for now.
If anyone still can find the solution in x86_x64_win32 at MinGW - W64, please post an answer

how come an undeclared variable is outputting a value [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.

Codeblocks C++ threading error [duplicate]

This question already has answers here:
MinGW 4.8.1 C++11 thread support
(2 answers)
Closed 7 years ago.
I have a problem including the thread library. The following code:
#include <string>
#include <iostream>
#include <thread>
using namespace std;
//The function we want to make the thread run.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
//Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
Produces these errors:
The code is taken from the answer to another stackoverflow question. I am fairly new to codeblocks and C++ so please explain to me what I am doing wrong.
You probably have not set proper flags for compiler (so that it uses c++11). Way of doing it in codeblocks

Fatal Error in c++ while using local variables [duplicate]

This question already has answers here:
Where to get iostream.h
(3 answers)
Closed 7 years ago.
As am learning cpp tutorial
#include <iostream.h>
using namespace std;
int main()
{
//variable declaration
int a,b;
int c;
//actual initialization
a=10;b=20;
c=a+b;
cout<<c;
return 0;
}
my error
fatal error: iostream.h
Just change <iostream.h> to <iostream>
Reason is that .h header extensions were used for C includes but aren't used for C++ anymore.
In fact, you can actually use C libraries with .h it's just there isn't one for iostream since its C++ exclusive, hence the fatal error.

Array defined as "_end[LEN]" causes segmentation fault in C/C++ [duplicate]

This question already has answers here:
What are the rules about using an underscore in a C++ identifier?
(5 answers)
Closed 7 years ago.
I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the name "_end" very special in C/C++ that causes such problem? Or this can be a very serious bug... (The code is attached below, and it breaks in g++ 4.3.2, 4.5.2, 4.9.2, etc.)
#include <iostream>
using namespace std;
int _end[1111];
int main() {
for (int i=0; i<1111; i++) {
cout << i << endl;
_end[i]++;
}
return 0;
}
You can see the result at https://ideone.com/XAcUeZ.
See here also for the C compiler.
Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.