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.
Related
The code below can never print m4 (and of course no t2 and t3) on VS 2013. It behaves like a deadlock and I don't know the reason. Is there anything I missed?
#include <mutex>
#include <thread>
#include <chrono>
#include <stdio.h>
std::mutex m;
void work()
{
printf("t1\n");
m.lock();
printf("t2\n");
m.unlock();
printf("t3\n");
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("m1\n");
m.lock();
printf("m2\n");
std::thread *th = new std::thread(work);
printf("m3\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
printf("m4\n");
m.unlock();
printf("m5\n");
th->join();
return 0;
}
Edit: Yes, I cannot see deadlock. But just try it in VS 2013. It locks. I want to know the reason. Is this a VS bug or somthing goes wrong?
The program runs with the follow output:
m1
m2
t1
m3
It blocks at std::this_thread::sleep_for(std::chrono::milliseconds(1000)); forever, but why?
I think this is a bug in VS 2013 update 5. Using GCC 6.0 in Ubuntu 16.04.5 cannot reproduce this problem. Furthermore, It may be a bug related to std::this_thread::sleep_for function which causes the internal dead lock with std::mutex. So, use it with caution.
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 :)
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.
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
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
using namespace std::chrono;
int sampleFunction(int a)
{
return a;
}
int main()
{
future<int> f1=async(launch::deferred,sampleFunction,10);
future_status statusF1=f1.wait_for(seconds(10));
if(statusF1==future_status::ready)
cout<<"Future is ready"<<endl;
else if (statusF1==future_status::timeout)
cout<<"Timeout occurred"<<endl;
else if (statusF1==future_status::deferred)
cout<<"Task is deferred"<<endl;
cout<<"Value : "<<f1.get()<<endl;
}
Output -
Timeout occurred
Value : 10
In above example, I was expecting future_status to be deferred instead of timeout. sampleFunction has been launched as launch::deferred. Hence it will not be executed until f1.get() has been called. In such condition wait_for should have returned future_status::deferred and not future_status::timeout.
Appreciate if someone can help me understand this.
I am using g++ version 4.7.0 on fedora 17.
GCC and the GNU STL have no support of the complete C++ 11.
Here you can check out the C++ 11 implementation status in GCC and GNU STL:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201