Chris Ross | 22 May 17:15

Boost 1.35 threads library question


  Hi there.  This is an introductory question as I've just started to
play with boost libraries.  If this would be better on the threads-devel
list, let me know, but I thought starting here would be good.

  I was playing with the threads library in 1.35, and I noticed that I
can't (with gcc 4.3 on Linux) put boost::thread objects into an STL
container.  I think I was trying with a std::vector, in case that
matters.

  I was reading in some of the C++0x documents that it's expected that
the standard library thread implementation is expected to be able to
have this work.  The boost documentation doesn't explicitly say whether
it should work or not.  Is being non-copyable but movable enough to make
this work?  Or, would an object have to be copyable to be put into an
STL container?  I thought that was not the case, but.

  Thanks...

                           - Chris
James Sutherland | 22 May 17:51
Picon

Re: Boost 1.35 threads library question


>  I was playing with the threads library in 1.35, and I noticed that I
> can't (with gcc 4.3 on Linux) put boost::thread objects into an STL
> container.  I think I was trying with a std::vector, in case that
> matters.

You may be interested in the thread_group
	http://www.boost.org/doc/libs/1_35_0/doc/html/thread.html
Also check out
	http://threadpool.sourceforge.net/

James
Chris Ross | 22 May 18:04

Re: Boost 1.35 threads library question

On Thu, 2008-05-22 at 09:51 -0600, James Sutherland wrote:
> >  I was playing with the threads library in 1.35, and I noticed that I
> > can't (with gcc 4.3 on Linux) put boost::thread objects into an STL
> > container.  I think I was trying with a std::vector, in case that
> > matters.
> 
> You may be interested in the thread_group
> 	http://www.boost.org/doc/libs/1_35_0/doc/html/thread.html
> Also check out
> 	http://threadpool.sourceforge.net/

  thread_group doesn't allow me to iterate the group.  It only will
allow me to join_all or interrupt_all, but not perform arbitrary actions
on all of them, or iterate them so I can do whatever I need/want...

  I was mostly inquiring because it was suggested that it should work in
the upcoming standards, so I was wondering if it was supposed to work in
boost 1.35.

                      - Chris
Anthony Williams | 22 May 18:17
Picon

Re: Boost 1.35 threads library question

Chris Ross <cross+boost <at> distal.com> writes:

>   Hi there.  This is an introductory question as I've just started to
> play with boost libraries.  If this would be better on the threads-devel
> list, let me know, but I thought starting here would be good.

Here is fine.

>   I was playing with the threads library in 1.35, and I noticed that I
> can't (with gcc 4.3 on Linux) put boost::thread objects into an STL
> container.  I think I was trying with a std::vector, in case that
> matters.

No, you can't do that. You need a container that can handle
movable-but-not-copyable objects. Not only that, but it needs to
handle the thread move emulation.

>   I was reading in some of the C++0x documents that it's expected that
> the standard library thread implementation is expected to be able to
> have this work.  The boost documentation doesn't explicitly say whether
> it should work or not.  Is being non-copyable but movable enough to make
> this work?  Or, would an object have to be copyable to be put into an
> STL container?  I thought that was not the case, but.

Non-copyable but movable is indeed enough for a class to be used in a
C++0x STL container. I don't know whether the containers with gcc 4.3
have that support in -std=c++0x mode or not. The current trunk has
some rvalue reference support in boost::thread, so it may well work,
but I haven't tried it.

(Continue reading)

dhruva | 23 May 08:05
Picon

Re: Boost 1.35 threads library question

On 5/22/08, Anthony Williams <anthony.ajw <at> gmail.com> wrote:
> Chris Ross <cross+boost <at> distal.com> writes:
>
>  >   Hi there.  This is an introductory question as I've just started to
>  > play with boost libraries.  If this would be better on the threads-devel
>  > list, let me know, but I thought starting here would be good.
>
>
> Here is fine.
>
>
>  >   I was playing with the threads library in 1.35, and I noticed that I
>  > can't (with gcc 4.3 on Linux) put boost::thread objects into an STL
>  > container.  I think I was trying with a std::vector, in case that
>  > matters.
>
>
> No, you can't do that. You need a container that can handle
>  movable-but-not-copyable objects. Not only that, but it needs to
>  handle the thread move emulation.

As a dirty alternative, it is possible to have the thread object
wrapped in a structure. You allocate the structure on the heap and put
that into the container. Since copying of pointers is cheap, you do
not incurr the overhead of copying in STL containers. However, you
need to free up the memory during clearing the container.
Or, even use the BOOST shared pointer, I have not used this though.

-dhruva

(Continue reading)


Gmane