|
Boost Users : |
Subject: Re: [Boost-users] functor for boost::thread_group threads: which one is correct?
From: Boost lzw (boostlzw_at_[hidden])
Date: 2009-08-23 22:36:10
Hi Guys,
Sorry for Mathias. Mathias is correct concerning the copy contructor. So my
completed code looks like this:
-----------------------------------------------
// Invoke several functions from multiple threads
#include <iostream>
#include "boost/thread/thread.hpp"
#include "boost/shared_ptr.hpp"
class FileThread
{
public:
void find() { }
void print() { }
};
class group_helper
{
private:
boost::shared_ptr<FileThread> m_object;
public:
group_helper() : m_object(boost::shared_ptr<FileThread>(new FileThread)) {
}
void operator()() { m_object->find(); m_object->print(); }
};
int main()
{
group_helper gp;
boost::thread_group threads;
for (int i = 0; i < 2; ++i)
threads.create_thread(gp);
threads.join_all();
return 0;
}
-----------------------------------------------
On Sun, Aug 23, 2009 at 10:23 PM, Boost lzw <boostlzw_at_[hidden]> wrote:
> Hi Guys,
>
> Steven is correct. After adding the following copy contructor to
> group_helper class:
> group_helper(const group_helper& gp) { std::cout << "group_helper::copy
> ctor called" << std::endl; }
>
> The number of ctors matches that of the dtors. So the threaded code is
> correct thanks to boost::shared_ptr.
>
> Cheers,
> Robert
>
>
>
> On Sun, Aug 23, 2009 at 10:05 PM, Mathias Gaunard <
> mathias.gaunard_at_[hidden]> wrote:
>
>> Boost lzw wrote:
>>
>> class group_helper { private:
>>> boost::shared_ptr<FileThread> m_object; public:
>>> group_helper() : m_object(boost::shared_ptr<FileThread>(new
>>> FileThread))
>>> { std::cout << "group_helper::ctor called" << std::endl;}
>>> void operator()() { m_object->find(); m_object->print(); }
>>> ~group_helper() { std::cout << "group_helper::***dtor called" <<
>>> std::endl; }
>>> };
>>>
>>> int main()
>>> {
>>> group_helper gp;
>>> boost::thread_group threads;
>>> for (int i = 0; i < 1; ++i)
>>> threads.create_thread(gp); threads.join_all();
>>>
>>> return 0;
>>> }
>>> ===========
>>> Output: 1 call to "group_helper::ctor called"
>>> 6 calls to "group_helper::***dtor called"
>>> ===========
>>> If the code is correct, does this mean that with thread_group we can no
>>> longer expect the # of ctors matches that of the dtors in threaded code?
>>>
>>
>> It has nothing to do with threads, it's because you're not displaying
>> calls to the copy constructor.
>>
>>
>> _______________________________________________
>> 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