Boost threads with arguments
I am trying to pass two arguments to a boost thread using this call: boost::thread workerthread(&SshClient::SSHconnect,&sshclt,1,provec); the method for SSHconnect is: int SSHconnect(int,vector<string>); When I try to make my program I get the errors: /usr/include/boost/bind/bind.hpp: In member function ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = int (SshClient::*)(int, std::vector<std::basic_string<char> >), A = boost::_bi::list0, A1 = boost::_bi::value<SshClient*>, A2 = boost::_bi::value<int>]’: /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, F = int (SshClient::*)(int, std::vector<std::basic_string<char> >), L = boost::_bi::list2<boost::_bi::value<SshClient*>, boost::_bi::value<int> >, boost::_bi::bind_t<R, F, L>::result_type = 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, int (SshClient::*)(int, std::vector<std::basic_string<char> >), boost::_bi::list2<boost::_bi::value<SshClient*>, boost::_bi::value<int> >
]’ terminal.cpp:271:1: instantiated from here /usr/include/boost/bind/bind.hpp:313:9: error: no match for call to ‘(boost::_mfi::dm<int(int, std::vector<std::basic_string<char> >), SshClient>) (SshClient*&, int&)’ /usr/include/boost/bind/mem_fn.hpp:314:34: note: candidates are: /usr/include/boost/bind/mem_fn.hpp:340:9: note: R& boost::_mfi::dm<R, T>::operator()(T*) const [with R = int(int, std::vector<std::basic_string<char> >), T = SshClient] /usr/include/boost/bind/mem_fn.hpp:340:9: note: candidate expects 1 argument, 2 provided /usr/include/boost/bind/mem_fn.hpp:345:15: note: const R& boost::_mfi::dm<R, T>::operator()(const T*) const [with R = int(int, std::vector<std::basic_string<char> >), T = SshClient] /usr/include/boost/bind/mem_fn.hpp:345:15: note: candidate expects 1 argument, 2 provided /usr/include/boost/bind/mem_fn.hpp:350:33: note: template<class U> const R& boost::_mfi::dm::operator()(const U&) const [with U = U, R = int(int, std::vector<std::basic_string<char> >), T = SshClient] /usr/include/boost/bind/mem_fn.hpp:357:9: note: R& boost::_mfi::dm<R, T>::operator()(T&) const [with R = int(int, std::vector<std::basic_string<char> >), T = SshClient] /usr/include/boost/bind/mem_fn.hpp:357:9: note: candidate expects 1 argument, 2 provided /usr/include/boost/bind/mem_fn.hpp:362:15: note: const R& boost::_mfi::dm<R, T>::operator()(const T&) const [with R = int(int, std::vector<std::basic_string<char> >), T = SshClient] /usr/include/boost/bind/mem_fn.hpp:362:15: note: candidate expects 1 argument, 2 provided make[2]: *** [terminal.o] Error 1 make[2]: Leaving directory `/home/deadpickle/hprcc_terminal/src' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/home/deadpickle/hprcc_terminal' make: *** [all] Error 2
Quite a long error. Not sure what to make of it and could use some help. thank you. -- Jamie Ryan Lahowetz University of Nebraska - Lincoln Graduate Student - Earth and Atmospheric Sciences 402.304.0766 jlahowetz@gmail.com
Hi, On 03.06.2012 22:11, Jamie Lahowetz wrote:
I am trying to pass two arguments to a boost thread using this call: boost::thread workerthread(&SshClient::SSHconnect,&sshclt,1,provec);
the method for SSHconnect is: int SSHconnect(int,vector<string>);
You will have to use boost::bind to bind the instance and additional arguments to your thread. See for example http://blog.emptycrate.com/node/277. Seemingly, what you originally wanted to use was the thread constructor with arguments <http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructor>. However, notice that it actually will create a copy of your object, which is not what you want. You will want to use bind with pointer to members <http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#with_member_pointers>. So _this_ will refer to your object inside the newly created thread. Regards, Robert
participants (2)
-
Jamie Lahowetz
-
Robert Abel