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

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;
}

Related

Why is CLion not showing exceptions?

CLion appears to be not showing me any exceptions when running my code. To test this, I've created a new project with only the following code:
#include <iostream>
int main() {
std::cout << "--- One" << std::endl;
throw 6;
std::cout << "--- Two" << std::endl;
return 0;
}
Which leads to the following output:
C:\Users\david\CLionProjects\untitled\cmake-build-debug\untitled.exe
--- One
Process finished with exit code 0
As you can see, code before the exception is executed and code following it is not executed (as you would expect). But instead of a message about the exception, it says "Process finished with exit code 0", as if no exception had occurred.
The same code compiled and executed on Ubuntu (via terminal) displayed an error message. So I assume the problem is with CLion.
How can I resolve this problem so that I can see messages for exceptions in my code?
Is there any setting that could lead to such behaviour?
I'm using CLion on Windows 10 with Cygwin. Here's a screenshot of the problem:
Throw requires also try and catch
From:
http://www.cplusplus.com/doc/tutorial/exceptions/
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
that compiled and run under cygwin shows:
$ g++ prova1.cc -o prova1
$ ./prova1
An exception occurred. Exception Nr. 2

boost system make_error_code of type errc::timed_out has message of "Unkown error"

Why does the following code output "Unknown error"? I expect some other message like "operation timed out" or other descriptive error.
OS: Windows 7
boost: 1.57
#include <iostream>
#include "boost/system/system_error.hpp"
void main()
{
boost::system::error_code ec = make_error_code(boost::system::errc::timed_out);
auto message = ec.message();
std::cout << message << std::endl;
}
Suggest you check include paths, library paths and project settings.
I have corrected the program (main must return an int) and compiled under clang:
#include <iostream>
#include <boost/system/system_error.hpp>
int main()
{
boost::system::error_code ec = make_error_code(boost::system::errc::timed_out);
auto message = ec.message();
std::cout << message << std::endl;
}
command line:
c++ -std=c++14 -I${HOME}/local/include -L${HOME}/local/lib -lboost_system
result:
Operation timed out
My boost installation is installed to the prefix ${HOME}/local

std::endl results in crash

During the development of a simple example (I haven't programmed C++ for some time) I encountered a weird behaviour. Following hello world program crashes under Windows (Mingw):
#include <iostream>
int main () {
for (int idx = 0; idx < 5; idx++) {
std::cout << "Hello World" << std::endl;
}
return 0;
}
If I remove std::endl the program does not crash though.
I use following commands to compile and execute the example, with Mingw32 (g++ 4.8.1) on a 64bit system and OS:
g++ example.cpp -o example.exe
example.exe
The error message is:
example.exe does not work any longer...
Is this a known issue or an obvious mistake of mine?
<< endl is a manipulation essentially a function. your output cant be flushed which is causing problems.

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)

C++ linker error using Boost Thread

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.