gcc giving error when initiating thread class - c++

I've been having an error declaring a thread class object. I tried this code and it gave me the error "'thread' was not declared in this scope". If it helps, I'm compiling to Windows using MinGW GCC.
#include <iostream>
#include <thread>
using namespace std;
void func(){
cout << "Hello from thread 2\n";
}
int main(){
cout << "Hello from thread 1\n";
thread t2(func);
t2.join();
return 0;
}
Compiling gives me "error: 'thread' was not declared in this scope"

Threads are missing in mingw, but you can use some library like this one mingw-std-threads.

Related

Initializing C++11 threads from within an if statement

I am implimenting threads in C++11 and experienceing a compilation issue whenever I initiate a thread from within an if statement.
The error I am receiving is:
file.cpp: In function ‘int main(int, char**)’:
file.cpp:16:2: error: ‘thread1’ was not declared in this scope
thread1.join();
When I move the thread outside of an if statement everything compiles and runs fine.
I am using g++ version 4.8.2 and using the -std=c++11 compiler option.
This code will not compile
#include <unistd.h>
#include <thread>
#include <iostream>
void testthread() {
std::cout << "Thread was run" << std::endl;
}
int main(int argc, char**argv) {
if (true) {
std::thread thread1(testthread);
}
sleep(1);
thread1.join();
return 0;
}
This code compiles and runs as expected
#include <unistd.h>
#include <thread>
#include <iostream>
void testthread() {
std::cout << "Thread was run" << std::endl;
}
int main(int argc, char**argv) {
std::thread thread1(testthread);
sleep(1);
thread1.join();
return 0;
}
The body of an if() statement is a block scope so the lives any variables created within it are bound to its scope. This means that thread1 isn't accessible outside of the if() statement.
Instead you can default construct the thread and then assign it to a new one:
std::thread thread1;
if (true) {
thread1 = std::thread(testthread)
}
You are declaring the thread variable inside the if block. It is only visible there.
If you really need to initialize it inside the if block and use it outside, you can use a pointer and allocate it inside the if block.
std::thread* pThread1 = nullptr;
if (true) {
pThread1 = new std::thread(testthread);
}
sleep(1);
pThread1->join();
delete(pThread1);

Threads in c++ using eclipse juno

I'm trying to run the following code, but eclipse is giving the errors "type thread cannot be resolved", "thread was not declared in this scope", "expected ; before t1".
#include <iostream>
#include "Tetris.h"
#include <Windows.h> //Sleep
#include <conio.h> //getch()
#include <thread>
#define SLEEP_TIME 1000
using namespace std;
void call_from_thread() {
cout << "hello!" << endl;
}
int main(){
/* some code... */
thread t1(call_from_thread);
/* some more code... */
return 0;
}
So far i've tried the following:
I've also tried the -thread flag as I've seen someone suggesting in an answer here.
I've been looking on google and stackoverflow for hours without being able to solve the problem. "thread" still cannot be resolved and therefore I can't compile and generate a binary.
Any help would be very welcome
thanks in advance

Boost thread c2064 error when trying to compile

I'm quite new to the stack overflow, in fact this is my first post, So hello everyone. So let's get to the point.
Using boost library thread ver. 1.54.0
Using VS2010 32 Bit - Professional
I have built the libraries for the boost thread,
not using precompiled headers in vs C++ settings,
linked the library to the project,
here is the code
#include <boost\thread\thread_only.hpp>
#include <iostream>
#include <conio.h>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
#define BOOST_LIB_NAME libboost_thread-vc100-mt-gd-1_54.lib
struct callable
{
void blah();
};
void callable::blah()
{
std::cout << "Threading test !\n";
}
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
createThread();
_getch();
return 0;
}
after all this I get this error
Error 1 error C2064: term does not evaluate to a function taking 0 arguments ..\..\boost_1_54_0\boost\thread\detail\thread.hpp 117 1 BoostTrial
Could you help me to get this example to work. Reason why I am using this example is because I have another app which has been set up exactly the same way and it's not working because of this error :-( my goal is to get the multithreading to work and then I can take it from there.
Thanks for your time.
You need to implement operator() in callable.
Don't forget to either join() or detach() thread to prevent abnormal program termination.
See boost::thread tutorial for more examples.
#include <boost\thread\thread_only.hpp>
#include <iostream>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
using namespace boost;
struct callable
{
void operator()()
{
std::cout << "Threading test !\n";
}
};
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
boost::thread th = createThread();
th.join();
}
Example with std::thread;

Error compiling thread program in codeblocks

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

Eclipse c++11 // vector

This is really driving me crazy:
#include <iostream>
#include <vector>
#include <string.h>
#include <thread>
using namespace std;
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
int main() {
thread(test).join();
return 0;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker.
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved".
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. This took me so many hours now, what am I doing wrong?!
Is there anybody getting a project setup without problems with this code?
EDIT: Other code example - same problem:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved
EDIT:
working versions:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims:
Method 'test' could not be resolved
Maybe I'm wrong, but I think your problem don't come from Eclypse. Juste, begin() on a vector return a std::vector<T>::iterator first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size(); if this is what you want.
The problem could come from your #include <string.h> that is not the same as #include <string>, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object.
I don't have Eclipse set up but the problem appears to be around std::string. Does the problem go away if you remove the threading from the example? (I also changed to #include <string> instead of string.h)
#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#if 0
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
#endif
int main() {
//thread(test).join();
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
return 0;
}
That should hopefully print out 10.
Update from comment:
Does this generate the Eclipse warning?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found:
I downloaded eclipse kepler Kepler
Created a new project and tried to compile this source code (like above):
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. To fix this I proceeded the following step:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.
Found here.
Now - unbelievable but true - it works, even without any errors marked by eclipse. The solution is using the (beta) version of eclipse, wich seems to handle this in a better way.
Thanks for all your help!