
Hello, I use a thread group for creating a blocking thread calculation. Each thread is created with a boost::bind to a member function, but I would like to create a own class for the thread eg class myThread { public some methods private some properties } The group should be boost::thread_group g; for(int i=0; i < 10; i++) g.add( myThread( i ) ) g.join_all(); Which structure must be implementated for the class myThread and from which class must be derived? Thanks Phil

Hi Philip,
2012/12/30 Philipp Kraus
Which structure must be implementated for the class myThread and from which class must be derived?
no need for derivation. Just implement operator() for your class like so: class myThread { public some methods void operator()() { // this will run in new thread } private some properties } Cheers, Simon -- Szymon Gatner The Lordz Games Studio www.thelordzgamesstudio.com

Am 30.12.2012 um 12:53 schrieb Szymon Gatner:
Hi Philip,
2012/12/30 Philipp Kraus
: Which structure must be implementated for the class myThread and from which class must be derived?
no need for derivation. Just implement operator() for your class like so:
Thanks Simon. The operator() method is equal to the function that I use with boost::bind, do I? Phil

Am 30.12.2012 um 12:53 schrieb Szymon Gatner:
Hi Philip,
2012/12/30 Philipp Kraus
: Which structure must be implementated for the class myThread and from which class must be derived?
no need for derivation. Just implement operator() for your class like so:
class myThread { public some methods
void operator()() { // this will run in new thread }
private some properties }
I get with this use compiler errors, this code boost::thread_group l_group; for(std::size_t i=0; i < 5; ++i) l_group( myThread() ); l_group.join_all(); shows: error: no match for call to '(boost::thread_group) (myThread)' on the loop line I have tested l_group( boost::thread(myThread) ); and creates the error: error: no match for call to '(boost::thread_group) (boost::thread) In my class "myThread" I have a overloaded public method: void operator() (void); How can I fix the error? Thanks Phil

Le 30/12/12 19:20, Philipp Kraus a écrit :
Am 30.12.2012 um 12:53 schrieb Szymon Gatner:
Hi Philip,
2012/12/30 Philipp Kraus
: Which structure must be implementated for the class myThread and from which class must be derived?
no need for derivation. Just implement operator() for your class like so:
class myThread { public some methods
void operator()() { // this will run in new thread }
private some properties } I get with this use compiler errors, this code
boost::thread_group l_group; for(std::size_t i=0; i < 5; ++i)
l_group.join_all();
shows: error: no match for call to '(boost::thread_group) (myThread)'
on the loop line I have tested
l_group( boost::thread(myThread) );
and creates the error:
error: no match for call to '(boost::thread_group) (boost::thread)
In my class "myThread" I have a overloaded public method: void operator() (void);
How can I fix the error?
To create and add a new thread to a thread_group you should use Member function |create_thread()| template<typename F> thread* create_thread(F threadfunc); Effects: Create a new |boost::thread| object as-if by |new thread(threadfunc)| and add it to the group. Postcondition: |this->size()| is increased by one, the new thread is running. Returns: A pointer to the new |boost::thread| object. l_group.create_thread( myThread() ); Best, Vicente

Am 30.12.2012 um 19:31 schrieb Vicente J. Botet Escriba:
Le 30/12/12 19:20, Philipp Kraus a écrit :
Am 30.12.2012 um 12:53 schrieb Szymon Gatner:
Hi Philip,
2012/12/30 Philipp Kraus
: Which structure must be implementated for the class myThread and from which class must be derived?
no need for derivation. Just implement operator() for your class like so:
class myThread { public some methods
void operator()() { // this will run in new thread }
private some properties } I get with this use compiler errors, this code
boost::thread_group l_group; for(std::size_t i=0; i < 5; ++i)
l_group.join_all();
shows: error: no match for call to '(boost::thread_group) (myThread)'
on the loop line I have tested
l_group( boost::thread(myThread) );
and creates the error:
error: no match for call to '(boost::thread_group) (boost::thread)
In my class "myThread" I have a overloaded public method: void operator() (void);
How can I fix the error?
To create and add a new thread to a thread_group you should use Member function create_thread()
template<typename F> thread* create_thread(F threadfunc); Effects: Create a new boost::thread object as-if by new thread(threadfunc) and add it to the group.
Postcondition: this->size() is increased by one, the new thread is running.
Returns: A pointer to the new boost::thread object.
l_group.create_thread( myThread() );
Thanks for this hint, I don't find this passage on the documentation at the moment, but with create_thread it works Phil
participants (3)
-
Philipp Kraus
-
Szymon Gatner
-
Vicente J. Botet Escriba