Error compiling thread program in codeblocks - c++

My thread program is:
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11

First, are you on windows or linux?
If you are on linux, you must compile with C++11 support. Just pass -std=c++11 to g++.
I can't help you with windows.

Your IDE may not support C++11 yet. since thread is included in the standard since C++11. See this thread for CodeBlocks? http://forums.codeblocks.org/index.php?topic=15536.0

Related

Using C++ Multithreading library <thread> is giving error 'thread' is not a member of 'std'

I know it is a repeat question but non of the answers that I read is addressing the issue. Hence I am Posting the question again.
I wrote this very simple program
#include<iostream>
#include<thread>
using namespace std;
void func1(char ch){
for(int i=0; i<100; i++){
cout<<ch;
}
}
int main(){
std::thread worker1(func1, 'a');
std::thread worker2(func1, 'b');
getchar();
return 0;
}
The above code when compiler with (MinGW.org GCC-6.3.0-1) 6.3.0(C++ 14) gives me error as thread' is not a member of 'std'. However, I am able to go to the definition of thread at location C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++\thread. The same code when compiled on Ubuntu with compiler g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 works perfectly fine. I don't seem to find a reason why it should not work on Windows.

Eclipse c++ giving me Invalid arguments if I write thread using lambda

When trying to make thread using lambda with following code:
#include <iostream>
#include <thread>
using namespace std;
int main() {
thread t([] {
cout << "hello\n";
});
t.join();
return 0;
Eclipse says:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
thread(const std::thread &)
thread(std::thread &&)
' cpptest.cpp /cpptest/src line 14 Semantic Error
Code compiles successfully, but that error is annoying, I also tried C/C++ Build > Settings > GCC C++ Compiler > Dialect > Language Standard = ISO C++17, but without luck.
There may be problem with cpp checking setting, which are not set to higher cpp version, but I can't see that option in project setting.

Thread could not be resolved in eclipse 4.7

I've installed MinGW(gcc 6.3.0) and Eclipse 4.7 on Windows 8.1. I could compile and run other programs without any issue. But when I was using thread it is showing that thread was not declared in this scope. I've tried this and this but both no avail. Could somebody help me fix this?
Here is the code I've treid.
#include <iostream>
#include <thread>
void foo(){
std::cout<<"thread function \n";}
int main(void)
{
std::thread t1(foo);
t1.join();
return 0;
}
I'm not sure about the C++11 features. I've tried Lambda function example from this page(last example) and it is working fine.

Eclipse Neon 3 - C++ 11 Threads

I have a problem with using Threads in C++. I read this topic:
How to make CDT/Eclipse work with C++11 threads?
But it didn't help at all :(
So, from the beginning. I'm trying to 'convert' big project from Visual Studio into Eclipse and I'm forcing with a problem with threads. So I created another Eclipse project in order to test that.
#include <iostream>
#include <thread>
void worker()
{
std::cout << "hello from worker" << std::endl;
}
int main(int argc, char **argv)
{
std::thread t(worker);
t.join();
}
But, I get:
error: 'thread' is not a member of 'std'
In GCC C++ Compiler->Miscellaneous I have
-c -fmessage-length=0 -std=c++11 -pthread
In MinGW C++ Linker->Libraries I have
pthread
Even in MinGW C++ Linker->Miscellaneous I have
-std=c++11
I'm a fresh user of Eclipse and I have no idea what I'm doing wrong.
I would be very grateful for any tips :)

Error compiling thread program in codeblocks 12.11

my thread program is :
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
I have the same problem. Unfortunately the version of GCC Code::Blocks uses does not support the features of C++11 that you want. Turning on the option -std=c++0x will only ugrade the compiler to a less recent version of the new Standard. That means you will only enable basic support of C+11.