boost-bind

Post callbacks to a task queue using boost::bind

血红的双手。 提交于 2021-01-27 10:41:53
问题 Suppose I have a function called subscribe() that takes a callback handler, which will be called when the event is triggered. Now, I have another version, called subscribe2() . Everything is the same except that, when triggered, it needs to post it to an event queue. It is implemented using the original subscribe() , with a helper funciton called helper() . All it does is to bind the original handler and whatever additional arguments into a functor, and call postToEventQueue() . Now, I wonder

Post callbacks to a task queue using boost::bind

倾然丶 夕夏残阳落幕 提交于 2021-01-27 10:40:35
问题 Suppose I have a function called subscribe() that takes a callback handler, which will be called when the event is triggered. Now, I have another version, called subscribe2() . Everything is the same except that, when triggered, it needs to post it to an event queue. It is implemented using the original subscribe() , with a helper funciton called helper() . All it does is to bind the original handler and whatever additional arguments into a functor, and call postToEventQueue() . Now, I wonder

Crash related to boost::function usage in thread pool

给你一囗甜甜゛ 提交于 2020-01-25 11:00:35
问题 I am trying to implement thread pool in C++ using pthread. I want to encapsulate logic related to threads management in one object which is taking ownership of these threads. That means whenever this object is destroyed, threads must be stopped and cleaned up. I've been testing my code and it turns out that I get segmentation fault when I destroy WorkerThreadManager object while there is boost::function called. See the code and backtrace from GDB. I don't really understand why it happens, as

boost::bind composition inside io_service::post function

五迷三道 提交于 2020-01-15 03:58:41
问题 Given the following class class task_counter { public: task_counter(short, boost::asio::io_service&); ~task_counter(void); template<typename CompletionHandler> void exec_task(CompletionHandler handler) { grant_access(); io_.post(boost::bind(&task_counter::exec_and_decrease_counter<CompletionHandler>, this, handler)); } template<typename CompletionHandler> void exec_and_decrease_counter(CompletionHandler handler) { handler(); decrease_counter(); } private: .... } Method exec_task is called by

How can I search a container of objects for a data member value?

浪尽此生 提交于 2020-01-13 17:03:10
问题 I have an object type like this: struct T { int x; bool y; }; and a container of them like this: std::vector<T> v; and a burning desire to determine — in a single statement — whether any of the elements of v have y == true . This likely involves std::find_if . My understanding is that std::bind and boost::bind are for member functions and cannot be applied to member data. Because I dislike them, I wish to avoid: comparison functions/functors loops Because my environment is C++03, the

C ++ Boost Bind Performance

流过昼夜 提交于 2020-01-13 09:47:05
问题 Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ? 回答1: Maybe, may not be. It depends. The result of std::bind (or also boost::bind ) is a so-called "bind expression", which has an un­know­able type determined by the implementation. This type is a Callable , and it is convertible to an in­stance of std::function (or boost::function ). Internally, function (may) use type erasure to handle various complex, stateful "call­able objects". This

How can I boost::bind to a member of a managed class which passes and returns a std::string?

喜你入骨 提交于 2020-01-07 03:36:09
问题 I'm trying to do something very similar to this but I'm struggling to pass the string to/from the callback. This is a pared down version of the code I'm trying to run: using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // The unmanaged boost function prototype the native library wants to bind to typedef boost::function<std::string(const std::string&)> MyNativeCallback; // The unmanaged library I'm trying to wrap class MyUnmanagedClass { public:

Access boost::function arguments

不问归期 提交于 2020-01-06 09:33:46
问题 Is it possible to access the arguments contained in a boost::function type? I'd like to be able to retrieve the address of the function to be called, and the values of the arguments provided for that function. 回答1: boost::function erases the implementation type, but if you know it, you can cast to it; since boost::function are comparable by value (== !=) the information is clearly available. It looks like (from the function_base superclass of functionN) you can get the implementation object

Access boost::function arguments

心已入冬 提交于 2020-01-06 09:33:13
问题 Is it possible to access the arguments contained in a boost::function type? I'd like to be able to retrieve the address of the function to be called, and the values of the arguments provided for that function. 回答1: boost::function erases the implementation type, but if you know it, you can cast to it; since boost::function are comparable by value (== !=) the information is clearly available. It looks like (from the function_base superclass of functionN) you can get the implementation object

Why boost::bind incompatible with forward declaration?

回眸只為那壹抹淺笑 提交于 2020-01-04 07:49:07
问题 boost::bind is unable to bind parameters declared via a forward declaration. Can anyone explain why? Is this a boost bug? Sample code: #include "boost/function.hpp" #include "boost/bind.hpp" #include <vector> #include <iostream> class B; class A { public: A() {} void func1(int i) { std::cout << __PRETTY_FUNCTION__ << "(" << i << ")\n"; } void func2(const std::string& s) { std::cout << __PRETTY_FUNCTION__ << "(" << s << ")\n"; } void func3(const B& b) { std::cout << __PRETTY_FUNCTION__ << "("