Opened 14 years ago

Closed 14 years ago

#2144 closed Bugs (wontfix)

boost::thread constructor wants to make copies of the argument

Reported by: mmikucionis@… Owned by: Anthony Williams
Milestone: Boost 1.36.0 Component: thread
Version: Boost 1.35.0 Severity: Optimization
Keywords: Cc:

Description

Hi,

I noticed that boost::thread constructor makes a copy of its argument. Suppose I have a big structure that I use as thread context and I don't want redundant copies on stack or anywhere else.

For example:

struct foo {

void operator()() {

zzz

}

private:

foo(foo&); no copying, please

};

boost::thread(foo()); this fails due to private copy constructor

I bumped into this problem when I included boost::thread itself into the structure (forbids copying too ;).

Thus I wonder if the argument could be replaced by a reference to an argument which would not insist on copying things.

As a workaround I do the following now:

void bar(foo *ptr) { (*ptr)(); } boost::thread(boost::bind(bar, new foo));

I looked at repository on the web and I could not find class thread anymore (moved somewhere else since 1.35?), so maybe it's already fixed...

Thanks for great library!

Change History (2)

comment:1 by mmikucionis@…, 14 years ago

Argh! I just now realized that by this copy constructor one makes sure that foo() does not disappear after the thread is constructed.

Sorry for the noise.

comment:2 by Anthony Williams, 14 years ago

Resolution: wontfix
Status: newclosed

Yes, as you noted the copying ensures the object lives as long as the thread.

If you need to use a large, non-copyable object you can either use a pointer (as in your example), or use boost::ref:

foo large_object; boost::thread t(boost::ref(large_object));

If you do this it is up to you to ensure that the referenced object outlives the thread.

Note: See TracTickets for help on using tickets.