Thread: Threading with boost

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Threading with boost

    Hi,


    What am i doing wrong ??


    Code:
    #include <boost/thread.hpp>
    #include <vector>
    #include <iostream>
    #include <boost/bind.hpp>
    
    
    using namespace boost;
    using namespace std;
    
    template <typename T, typename T1>
    void task(T& vec, T1 x) {
        for (int i =0; i< 10; i++)
          vec[i] = i+x;
    }
    
    int main () {
        vector <int> vek1(10,0);
        vector <int> vek2(10,0);
        vector <int> vv(20,0);
    
        thread_group tgroup;
    
    
        tgroup.create_thread(bind( &task, vek1,6));
        tgroup.create_thread(bind( &task, vek2,3));
    
    
        // do other stuff
        tgroup.join_all();
    
        for (int i=0; i< 10; i++){
          vv[i] = vek1[i];
          vv[i+10] = vek2[i];
        }
        for (int i = 0; i< 20; i++)
          cout << vv[i] << endl;
        return 0;
    }

    so i want to thread a function and then join the results in one vector.

    when i compile thsi i get tons of error messages that i don't understand:


    Code:
    threads.cpp: In function ‘int main()’:
    threads.cpp:24:45: error: no matching function for call to ‘bind(<unresolved overloaded function type>, std::vector<int>&, int)’
    threads.cpp:24:45: note: candidates are:
    /usr/include/c++/4.6/functional:1444:5: note: template<class _Functor, class ... _ArgTypes> typename std::_Bind_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
    /usr/include/c++/4.6/functional:1471:5: note: template<class _Result, class _Functor, class ... _ArgTypes> typename std::_Bindres_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
    /usr/include/boost/bind/bind.hpp:1728:1: note: template<class A1, class M, class T> boost::_bi::bind_t<typename boost::_bi::dm_result<M T::*, A1>::type, boost::_mfi::dm<M, T>, typename boost::_bi::list_av_1<A1>::type> boost::bind(M T::*, A1)
    /usr/include/boost/bind/bind_mf2_cc.hpp:223:5: note: template<class Rt2, class R, class T, class B1, class B2, class B3, class B4, class B5, class B6, class B7, class B8, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9> boost::_bi::bind_t<Rt2, boost::_mfi::cmf8<R, T, B1, B2, B3, B4, B5, B6, B7, B8>, typename boost::_bi::list_av_9<A1, A2, A3, A4, A5, A6, A7, A8, A9>::type> boost::bind(boost::type<Rt2>, R (T::*)(B1, B2, B3, B4, B5, B6, B7, B8)const, A1, A2, A3, A4, A5, A6, A7, A8, A9)
    /usr/include/boost/bind/bind_mf2_cc.hpp:212:5: note: template<class Rt2, class R, class T, class B1, class B2, class B3, class B4, class B5, class B6, class B7, class B8, class A1, class A2, class ............
    thnx

    b
    Last edited by baxy; 03-05-2014 at 07:18 AM.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It says you're not binding correctly so that's probably where I would look.

    Also, instead of boost, using std::thread and a lambda is pretty simple too.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>threads.cpp:24:45: error: no matching function for call to ‘bind(<unresolved overloaded function type>, std::vector<int>&, int)’
    This pretty much says it all. You are trying to pass a template function to bind without specifying what T and T1 is. bind cannot accept such a function. It wants a callable object.

    To properly compile this, you would have to write
    tgroup.create_thread(bind(&task<vector<int>, int>, vek1, 6));
    Since you are taking the address of the function, the compiler cannot automatically deduce the template parameters.

    But wait, there's more.
    You don't have to use bind. This does the same:
    tgroup.create_thread(&task<vector<int>, int>, vek1, 6);

    But this still has a bug because all arguments passed to threads like this are actually copied. So a different vek1 is passed to task than the original. To fix, use boost::ref (or std::ref):
    tgroup.create_thread(&task<vector<int>, int>, boost::ref(vek1), 6);

    Of course, we don't want to specify the template parameters, so we can fix this by using a lambda:
    Code:
    tgroup.create_thread([&vek1] { task(vek1, 6); });
    Consider also that threads are part of the standard now (e.g. std::thread, std::ref).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    ok thnx that worked. but what if i want to pass a ref of a vector ref. example:


    Code:
    #include <thread>
    #include <vector>
    #include <iostream>
    #include <functional>
    
    
    using namespace std;
    
    template <typename T, typename T1>
    void task(T& vec, T1& x) {
        for (int i =0; i< 9; i++)
          vec[i] = i+x[i+1];
    }
    
    int main () {
        vector <int> vek1(10,1);
        vector <int> vek2(10,5);
        vector <int> vv(20,0);
        vector<vector<int>> xl(2,vek2);
        vector<thread> th(2);
    
    
      for(int i = 0;i< 2; i++)
        thread th[i]([&vek1,&xl[i]] {task(vek1,xl[i]);});
    
      for(int i = 0;i< 2; i++)
        th[i].join();
    
        for (int i=0; i< 10; i++){
          vv[i] = vek1[i];
          vv[i+10] = vek2[i];
        }
        for (int i = 0; i< 20; i++)
          cout << vv[i] << endl;
        return 0;
    }
    i get errors:

    Code:
    threads.cpp: In function ‘int main()’:
    threads.cpp:24:28: error: expected ‘,’ before ‘[’ token
    threads.cpp:24:28: error: expected identifier before ‘[’ token
    threads.cpp: In lambda function:
    threads.cpp:24:47: error: ‘i’ is not captured
    threads.cpp: In function ‘int main()’:
    threads.cpp:24:52: error: variable-sized object ‘th’ may not be initialized

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        vector<thread> th(2);
     
     
      for(int i = 0;i< 2; i++)
        thread th[i]([&vek1,&xl[i]] {task(vek1,xl[i]);});
     
      for(int i = 0;i< 2; i++)
        th[i].join();
    This will never work. You are re-declaring (i.e. hiding) th in the outer scope in your for loop. You can't just declare some variable and initialize it later. Doesn't work that way.
    Also, you can't capture indexes from variables. You can only capture variables.
    You want to do something like

    Code:
    std::vector<std::thread> th;
    for (size_t i = 0; i < 2; i++) // Using size_t to avoid conversion int -> size_t when indexing the vector.
        th.emplace_back([&vek1, &xl,  i] { task(vek1, xl[i]); });
    for (auto & thread : th)
        th.join();
    From bottom down:
    Construct a vector, th, that stores threads.
    Create a new thread inside the vector which calls a lambda function that calls task. We capture the arrays and the index so we can index in the array.
    We then iterate through all objects in th and call join on them.

    Your method does not work since it creates local thread objects in the for loop. You have to either detach them or join them before they get destructed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thank you, thank you, thank you. I like the way you quickly and clearly dissect the problem and provide a solution Thank you once more, everything is working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. erasing elements from a boost::ptr_vector (boost n00b)
    By Marcos in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2006, 12:54 PM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  3. Threading
    By slcjoey in forum C# Programming
    Replies: 2
    Last Post: 03-24-2005, 01:56 AM
  4. Threading
    By vasanth in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-18-2004, 04:55 PM
  5. Threading
    By threads in forum C# Programming
    Replies: 0
    Last Post: 01-17-2003, 11:50 PM

Tags for this Thread