How to compile C++ std::thread code with MinGW? - c++

I would like to compile my c++11 project (recently moved to c++11) with MinGW. And I have compiling errors about c++11 code like "std::thread not found".
I used the last MinGW with gcc 5.3.0 (December 2015). A the end, I would like only to compile this example before to compile my big project :
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
(source : http://en.cppreference.com/w/cpp/thread/thread/join)
I tried "g++ -std=c++11 main.cpp" and "g++ main.cpp -std=c++0x" but I have always those following errors :
main.cpp: In function 'void foo()':
main.cpp:8:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'void bar()':
main.cpp:14:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'int main()':
main.cpp:20:5: error: 'thread' is not a member of 'std'
std::thread helper1(foo);
^
main.cpp:23:5: error: 'thread' is not a member of 'std'
std::thread helper2(bar);
^
main.cpp:26:5: error: 'helper1' was not declared in this scope
helper1.join();
^
main.cpp:27:5: error: 'helper2' was not declared in this scope
helper2.join();
^

MinGW mostly does not have a port of glibc which supports pthreading or gthreading like in GCC.
For solving this, the first solution can be installing library of thread headers.
The other solution can be working with GCC compiler.

Related

Unable to compile C++ thread library on my Ubuntu Linux system

I am unable to compile C++ thread library on my Ubuntu Linux system. With every option I give while compiling I always get this error
Either I compile giving
alc:~/practice/C++$ g++ -o thr thr.cpp -lpthread -std=c++11
or
alc:~/practice/C++$ g++ -o thr thr.cpp -std=c++0x -lpthread
In file included from thr.cpp:3:0:
/usr/include/c++/4.9/thread: In constructor ‘std::thread::thread(_Callable&&, _Args&& ...)’:
/usr/include/c++/4.9/thread:138:46: error: ‘__bind_simple’ is not a member of ‘std’
_M_start_thread(_M_make_routine(std::__bind_simple(
^~~~~~~~~~~~~
/usr/include/c++/4.9/thread:138:46: note: suggested alternative: ‘__big_div_impl’
_M_start_thread(_M_make_routine(std::__bind_simple(
^~~~~~~~~~~~~
__big_div_impl
This is my C++ program
#include <c++/4.9/iostream>
#include <c++/4.9/thread>
using namespace std;
void func_dummy(int N)
{
for(int i=0 ; i < N ; i++)
cout << "Thread 1 : function pointer" << endl;
}
int main()
{
thread thr1(func_dummy, 2);
thr1.join();
return 0;
}

Unable to compile a deliberate file on cygwin

The problem occurs when compiling the vim plugin color_coded, and can be summarized as follows:
With g++ -std=c++14, symlink can not be found in unistd.h.
With clang++ -std=c++14, mutex is not supported on cygwin.
So, how to compile this code on cygwin?
#include <unistd.h>
#include <mutex>
#include <thread>
#include <iostream>
std::mutex io_mutex;
auto foo(){
std::lock_guard<std::mutex> guard{io_mutex};
std::cout << &symlink << "\n";
}
int main(){
std::thread t1{foo}, t2{foo};
t1.join();
t2.join();
}
The following are the errors.
with gcc 5.4.0 and g++ -std=c++14:
t.cpp: In function 'auto foo()':
t.cpp:9:18: error: 'symlink' was not declared in this scope
std::cout << &symlink << "\n";
With clang 3.7.1 and clang++ -std=c++14:
In file included from t.cpp:2:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:699:10: error: thread-local
storage is not supported for the current target
extern __thread void* __once_callable;
^
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:700:10: error: thread-local
storage is not supported for the current target
extern __thread void (*__once_call)();
^
2 errors generated.

Multithreaded code won't compile using g++, but is fine with clang++

I am having trouble with the g++ compiler. On my work machine (running OS X 10.10.4) I was experimenting with some code using Xcode. The code did compile succesfully, and the resulting executable works as expected. Output of clang++ --version:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on
LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4.0 Thread model: posix
Then I decided to compile this code on a server running Debian 8 with g++. The output of g++ --version:
g++ (Debian 4.9.2-22) 4.9.2 Copyright (C) 2014 Free Software
Foundation, Inc.
The code won't even compile using g++. The command I tried using: g++ -std=c++11 -pthread main.cpp
I get the following error messages:
main.cpp: In function 'int main()':
main.cpp:32:106: error: invalid use of incomplete type 'class std::packaged_task'
std::shared_ptr > ptr(new std::packaged_task(std::bind(factorial, 6)));
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp:33:22: error: variable 'std::future fu1' has initializer but incomplete type
std::future fu1 = ptr->get_future();
^
main.cpp:33:31: error: invalid use of incomplete type 'class std::packaged_task'
std::future fu1 = ptr->get_future();
^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp: In lambda function:
main.cpp:34:48: error: invalid use of incomplete type 'class std::packaged_task'
std::function task1 = &ptr{ ptr->operator()(); };
^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp: In function 'int main()':
main.cpp:36:38: error: variable 'std::packaged_task t' has initializer but incomplete type
std::packaged_task t(std::bind(factorial, 5));
^
main.cpp:37:22: error: variable 'std::future fu2' has initializer but incomplete type
std::future fu2 = t.get_future();
^
My code:
#include <iostream>
#include <thread>
#include <future>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
unsigned long long int factorial(unsigned long long int num)
{
unsigned long long int N = num;
for (unsigned long long int i = num; i > 1; --i)
{
num *=(--N);
}
return num;
}
int main()
{
std::shared_ptr<std::packaged_task<int()> > ptr(new std::packaged_task<int()>(std::bind(factorial, 6)));
std::future<int> fu1 = ptr->get_future();
std::function<void()> task1 = [&ptr](){ ptr->operator()(); };
std::packaged_task<int()> t(std::bind(factorial, 5));
std::future<int> fu2 = t.get_future();
std::function<void()> task2 = [&t](){ t(); };
std::thread threads[2];
threads[0] = std::thread(task1);
threads[1] = std::thread(task2);
cout << fu1.get() << endl;
cout << fu2.get() << endl;
threads[0].join();
threads[1].join();
return 0;
}
What could be the issue with g++?
It seems, that std::future & std::async are not implemented on the armel architecture for some reason.
I can't really find out why is this (some argue on mailing lists, that they are not implemented by design, some others say this is a bug) and what is the current state of the problem.
However, I've also found a reply that stated this may be already resolved in the newer versions of libstdc++(My system is running the Testing version of debian, I do not have these versions yet, and I don't plan to get the package from unstable repos, so I'll just wait for it).

Weird thread issue

Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags

Boost compilation error

I am trying to compile my boost simple code:
#include <iostream>
#include <boost/thread.hpp>
void workerFunc(const char* msg, float delay_ms)
{
boost::posix_time::milliseconds workTime(delay_ms);
std::cout << "Worker: running, message = " << msg << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc, "Hello, Boost!", 2.5e3);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
using g++ with this command
g++ main.cpp -o main
but i get an error like this:
main.cpp: In function `void workerFunc(const char*, float)':
main.cpp:7: error: `boost::posix_time' has not been declared
main.cpp:7: error: `milliseconds' was not declared in this scope
main.cpp:7: error: expected `;' before "workTime"
main.cpp:12: error: `boost::this_thread' has not been declared
main.cpp:12: error: `workTime' was not declared in this scope
main.cpp: In function `int main(int, char**)':
main.cpp:21: error: no matching function for call to `boost::thread::thread(void (&)(const char*, float), const char[14], double)'
/usr/include/boost/thread/thread.hpp:35: note: candidates are: boost::thread::thread(const boost::thread&)
/usr/include/boost/thread/thread.hpp:38: note: boost::thread::thread(const boost::function0<void, std::allocator<boost::function_base> >&)
/usr/include/boost/thread/thread.hpp:37: note: boost::thread::thread()
What's wrong and how should i compile it...?
According to this
http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html
You need this
#include "boost/date_time/posix_time/posix_time.hpp"
I suspect you have an old version of Boost installed in your system. Read the file /usr/include/boost/version.hpp. And depending on which version you have, consult the version specific documentation (see Boost Documentation). Or install the latest version of Boost by either using you system's packaging functionality, if available, or manually following the install directions (see Getting Started on Unix Variants).
Since the compiler doesn't recognize some type, it means that you are missing some includes.
For posix_time you need to add #include "boost/date_time/posix_time/posix_time.hpp" on top of your code.
You need to include the header declaring posix_time. Look at the boost doc to see which it is (you can try #include "boost/date_time/posix_time/posix_time_system.hpp" but I'm not sure that would be enough).