So this is the first time i've done any multi-threading in C++. Right now my code is throwing some kind of error i can't identify but it seems to indicate that it occurs when i include boost/thread.hpp.
The error look something like this.
In file included
from /usr/include/boost/mem_fn.hpp:22:0,
from /usr/include/boost/bind/bind.hpp:26,
from /usr/include/boost/bind.hpp:22,
from /usr/include/boost/thread/detail/thread.hpp:22,
from /usr/include/boost/thread/thread.hpp:22,
from /usr/include/boost/thread.hpp:13,
from processes/Video_process.h:8,
from processes/Video_process.cpp:7:
/usr/include/boost/bind/mem_fn.hpp: In member function ‘R& boost::_mfi::dm<R, T>::operator()(T*) const [with R = void*(void*), T = Video_process]’:
/usr/include/boost/bind/bind.hpp:243:60: instantiated from ‘R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = void* (&)(void*), F = boost::_mfi::dm<void*(void*), Video_process>, A = boost::_bi::list0, A1 = boost::_bi::value<Video_process*>]’
/usr/include/boost/bind/bind_template.hpp:20:59: instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void* (&)(void*), F = boost::_mfi::dm<void*(void*), Video_process>, L = boost::_bi::list1<boost::_bi::value<Video_process*> >, boost::_bi::bind_t<R, F, L>::result_type = void* (&)(void*)]’
/usr/include/boost/thread/detail/thread.hpp:61:17: instantiated from ‘void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void* (&)(void*), boost::_mfi::dm<void*(void*), Video_process>, boost::_bi::list1<boost::_bi::value<Video_process*> > >]’
processes/Video_process.cpp:240:1: instantiated from here
/usr/include/boost/bind/mem_fn.hpp:342:23: error: invalid use of non-static member function
my codes a bit long so i dont know which bits if any would be useful but here are some anyways...
1.
boost::unique_lock<boost::mutex> lock(mx);
lock.lock();
(...) //stuff
is_data_ready = 1;
lock.unlock();
2.
stserver = boost::shared_ptr<boost::thread>
(new boost::thread(boost::bind(&Video_process::streamServer, this)));
Here is how streamServer is declared in the header
void* streamServer(void* arg);
The error that shows in boost::bind basically says that your member function void* Video_process::streamServer(void* arg) expects two arguments this and void*. Your code only binds the first argument.
If you'd like to use it with boost::thread it shouldn't require the second void* argument or have it bound to a constant. Also, boost::thread discards the return value of the functor, so you may like to use void, i.e.:
struct Video_process {
void streamServer();
};
boost::thread(boost::bind(&Video_process::streamServer, this));
// or simply
boost::thread(&Video_process::streamServer, this);
With regards to:
boost::unique_lock<boost::mutex> lock(mx);
lock.lock();
The first line locks the mutex in the constructor, so the second line with explicit lock is unnecessary.
Related
While working on a project I encounter a situation std::apply does not forward rvalue references from std::tuple created by std::forward_as_tuple *IF* resulting std::tuple is stored in a variable! However if std::forward_as_tuple result is not stored in a variable, but is just passed as a second argument to std::apply then it works and rvalue reference gets perfectly forwarded.
I tried many options including using different types for std::tuple, like
decltype(auto) t = forward_as_tuple(1, std::move(r))
auto t = forward_as_tuple(1, std::move(r))
auto&& t = forward_as_tuple(1, std::move(r))
auto& t = forward_as_tuple(1, std::move(r))
Nothing helped to store a tuple in a variable and then pass it to std::apply. It appears like lvalue reference being forwarded at the end to std::__invoke by std::apply...
There is a godbolt link to my code: https://godbolt.org/z/24LYP5
Code snippet
#include <functional>
#include <iostream>
auto product(int l, int&& r) { return l * r; }
static void test_not_works()
{
int r = 2;
decltype(auto) t = std::forward_as_tuple(1, std::move(r));
std::apply(product, t);
}
static void test_works()
{
int r = 2;
std::apply(product, std::forward_as_tuple(1, std::move(r)));
}
Error message
In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/functional:54:0,
from <source>:1:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple: In instantiation of 'constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = int (&)(int, int&&); _Tuple = std::tuple<int&&, int&&>&; long unsigned int ..._Idx = {0, 1}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1>]':
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:1671:31: required from 'constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = int (&)(int, int&&); _Tuple = std::tuple<int&&, int&&>&]'
<source>:10:26: required from here
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:1662:27: error: no matching function for call to '__invoke(int (&)(int, int&&), int&, int&)'
return std::__invoke(std::forward<_Fn>(__f),
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
std::get<_Idx>(std::forward<_Tuple>(__t))...);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:41:0,
from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/functional:54,
from <source>:1:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
__invoke(_Callable&& __fn, _Args&&... __args)
^~~~~~~~
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h: In substitution of 'template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = int (&)(int, int&&); _Args = {int&, int&}]':
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:1662:27: required from 'constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = int (&)(int, int&&); _Tuple = std::tuple<int&&, int&&>&; long unsigned int ..._Idx = {0, 1}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1>]'
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:1671:31: required from 'constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = int (&)(int, int&&); _Tuple = std::tuple<int&&, int&&>&]'
<source>:10:26: required from here
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5: error: no type named 'type' in 'struct std::__invoke_result<int (&)(int, int&&), int&, int&>'
Compiler returned: 1
When rvalue references are passed to std::forward_as_tuple, it constructs a std::tuple of rvalue references. So first of all, declaring t as auto t = std::forward_as_tuple(...) is fine, the "rvalueness" of the object is encoded inside the type generated by std::forward_as_tuple.
But then note that there is something special about the first argument to std::apply: product takes an int parameter by value, and a second one as int&&, i.e., an rvalue-reference. Calling such a function obviously requires the second argument to be an rvalue reference, but this does only work if you make sure it is one. Hence:
auto t = std::forward_as_tuple(1, std::move(r));
std::apply(product, std::move(t));
// ^^^^^^^ cast to rvalue here
Another possible fix is to change product to accept to plain ints by value (in case of an int, there is no performance hit anyway). Then, you can pass t as an lvalue, too.
std::apply perfect-forwards the tuple to std::get to access elements of the tuple. std::get returns an lvalue reference if its tuple argument is an lvalue. Since your t variable is an lvalue, std::apply calls product with an int& instead of an int&&.
I can't speak for the motivation behind having it work this way despite the tuple explicitly holding an rvalue reference. However, a simple way to get the same behaviour as a temporary here is to use std::move to produce an xvalue, which will handle stored references in the way you want:
std::apply(product, std::move(t));
Note, however, that unlike using just t, any non-reference types in the tuple should now be treated as moved from. I'm unsure if there's a simple way to both use the tuple's reference types and treat non-reference types as lvalues for an lvalue tuple. Granted this won't be an issue when creating the tuple using forward_as_tuple since it will always contain references.
I have followed the Boost tutorial, "Passing Slots (Intermediate)", at reference [ 1 ] to the letter yet I am getting a weird error with my code that I cannot decipher or find any help with regard to. My code is below:
[ 1 ] - http://www.boost.org/doc/libs/1_55_0/doc/html/signals2/tutorial.html#idp204830936
namespace GekkoFyre {
class TuiHangouts {
private:
typedef boost::signals2::signal<void()> onRosterUpdate;
typedef onRosterUpdate::slot_type onRosterUpdateSlotType;
void logMsgs(std::string message, const xmppMsgType &msgType);
void logMsgsDrawHistory();
// Slots
boost::signals2::connection doOnRosterUpdate(const onRosterUpdateSlotType &slot);
onRosterUpdate rosterUpdate;
};
}
boost::signals2::connection GekkoFyre::TuiHangouts::doOnRosterUpdate(
const GekkoFyre::TuiHangouts::onRosterUpdateSlotType &slot)
{
return rosterUpdate.connect(slot);
}
The problem is with this specifically:
void GekkoFyre::TuiHangouts::logMsgs(std::string message, const xmppMsgType &msgType)
{
doOnRosterUpdate(&GekkoFyre::TuiHangouts::logMsgsDrawHistory);
}
I get the error:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
from /usr/include/boost/function.hpp:64,
from /usr/include/boost/signals2/signal.hpp:18,
from /usr/include/boost/signals2.hpp:19,
from /home/phobos/Programming/gecho/src/tui/chat.hpp:47,
from /home/phobos/Programming/gecho/src/tui/chat.cpp:35:
/usr/include/boost/function/function_template.hpp: In instantiation of 'void boost::function0<R>::assign_to(Functor) [with Functor = void (GekkoFyre::TuiHangouts::*)(); R = void]':
/usr/include/boost/function/function_template.hpp:722:7: required from 'boost::function0<R>::function0(Functor, typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), int>::type) [with Functor = void (GekkoFyre::TuiHangouts::*)(); R = void; typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), int>::type = int]'
/usr/include/boost/function/function_template.hpp:1071:16: required from 'boost::function<R()>::function(Functor, typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), int>::type) [with Functor = void (GekkoFyre::TuiHangouts::*)(); R = void; typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), int>::type = int]'
/usr/include/boost/function/function_template.hpp:1126:5: required from 'typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), boost::function<R()>&>::type boost::function<R()>::operator=(Functor) [with Functor = void (GekkoFyre::TuiHangouts::*)(); R = void; typename boost::enable_if_c<(boost::type_traits::ice_not<(boost::is_integral<Functor>::value)>::value), boost::function<R()>&>::type = boost::function<void()>&]'
/usr/include/boost/signals2/detail/slot_template.hpp:160:24: required from 'void boost::signals2::slot<R(Args ...), SlotFunction>::init_slot_function(const F&) [with F = void (GekkoFyre::TuiHangouts::*)(); SlotFunction = boost::function<void()>; R = void; Args = {}]'
/usr/include/boost/signals2/detail/slot_template.hpp:85:27: required from 'boost::signals2::slot<R(Args ...), SlotFunction>::slot(const F&) [with F = void (GekkoFyre::TuiHangouts::*)(); SlotFunction = boost::function<void()>; R = void; Args = {}]'
/home/phobos/Programming/gecho/src/tui/chat.cpp:802:74: required from here
/usr/include/boost/function/function_template.hpp:924:9: error: no class template named 'apply' in 'struct boost::detail::function::get_invoker0<boost::detail::function::member_ptr_tag>'
handler_type;
If anyone could assist with this then it would be immensely appreciated. As I said previously, I did some research and couldn't really find much of anything. It seems kind of unique to me, perhaps, and I did follow the tutorial to the letter. I know this isn't always the right thing to do but from what I can gather through research, this code should work.
&GekkoFyre::TuiHangouts::logMsgsDrawHistory is a member function pointer, which has the type void (GekkoFyre::TuiHangouts::*)(). This isn't like any other function, and so it cannot be called like any other function. Signals2 will attempt to call this with the syntax func(), but there is no this pointer here. To provide it with a this pointer, you would use the syntax (p->*func)(). The p here becomes the this pointer. boost::bind (also in the C++ standard since 2011 called std::bind) will wrap this up as a function object that can be called as func() by calling (p->*func)().
Let me start with explaining what I try to accomplish. I need to create a type-erased functor (using templates and virtual functions) that would be able to "emplace" a new object in the storage of message queue for the RTOS I'm developing. Such "trickery" is required, because I want most of message queue's code to be non-templated, with only the parts that really need the type info implemented as such type-erased functors. This is a project for embedded microcontrollers (*), so please assume that I just cannot make whole message queue with a template, because ROM space is not unlimited in such environment.
I already have functors that can "copy-construct" and "move-construct" the object into the queue's storage (for "push" operations) and I also have a functor that can "swap" the object out of the queue's storage (for "pop" operations). To have a complete set I need a functor that will be able to "emplace" the object into the queue's storage.
So here is the minimum example that exhibits the problem I'm facing with creating it. Do note that this is a simplified scenario, which doesn't show much of the boiler plate (there are no classes, no inheritance and so on), but the error is exactly the same, as the root cause is probably the same too. Also please note, that the use of std::bind() (or a similar mechanism that would NOT use dynamic allocation) is essential to my use case.
#include <functional>
template<typename T, typename... Args>
void emplacer(Args&&... args)
{
T value {std::forward<Args>(args)...};
}
template<typename T, typename... Args>
void emplace(Args&&... args)
{
auto boundFunction = std::bind(emplacer<T, Args...>,
std::forward<Args>(args)...);
boundFunction();
}
int main()
{
int i = 42;
emplace<int>(i); // <---- works fine
emplace<int>(42); // <---- doesn't work...
}
When compiled on PC with g++ -std=c++11 test.cpp the first instantiation (the one using a variable) compiles with no problems, but the second one (which uses a constant 42 directly) throws this error messages:
test.cpp: In instantiation of ‘void emplace(Args&& ...) [with T = int; Args = {int}]’:
test.cpp:21:17: required from here
test.cpp:13:16: error: no match for call to ‘(std::_Bind<void (*(int))(int&&)>) ()’
boundFunction();
^
In file included from test.cpp:1:0:
/usr/include/c++/4.9.2/functional:1248:11: note: candidates are:
class _Bind<_Functor(_Bound_args...)>
^
/usr/include/c++/4.9.2/functional:1319:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int&&); _Bound_args = {int}]
operator()(_Args&&... __args)
^
/usr/include/c++/4.9.2/functional:1319:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9.2/functional:1315:37: error: cannot bind ‘int’ lvalue to ‘int&&’
= decltype( std::declval<_Functor>()(
^
/usr/include/c++/4.9.2/functional:1333:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int&&); _Bound_args = {int}]
operator()(_Args&&... __args) const
^
/usr/include/c++/4.9.2/functional:1333:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9.2/functional:1329:53: error: invalid initialization of reference of type ‘int&&’ from expression of type ‘const int’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9.2/functional:1347:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int&&); _Bound_args = {int}]
operator()(_Args&&... __args) volatile
^
/usr/include/c++/4.9.2/functional:1347:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9.2/functional:1343:70: error: invalid initialization of reference of type ‘int&&’ from expression of type ‘volatile int’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9.2/functional:1361:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int&&); _Bound_args = {int}]
operator()(_Args&&... __args) const volatile
^
/usr/include/c++/4.9.2/functional:1361:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9.2/functional:1357:64: error: invalid initialization of reference of type ‘int&&’ from expression of type ‘const volatile int’
typename add_cv<_Functor>::type>::type>()(
I tried looking for inspiration in other places, but Intel's TBB library which has a similar code (concurent_queue) with similar functionality (there's an emplace function) is actually no emplace at all - it constructs the object instantly and just "moves" it into the queue...
Any idea what's wrong with the code above? I suppose it's something really small, but I just cannot solve that myself...
(*) - https://github.com/DISTORTEC/distortos
You've already had an explanation of how that is just how std::bind works (it turns everything into an lvalue), and to use a lambda instead. However, that is not exactly trivial. Lambdas can capture by value, or by reference. You sort of need a mix of both: rvalue references should be assumed to possibly reference temporaries, so should be captured by value, with move semantics. (Note: that does mean that the original object gets moved from before the lambda gets invoked.) Lvalue references should be captured by reference, for probably obvious reasons.
One way to make this work is to manually put the captured arguments in a tuple of lvalue reference types and non-reference types, and unpack when you want to invoke the function:
template <typename T>
struct remove_rvalue_reference {
typedef T type;
};
template <typename T>
struct remove_rvalue_reference<T &&> {
typedef T type;
};
template <typename T>
using remove_rvalue_reference_t = typename remove_rvalue_reference<T>::type;
template <typename F, typename...T, std::size_t...I>
decltype(auto) invoke_helper(F&&f, std::tuple<T...>&&t,
std::index_sequence<I...>) {
return std::forward<F>(f)(std::get<I>(std::move(t))...);
}
template <typename F, typename...T>
decltype(auto) invoke(F&&f, std::tuple<T...>&&t) {
return invoke_helper<F, T...>(std::forward<F>(f), std::move(t),
std::make_index_sequence<sizeof...(T)>());
}
template<typename T, typename... Args>
void emplacer(Args&&... args) {
T{std::forward<Args>(args)...};
}
template<typename T, typename...Args>
void emplace(Args&&...args)
{
auto boundFunction =
[args=std::tuple<remove_rvalue_reference_t<Args>...>{
std::forward<Args>(args)...}]() mutable {
invoke(emplacer<T, Args...>, std::move(args));
};
boundFunction();
}
When calling emplace with args T1 &, T2 &&, the args will be captured in a tuple<T1 &, T2>. The tuple gets unpacked (thanks to #Johannes Schaub - litb for the basic idea) when finally invoking the function.
The lambda needs to be mutable, to allow that captured tuple to be moved from when invoking the function.
This uses several C++14 features. Most of these can be avoided, but I don't see how to do this without the ability to specify an initialiser in the capture list: C++11 lambdas can only capture by reference (which would be reference to the local variable), or by value (which would make a copy). In C++11, I think that means the only way to do it is not use a lambda, but effectively re-create most of std::bind.
To expand on #T.C.'s comment, you can make the code work by changing the type of the created emplacer.
auto boundFunction = std::bind(emplacer<T, Args&...>,
std::forward<Args>(args)...);
Notice the & right after Args. The reason is you're passing an rvalue to the emplace function which in turn creates emplacer(int&&). std::bind however always passes an lvalue (because it comes from its internals). With the change in place, the signature changes to emplacer(int&) (after reference collapsing) which can bind to an lvalue.
I have this simple class:
struct Worker
{
Worker() : done{false} {}
Worker(const Worker& rhs) : done{rhs.done}, qworker{} {}
Worker(Worker &&rhs) : done{rhs.done}
{
qworker = std::move(rhs.qworker);
}
...
}
this compile fine with gcc-4.7.2 but if I try to use this version I obtain an error
struct Worker
{
Worker() : done{false} {}
Worker(const Worker& rhs) : done{rhs.done}, qworker{} {}
Worker(Worker &&rhs) : done{rhs.done}
, qworker{std::move(rhs.qworker)} // <- ERROR
{
}
...
}
Why?
In file included from tlog.cpp:8:0:
log11.hpp: In member function ‘void Log11::Worker::run()’:
log11.hpp:34:29: error: ‘class std::vector<std::function<void()> >’ has no member named ‘pop_front’
In file included from /usr/include/c++/4.7/thread:39:0,
from tlog.cpp:3:
/usr/include/c++/4.7/functional: In instantiation of ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::vector<std::function<void()> >; _ArgTypes = {}]’:
/usr/include/c++/4.7/functional:2298:6: required from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::vector<std::function<void()> >; _Res = void; _ArgTypes = {}; typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void()>::_Useless]’
log11.hpp:20:78: required from here
/usr/include/c++/4.7/functional:1926:2: error: no match for call to ‘(std::vector<std::function<void()> >) ()’
According to the C++11 standard std::function has an unconstrained constructor template that accepts any argument type:
template<class F> function(F f);
When you say qworker{std::move(rhs.qworker)} this first attempts to call a constructor taking std::initializer_list<std::function<void()>>. Because of the unconstrained constructor template shown above, a std::function<void()> can be constructed from any type, so you get an initializer_list with one member, like this:
{ std::function<void()>{std::move(rhs.qworker)} }
This is invalid, because rhs.qworker is not a callable object, but the error only happens when you try to invoke the function objects.
If you say qworker(std::move(rhs.qworker)) then the initializer list constructor is not a candidate and the move constructor is called instead.
There is a defect report against the standard (LWG 2132) which fixes this by preventing the function(F) constructor template being called unless the argument is a callable object. That prevents an initializer_list<function<void()>> being created, and instead qworker{std::move(rhs.qworker)} calls the move constructor, as intended. GCC 4.7 does not implement the resolution for LWG 2132, but GCC 4.8 does.
I have a class hierarchy like this one (this is the actual class but I cleaned it up):
class Notifiable
{
public:
void notify();
}
template <class Exp>
class Batch : public Notifiable
{
public:
void run();
}
void Batch<Exp>::run()
{
done.clear();
generator->resetGeneration();
while(generator->hasMoreParameters())
{
// Lock for accessing active
std::unique_lock<std::mutex> lock(q_mutex, std::adopt_lock);
// If we've less experiments than threads
if (active.size() < threads)
{
Configuration conf = generator->generateParameters();
Exp e(executable, conf);
//std::weak_ptr<Batch<Exp>> bp;
//bp.reset(this);
std::thread t(&Exp::run, e, *this);
std::thread::id id = t.get_id();
active.insert(id);
t.detach();
}
q_control.wait(lock, [this] { return active.size() < threads; } );
}
}
class Experiment
{
public:
void run(Notifiable& caller)
{
do_stuff();
caller.notify();
}
virtual void do_stuff() = 0;
}
class MyExperiment : public Experiment
{
public:
void do_stuff()
{
// do my stuff
}
}
I then instantiate a Batch<MyExperiment> object and call run(), using this code:
Batch<ELExperiment> b(pex, options["name"].as<string>(), options["executable"].as<string>());
b.run();
but I get this at compile-time:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void (Experiment::*)(Notifiable&)>, MyExperiment, Batch<MyExperiment> >':
/opt/local/include/gcc47/c++/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/functional:1563:61: required from 'struct std::_Bind_simple<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/thread:133:9: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Experiment::*)(Notifiable&); _Args = {MyExperiment&, Batch<MyExperiment>&}]'
json2cli/batch.hh:86:46: required from 'void Batch<Exp>::run() [with Exp = MyExperiment]'
json2cli/main.cpp:113:15: required from here
/opt/local/include/gcc47/c++/type_traits:1834:9: error: no match for call to '(std::_Mem_fn<void (Experiment::*)(Notifiable&)>) (MyExperiment, Batch<MyExperiment>)'
In file included from /opt/local/include/gcc47/c++/memory:81:0,
from json2cli/parameterexpression.hh:19,
from json2cli/main.cpp:13:
/opt/local/include/gcc47/c++/functional:525:11: note: candidates are:
/opt/local/include/gcc47/c++/functional:548:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:548:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment&'
/opt/local/include/gcc47/c++/functional:553:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:553:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment*'
/opt/local/include/gcc47/c++/functional:559:2: note: template<class _Tp> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&, _ArgTypes ...) const [with _Tp = _Tp; _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:559:2: note: template argument deduction/substitution failed:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits:1834:9: note: cannot convert 'std::declval<Batch<MyExperiment> >()' (type 'Batch<MyExperiment>') to type 'Notifiable&'
It looks like I can't just expect to generalize any Batch<Exp> into a Notifiable for function calling. Can you confirm that?
Update sorry, I thought I could avoid dumping all of my code inside the question, but in fact there must be something wrong in the way I spawn the thread for Batch<Exp>::run(). There are still a couple of details missing, but I don't really think they are related (e.g. how I generate the parameters for the experiment).
Thanks
Your error is not in the code that you show to us, Some where in the code you try to bind your run and create a thread using std::thread and that's the problem, since it can't create a correct struct for your bound function and simplest workaround is to write your own wrapper:
template< class Expr >
struct my_bind {
my_bind( Expr& e, Notifiable& n ) : e_( e ), n_(n) {}
void operator()() {e_.run(n_);}
Expr& e_;
Notifiable& n_;
};
And then use your own wrapper to start the function, I can't say for sure but I think this is a bug in compiler (all compilers have this bug: GCC, MSVC, ...) that when you expression get complicated they fail to use it in std::bind!!
Change
std::thread t(&Exp::run, e, *this);
to
std::thread t([](Exp&& e, Batch& b) { e.run(b); }, std::move(e), std::ref(*this));
Or if you really intended for the thread to inherit a copy from *this:
std::thread t([](Exp&& e, Batch&& b) { e.run(b); }, std::move(e), *this);
The root of the error (at least the one referenced by the messages) is the semantics of that particular std::thread constructor (which I'm not going to expose here, it's sort of gory). If you're already familiar with the semantics std::bind (which do have their own quirks), you can defer to it:
std::thread t(std::bind(&Exp::run, std::move(e), std::ref(*this));
(Once again std::ref(*this) can be substituted by *this depending on what you want.)