Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Core dump with packaged_task

Tags:

c++

gcc

c++11

g++

I got a strange core dump which I copied from a part of code in http://en.cppreference.com/w/cpp/thread/packaged_task,

#include <future>
#include <iostream>
#include <cmath>

void task_lambda() {
    std::packaged_task<int(int,int)> task([](int a, int b) {
        return std::pow(a, b);
    });
    std::future<int> result = task.get_future();

    task(2, 9);

    std::cout << "task_lambda:\t" << result.get() << '\n';
}


int main() {
    task_lambda();
}

I got this

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
[1]    28373 abort (core dumped)  ./a.out

The call stack is like below:

#0  0x00007ffff71a2428 in __GI_raise (sig=sig@entry=6) at              ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff71a402a in __GI_abort () at abort.c:89
#2  0x00007ffff7ae484d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7ae26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7ae2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7ae2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7b0b7fe in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x0000000000404961 in std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) (__once=..., __f=<unknown type in /home/ace/test/a.out, CU 0x0, DIE 0x1246d>)
at /usr/include/c++/5/mutex:746
#8  0x0000000000403eb2 in std::__future_base::_State_baseV2::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) (
this=0x61ec30, __res=..., __ignore_failure=false) at /usr/include/c++/5/future:387
#9  0x0000000000402b76 in std::__future_base::_Task_state<task_lambda()::<lambda(int, int)>, std::allocator<int>, int(int, int)>::_M_run(<unknown type in /home/ace/test/a.out, CU 0x0, DIE 0x17680>, <unknown type in /home/ace/test/a.out, CU 0x0, DIE 0x17685>) (this=0x61ec30, __args#0=<unknown type in /home/ace/test/a.out, CU 0x0, DIE 0x17680>, 
__args#1=<unknown type in /home/ace/test/a.out, CU 0x0, DIE 0x17685>) at /usr/include/c++/5/future:1403
#10 0x00000000004051c1 in std::packaged_task<int (int, int)>::operator()(int, int) (this=0x7fffffffdca0, __args#0=2, __args#1=9) at /usr/include/c++/5/future:1547
#11 0x0000000000401c7d in task_lambda () at aa.cc:12
#12 0x0000000000401d1b in main () at aa.cc:19

Then I added some sample code into my program, it became

#include <iostream>
#include <cmath>
#include <future>
#include <thread>
int f(int x, int y) { return std::pow(x,y); }
void task_thread() {
    std::packaged_task<int(int,int)> task(f);
    std::future<int> result = task.get_future();

    std::thread task_td(std::move(task), 2, 10);
    task_td.join();

    std::cout << "task_thread:\t" << result.get() << '\n';
}
void task_lambda() {
    std::packaged_task<int(int,int)> task([](int a, int b) {
        return std::pow(a, b);
    });
    std::future<int> result = task.get_future();

    task(2, 9);

    std::cout << "task_lambda:\t" << result.get() << '\n';
}


int main() {
    task_lambda();
}

the error was gone. How can I correct the program by adding a function even though I never call it?

gcc version

gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

Promgram compiled with command:

g++ -std=c++11 aa.cc -lpthread

With @fedepad's help, I got correct output by replace lpthread with pthread. But I still confused how second code work by add a dummy function!

like image 948
SpadeAce Avatar asked Jan 22 '17 11:01

SpadeAce


1 Answers

I tried your first code snippet using the following version of g++:

g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

and compiled with the following

g++ -o test_threads test_threads.cpp -std=c++11 -pthread  

and I can run the program with no problems, and getting the following output:

$ ./test_threads
task_lambda: 512

If I then use the -lpthread as you did with the following

g++ -o test_threads test_threads.cpp -std=c++11 -lpthread  

I get

$ ./test_threads terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
[1] 7890 abort ./test_threads

So please use -pthread as a flag and not -lpthread.

This behavior is also mentioned in the following
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59830

There's a difference between -pthread and -lpthread.
Looking at the man page for g++

-pthread
Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

To have a look to what flags are activated for both one can check with the following:

g++ -dumpspecs | grep pthread
g++ -dumpspecs | grep lpthread

As one can clearly see, there are some preprocessor macros that are not activated if one is using -lpthread.

like image 190
fedepad Avatar answered Oct 03 '22 06:10

fedepad