Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2002-08-06 12:04:25


> I don't think so. There's no requirement for return values to be default
> constructable, but that's the only manner in which I can see to implement
> this. [ Return values from user functions in thread joins ]

I think you can do it with a placement new, using only copy
constructors:

Pseudocode:

thread::thread<R>(user_function) {
          
  new <space for aligned R> return_location;
  run_implementation_thread(run_thread_function,return_location,
                            user_function);
}

R thread::join() {
  do_implementation_joining(...);
  return reinterpret_cast<R &> return_location;
}

// Run in the thread...
thread::run_thread_function(R *return_location) {
  new (return_location) R(user_function(...));
}

Well, you need a reinterpret cast somewhere, or use the std
uninitialized memory functions somehow, but I think this is a
legitimate use for it, and even portable.

Since this is effectively a translation of what most compilers do under
the hood for complex-type function returns anyway, it ought to work.

George Heintzelman
georgeh_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk