- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 std::thread 制作一个包装器,它允许我查看在被调用的线程上抛出了哪些异常。我在加入线程时向上传播它们。这在调用带参数的函数或不带参数的类成员函数时效果很好。但是如果我试图让它与一个带参数的类成员函数一起工作,我会收到一个我无法理解的错误。下面是一些示例代码,可重现我所看到的问题。
#include <iostream>
#include <string>
#include <thread>
#include <exception>
#include <utility>
class BlazingThread {
public:
template< typename Function, typename... Args >
explicit BlazingThread( Function&& f, Args&&... args ){
exception = nullptr;
thread = std::thread(([&](){
try{
auto caller = std::forward<Function>(f);
caller(args...);
}catch(...){
//non handled exception rethrow so we can stop whoever started this mess
//exception = std::make_exception_ptr(BlazingException("An unknown error occurred!"));
}
}));
}
template< typename Function >
explicit BlazingThread( Function&& f){
exception = nullptr;
thread = std::thread(([&](){
try{
//std::bind(f);
std::forward<Function>(f);
}catch(...){
//non handled exception rethrow so we can stop whoever started this mess
// exception = std::make_exception_ptr(BlazingException("An unknown error occurred!"));
}
}));
}
BlazingThread(BlazingThread && other){
this->thread = std::move(other.thread);
this->exception = std::move(other.exception);
}
BlazingThread(){
}
BlazingThread& operator=(const BlazingThread&) = delete;
BlazingThread& operator=(BlazingThread && other){
this->thread = std::move(other.thread);
this->exception = std::move(other.exception);
return *this;
}
BlazingThread(const BlazingThread& other) = delete;
virtual ~BlazingThread();
void join();
static unsigned int hardware_concurrency(){
return std::thread::hardware_concurrency();
}
private:
std::thread thread;
std::exception_ptr exception;
};
void BlazingThread::join(){
thread.join();
if(this->exception != nullptr){
//an exception was thrown in the thread, lets rethrow it
std::rethrow_exception(this->exception);
}
}
BlazingThread::~BlazingThread() {
// TODO Auto-generated destructor stub
}
void testFunction(int x, int y){
int z = x + y;
std::cout<<"x + y = "<<z<<std::endl;
//throw BlazingException("A planned error!");
}
class TestClass{
public:
TestClass(int newX){
this->x = newX;
}
void talk(){
std::cout<<"this is "<<x<<std::endl;
}
void talking(){
BlazingThread thread2(&TestClass::talk);
thread2.join();
std::string msg = "something else ";
BlazingThread thread3(&TestClass::talkSomething, msg);
thread3.join();
}
void talkSomething(std::string testMsg){
std::cout<<testMsg<<x<<std::endl;
}
private:
int x;
};
int main() {
try{
TestClass test(3);
test.talk();
BlazingThread thread(testFunction,2,4);
thread.join();
}catch(...){
//std::cout<<"Found BlazingException, what = "<<e.what()<<std::endl;
}
return 0;
}
线条
BlazingThread thread3(&TestClass::talkSomething, msg);
thread3.join();
防止编译。我看到的错误是
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (TestClass::*)(std::__cxx11::basic_string<char>), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>’:
/usr/include/c++/5/functional:1439:12: required from ‘struct std::_Bind_helper<false, void (TestClass::*)(std::__cxx11::basic_string<char>), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>’
/usr/include/c++/5/functional:1462:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (TestClass::*)(std::__cxx11::basic_string<char>); _BoundArgs = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]’
../src/simple-thread.cpp:24:34: required from ‘BlazingThread::BlazingThread(Function&&, Args&& ...)::<lambda()> [with Function = void (TestClass::*)(std::__cxx11::basic_string<char>); Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]’
../src/simple-thread.cpp:24:80: required from ‘struct BlazingThread::BlazingThread(Function&&, Args&& ...) [with Function = void (TestClass::*)(std::__cxx11::basic_string<char>); Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]::<lambda()>’
../src/simple-thread.cpp:21:10: required from ‘BlazingThread::BlazingThread(Function&&, Args&& ...) [with Function = void (TestClass::*)(std::__cxx11::basic_string<char>); Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]’
../src/simple-thread.cpp:121:55: required from here
/usr/include/c++/5/functional:1426:7: error: static assertion failed: Wrong number of arguments for pointer-to-member
static_assert(_Varargs::value
^
下面是一个工作版本,我需要修复它,以便它像 std::thread 一样需要 std::ref 的引用
#include <iostream>
#include <string>
#include <thread>
#include <exception>
#include <utility>
class BlazingThread {
public:
template< typename Function, typename... Args >
explicit BlazingThread( Function&& f, Args&&... args ){
exception = nullptr;
thread = std::thread(([&f,args...](){
try{
//auto caller = std::forward<Function>(f);
//caller(std::forward<Args>(args)...);
auto functionCall = std::bind(std::forward<Function>(f),args...);
functionCall();
}catch(...){
//non handled exception rethrow so we can stop whoever started this mess
//exception = std::make_exception_ptr(BlazingException("An unknown error occurred!"));
}
}));
}
template< typename Function >
explicit BlazingThread( Function&& f){
exception = nullptr;
thread = std::thread(([&](){
try{
//std::bind(f);
std::forward<Function>(f)();
}catch(...){
//non handled exception rethrow so we can stop whoever started this mess
// exception = std::make_exception_ptr(BlazingException("An unknown error occurred!"));
}
}));
}
BlazingThread(BlazingThread && other){
this->thread = std::move(other.thread);
this->exception = std::move(other.exception);
}
BlazingThread(){
}
BlazingThread& operator=(const BlazingThread&) = delete;
BlazingThread& operator=(BlazingThread && other){
this->thread = std::move(other.thread);
this->exception = std::move(other.exception);
return *this;
}
BlazingThread(const BlazingThread& other) = delete;
virtual ~BlazingThread();
void join();
static unsigned int hardware_concurrency(){
return std::thread::hardware_concurrency();
}
private:
std::thread thread;
std::exception_ptr exception;
};
void BlazingThread::join(){
thread.join();
if(this->exception != nullptr){
//an exception was thrown in the thread, lets rethrow it
std::rethrow_exception(this->exception);
}
}
BlazingThread::~BlazingThread() {
// TODO Auto-generated destructor stub
}
void testFunction(){
std::cout<<"tester"<<std::endl;
//throw BlazingException("A planned error!");
}
void testFunction2(int x, int y){
int z = x + y;
std::cout<<"x + y = "<<z<<std::endl;
//throw BlazingException("A planned error!");
}
class TestClass{
public:
TestClass(int newX){
this->x = newX;
}
void talk(){
std::cout<<"this is "<<x<<std::endl;
}
void talking(){
BlazingThread thread2(&TestClass::talk,this);
thread2.join();
std::string msg = "something else ";
BlazingThread thread3(&TestClass::talkSomething,this, msg);
thread3.join();
}
void talkSomething(std::string testMsg){
std::cout<<testMsg<<x<<std::endl;
}
private:
int x;
};
int main() {
try{
TestClass test(3);
test.talking();
BlazingThread thread(testFunction2,2,4);
thread.join();
BlazingThread thread2(testFunction);
thread2.join();
}catch(...){
//std::cout<<"Found BlazingException, what = "<<e.what()<<std::endl;
}
return 0;
}
最佳答案
改变
BlazingThread thread2(&TestClass::talk);
到
BlazingThread thread2(&TestClass::talk, this);
和类似的地方。您需要传递要处理的对象。
当 lambda 超出当前作用域时(就像将它传递给 std 线程时一样),也停止使用 [&]
捕获。但这只是一个运行时错误。
关于c++ - 在 lambda 中传递参数时无法调用指向成员的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49185078/
可以使用 lambda 和函数创建有序对(Lisp 中的缺点),如 Use of lambda for cons/car/cdr definition in SICP 所示。 它也适用于 Python
我正在尝试从另一个调用一个 AWS lambda 并执行 lambda 链接。这样做的理由是 AWS 不提供来自同一个 S3 存储桶的多个触发器。 我创建了一个带有 s3 触发器的 lambda。第一
根据以下源代码,常规 lambda 似乎可以与扩展 lambda 互换。 fun main(args: Array) { val numbers = listOf(1, 2, 3) f
A Tutorial Introduction to the Lambda Calculus 本文介绍乘法函数 The multiplication of two numbers x and y ca
我想弄清楚如何为下面的表达式绘制语法树。首先,这究竟是如何表现的?看样子是以1和2为参数,如果n是 0,它只会返回 m . 另外,有人可以指出解析树的开始,还是一个例子?我一直找不到一个。 最佳答案
在 C++0x 中,我想知道 lambda 函数的类型是什么。具体来说: #include type1 foo(int x){ return [x](int y)->int{return x * y
我在其中一个职位发布中看到了这个问题,它询问什么是 lambda 函数以及它与高阶函数的关系。我已经知道如何使用 lambda 函数,但不太自信地解释它,所以我做了一点谷歌搜索,发现了这个:What
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
我正在处理 MyCustomType 的实例集合如下: fun runAll(vararg commands: MyCustomType){ commands.forEach { it.myM
Brian 在他对问题 "Are side effects a good thing?" 的论证中的前提很有趣: computers are von-Neumann machines that are
在 Common Lisp 中,如果我希望两个函数共享状态,我将按如下方式执行 let over lambda: (let ((state 1)) (defun inc-state () (in
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
作为lambda calculus wiki说: There are several possible ways to define the natural numbers in lambda cal
我有一个数据类,我需要初始化一些 List .我需要获取 JsonArray 的值(我使用的是 Gson)。 我做了这个函数: private fun arrayToList(data: JsonAr
((lambda () )) 的方案中是否有简写 例如,代替 ((lambda () (define x 1) (display x))) 我希望能够做类似的事情 (empty-lam
我在 Java library 中有以下方法: public void setColumnComparator(final int columnIndex, final Comparator colu
我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。 white-pawn-move 函数有效。当我试图将其概括为任一玩家的棋子 (pawn-move) 时,我遇到了非法函数调用。我已经在 repl
考虑这段代码(在 GCC 和 MSVC 上编译): int main() { auto foo = [](auto p){ typedef decltype(p) p_t;
我正在阅读一个在 lambda 内部使用 lambda 的片段,然后我想通过创建一个虚拟函数来测试它,该函数从文件中读取然后返回最大和最小数字。 这是我想出来的 dummy = lambda path
我是一名优秀的程序员,十分优秀!