C++ linker error using Boost Thread - c++

Attempting to build a standard Boost::thread example I found on the internet, I get multiple errors thrown by the Boost header file thread_data.hpp, which I don't link to directly but which I presume gets linked by Boost. (I also get the same errors in my actual program, but I am using the example code to ensure it's not a problem with my code.)
Here is the example code I found in a boost::thread tutorial:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << 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);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
Here is my build command:
mingw32-g++.exe -LC:\projects\boost\lib -o bin\Debug\Guardian.exe obj\Debug\Scratch.o -lboost_filesystem-mgw47-mt-1_53 -lboost_date_time-mgw47-mt-1_53 -lboost_system-mgw47-mt-1_53 -lboost_thread-mgw47-mt-1_53
(Notice that I am linking the boost.thread library.)
Here is the first error thrown:
C:\projects\boost\include\boost-1_53\boost\thread\win32\thread_data.hpp|123|undefined reference to `_imp___ZTVN5boost6detail16thread_data_baseE'|
In CodeBlocks I get pointed to line 123 of the header file thread_data.hpp as the source of the error:
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
, interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
, interruption_enabled(true)
Have I forgotten to link a library? I've been using Boost without problems, until now I'm trying to use the thread library. I'm new to Boost and don't know what could be causing the error.

Try add -lpthread to the linker.

Related

C++ promise.set_value fails with unknown error under linux

I'm trying to get my simulation running on our high performance server. It (unfortunately) uses CentOS Linux release 7.7.1908 (Core) instead of Win10, under which I'm developing the program. With this came a large amount of errors, one of them I was not able to fix on my on:
#include <future>
#include <iostream>
int main(int argument_count, char** arguments) {
int i = 1234;
std::cout << "Initialized i" << std::endl;
std::promise<int> promise;
std::cout << "Constructed promise" << std::endl;
promise.set_value(std::move(i));
std::cout << "Set value" << std::endl;
std::future<int> future = std::move(promise.get_future());
std::cout << "Retrieved future" << std::endl;
int j = std::move(future.get());
std::cout << "Got value: " << j << std::endl;
return 0;
}
When compiling this under Win10 with "cl test.cpp", the output looks like I'd expect:
Desktop>test.exe
Initialized i
Constructed promise
Set value
Retrieved future
Got value: 1234
On the other hand, when compiling on the server with "g++ -std=c++11 test.cpp", the output is different:
~/test_dir$ ./a.out
Initialized i
Constructed promise
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
I do get the same error when trying this with an Ubuntu 16.04.6 LTS machine. I don't see why this happens.
Obviously, something is fishy in this line: promise.set_value(std::move(i)) since the output before is printed and the line after that statement is not executed any more. Furthermore, the compiler/ linker does find a one of the two versions "void set_value (const T& val);" or "void set_value (T&& val);" that would be appropriate for the template specification "int", i strongly suspect the later.
But why is the program aborting when setting an integer as the value of a promise? Even when inlining the value and skipping the variable all together does produce the error.
Can someone point me to where the error is?
Try compiling using the pthread flag:
g++ -std=c++11 test.cpp -pthread
Linking against both pthread and stdc++ may resolve the issue.
Example adding stdc++ in Cmake:
target_link_libraries(
# Your library or binary here
stdc++
pthread
)
Tested with the following code:
#include <iostream>
#include <future>
int main(int argc, char *argv[]) {
static_cast<void>(argc);
static_cast<void>(argv);
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10);
std::cout << fut.get() << std::endl;
return 0;
}

What is boost system error_code number 2

I am trying to figure out what boost system error code 2 is. Within a program they print out the boost error code. However, I am not sure how to look up this error code.
Any help would be apprecaited.
Off the top of my head: ENOENT/FileNotFound
See the errorcodes in http://www.boost.org/doc/libs/1_58_0/libs/system/doc/reference.html#Header-error_code
Live On Coliru
#include <boost/system/error_code.hpp>
#include <iostream>
int main()
{
boost::system::error_code ec;
ec.assign(2, boost::system::system_category());
std::cout << ec.message() << "\n";
ec.assign(boost::system::errc::no_such_file_or_directory, boost::system::system_category());
std::cout << ec.message() << "\n";
}
Prints
No such file or directory
No such file or directory
For windows it is ERROR_FILE_NOT_FOUND
Check the reference

Boost threads terminates program?

I have a boost thread program from a tutorial, i can compile it with no problems or warnings but when i run it i don't get any output form eclipse. The program termiates a once. I'm using MiNGW with my eclipse could this be the problem?
Does anybody have a idea? All sugestions are welcome!
Below is the tutorial code that i used, its from this site:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(10000);
std::cout << "Worker: running" << 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);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
Update
I have linked pthreads under mingw linker and the program compiles but the result is the same. When i put i breakpoiiny just below main i get this:
The target endianness is set automatically (currently little endian)
[New Thread 4168.0xbf8]
And the program terminates does anybody have idea?
You need to make sure you're linking in both the boost_thread and pthread libraries.

boost::this_thread::sleep() returns immediately

I'm working with a very simply boost sample on Windows, and I'm running into several strange issues.
Here's the program:
// BoostThreadTest.cpp : Defines the entry point for the console application.
//
#define BOOST_ALL_NO_LIB
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
boost::posix_time::seconds workTime(10);
boost::this_thread::sleep(workTime);
//workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
The main issue is that the boost::this_thread::sleep call in the workerFunc is not actually sleeping, it is returning immediately. I also get a generic exception in the debugger when I attempt to join with the thread. The really strange thing is that a call to boost::this_thread::sleep in the main method works fine!
Does anyone have any clue what the issue might be?
I am on Windows 7, using boost 1_53_0. I built the boost thread library as a static library and am linking it in with my application built using Visual Studio 2008.

undefined reference to `_gmtime32' with boost threads tutorial

