Boost logo

Boost Users :

From: Monica Gretzer (mogr_progr_at_[hidden])
Date: 2006-10-02 08:18:50


Hi,
   
  Thanks for your answers. Now I wonder how to a certain number of threads which is decided at runtime and can vary between program runs. I send the number of threads as an argument to CreateThreads.
   
  // ----------- create a number of threads that is known at runtime --------
   
  #include <boost/thread/thread.hpp>
  #include <boost/bind.hpp>
  #include <iostream>
   
  using namespace std;
   
  class TestThread
  {
  public:
     void thread(int a);
     void CreateThreads(int nb);
  };
   
  void TestThread::thread(int a)
  {
     cout << "I am thread " << a << "." << endl;
  }
   
  void TestThread::CreateThreads(int nb)
  {
      boost::thread t1( boost::bind( &TestThread::thread, this, 1));
      boost::thread t2( boost::bind( &TestThread::thread, this, 2));
      boost::thread t3( boost::bind( &TestThread::threas, this, 3));
   
      t1.join();
      t2.join();
      t3.join();
  }
   
  int main()
  {
     TestThread t;
      t.CreateThreads(3);
  }
   
  // ------------------------------------------------------------------
   
  The argument to CreateThreads, ("nb", the number of threads that shall be created, in this case 3), will be known at runtime (the number of threads in my application will be in the interval [1,11]).
  
In the code above I don't use the argument ("nb") passed to CreateThreads and the code will always create 3 threads. How do I change the code in CreateThreads to make it use the "nb" argument and create the specified number of threads?
   
  Thanks in advance
  Monica Gretzer
  
Peter Dimov <pdimov_at_[hidden]> skrev:
  Vladimir Prus wrote:
> Monica Gretzer wrote:
>
>> void TestThread::thread(int a)
>> {
>> cout << "I am thread " << a << endl;
>> }
>>
>> void TestThread::CreateThreads()
>> {
>> boost::thread t1( boost::bind( &TestThread::thread, 10));
>> boost::thread t2( boost::bind( &TestThread::thread, 20));
>
> The ::thread method is non-static, and you don't pass any object to
> bind. You should have
>
> boost::bind (&TestThread::thread, some_object, 10)

... or, in this case, since you probably want to call this->thread( 10 ) in
a thread, you need

boost::thread t1( boost::bind( &TestThread::thread, this, 10));
boost::thread t2( boost::bind( &TestThread::thread, this, 20));

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net