- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我从 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();
}
我明白了
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
[1] 28373 abort (core dumped) ./a.out
调用栈如下:
#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
然后我在我的程序中添加了一些示例代码,它变成了
#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();
}
错误消失了。即使我从未调用过函数,如何通过添加函数来更正程序?
海湾合作委员会版本
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
用命令编译的程序:
g++ -std=c++11 aa.cc -lpthread
在@fedepad 的帮助下,我通过将 lpthread 替换为 pthread 获得了正确的输出。 但我仍然对第二个代码如何通过添加一个虚拟函数感到困惑!
最佳答案
我使用以下版本的 g++ 尝试了您的第一个代码片段:
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
并用以下内容编译
g++ -o test_threads test_threads.cpp -std=c++11 -pthread
我可以毫无问题地运行程序,并得到以下输出:
$ ./test_threads
task_lambda: 512
如果我然后使用 -lpthread
就像您对以下内容所做的那样
g++ -o test_threads test_threads.cpp -std=c++11 -lpthread
我明白了
$ ./test_threads terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
[1] 7890 abort ./test_threads
所以请使用 -pthread
作为标志,而不是 -lpthread
。
下面也提到了这种行为
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59830
-pthread
和-lpthread
是有区别的。
查看 g++ 的手册页
-pthread
Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.
要查看为两者激活了哪些标志,可以检查以下内容:
g++ -dumpspecs | grep pthread
g++ -dumpspecs | grep lpthread
可以清楚地看到,如果使用 -lpthread
,有些预处理器宏不会被激活。
关于C++:带有 packaged_task 的核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790363/
正在关注 this excellent tutorial对于futures、promises 和打包任务,我到了要准备自己的任务的地步 #include #include using namesp
在下面的程序中,我试图创建一个带有成员函数的 packaged_task: #include using namespace std; struct S { int calc(int& a)
我天真地期望它能编译: template auto run(Func && func) { auto package = std::packaged_task{std::forward(fu
我正在尝试使用这样的仿函数创建一个打包任务: Worker w(someString, anotherString, i*length,length); boost::packaged_task ta
我试图绕过 std::packaged_task 缺少复制构造函数的问题,以便我可以将它传递给 std::function(它只会被移动)。 我继承自 std::packaged_task 并添加了一
嗨,我不知道如何编写一个正确的队列绑定(bind)并执行传递给方法 OCRQueue::enqueue() 的 lambda 表达式 // the task queue std::queue > ta
编译下面的程序时,出现错误信息: 错误 1 错误 C2228:'.get_future' 的左侧必须具有类/结构/union c:\users\haliaga\documents\visual s
我正在分析以下代码片段并试图详细理解它: template auto ThreadPool::add( FUNCTION&& Function, ARGUMENTS&&... Arguments )
我从 http://en.cppreference.com/w/cpp/thread/packaged_task 中的部分代码中复制了一个奇怪的核心转储, #include #include #i
我正在尝试采用 std::async 风格的“任务”并将其存储在容器中。我必须克服困难才能实现它,但我认为一定有更好的方法。 std::vector> mTasks; template std::fu
我最近在我的项目上运行 valgrind --tool=helgrind 并收到警告“可能的数据竞争”,我认为这是令人担忧的。然而,即使是这个简单的测试程序也会导致此消息: #include #in
我正在尝试使用 packaged_task 实现异步。我正在通过一个模板化函数 bsync 来尝试这个。 bsync 接受 2 个参数:一个函数 f 和一个参数包 args,并返回一个 future
我一直在寻找 problem of type-erasing a std::packaged_task using std::function 的解决方法. 我想做的是这样的: #include #
我正在玩 Antony Williams - C++ Concurrency in Action 一书中的示例 4.14,其中使用 std::packaged_task 和 std::thread 模
我试图将 std::packaged_task 包装在另一个类中,以便与任务调度程序一起使用。 目前,除了 std::future 支持外,我的一切都正常工作。为了获得 std::future 支持,
我正在尝试围绕 Glib::Dispatcher 构建一些包装器,以将任何功能类型分派(dispatch)到分派(dispatch)器中。我想要一些可以将函数传输到 Glib 主循环中的函数分派(di
std::packaged_task 模板有一个接受分配器的构造函数: packaged_task pt {allocator_arg_t, a, f}; 我想问: 1) 它的存在表明 package
是否有一种标准方法可以将 std::packaged_task 添加到现有线程?在任务运行之前必须发生大量开销,所以我想这样做一次,然后保持线程运行并等待任务执行。我希望能够使用 futures,这样
我正在尝试将 packaged_task 包装在 lambda 中,以便将它们存放在容器中。我在下面写了一个测试代码来模拟包装和调用 lambda 函数。我的代码如下: int test() {
在使用 C++11 的线程模型时,我注意到 std::packaged_task task([](int a, int b) { return a + b; }); auto f = task.get
我是一名优秀的程序员,十分优秀!