I was following this beginner tutorial on boost threads:
http://www.codeproject.com/Articles/279053/How-to-get-started-using-Boost-threads
Everything was going fine with this sample they provided:
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << 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);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
But then I decided to try it without the sleep function. So I commented out those lines.
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
//boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << 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);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
I started to get the following compile error:
)]+0x40)||undefined reference to `_gmtime32'|
I've done quite a bit of snooping around trying to figure out what this means and why it is only happening when I remove those two lines. As of right now, I am leaning towards it being something to do with some kind of header, like time.h, that I have to include (although I tried that already obviously).
I am using a rather strange setup. Code::Blocks with mingw/gcc. I compiled the boost library myself with command line arguments following the codeblocks tutorial:
http://wiki.codeblocks.org/index.php?title=BoostWindowsQuickRef
Everything seemed to work fine with this, but I suppose I could have built the libraries incorrectly.
I would dig deeper but the file name of the error is not standard and is listed as ")]+0x40)". I am not sure what this means - is it maybe some kind of file location address?
MORE INFO:
Windows XP 32 bit
CodeBlocks 10.05
MingW GCC 4.4.3
Boost 1_53_0
BUILD LOG:
Linking console executable: bin\Debug\Bjarne_Strousup_Samples.exe
....\CodeBlocks\lib\libboost_thread-mgw44-mt-1_53.a(thread.o):thread.cpp:(.text$_ZN5boost9date_time6c_time6gmtimeEPKlP2tm[boost::date_time::c_time::gmtime(long
const*, tm*)]+0x40): undefined reference to `_gmtime32' collect2: ld
returned 1 exit status Process terminated with status 1 (0 minutes, 6
seconds) 1 errors, 0 warnings
COMMAND LINE ATTEMPT:
C:\CodeBlocks Tests\BoostExamples>g++ main.cpp -lboost_thread -lboost_system -lb
oost_chrono
main.cpp:5:28: error: boost/thread.hpp: No such file or directory
main.cpp:6:31: error: boost/date_time.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:21: error: 'boost' has not been declared
main.cpp:21: error: expected ';' before 'workerThread'
main.cpp:24: error: 'workerThread' was not declared in this scope
C:\CodeBlocks Tests\BoostExamples>
LINKER SETTINGS:
IDM Link:
http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Fctime.htm
I think the error is on this line of c_time.hpp:
static std::tm* gmtime(const std::time_t* t, std::tm* result)