Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2000-08-08 16:19:59


Hi,

--- William Kempf <sirwillard_at_[hidden]> wrote:
> --- In boost_at_[hidden], Dietmar Kuehl <dietmar_kuehl_at_y...> wrote:
[A thread class to be derived from removed]

> This is the "thin wrapper" that I dislike. Now we must derive from
> thread (or in other implementation, derive from runnable or some
> other suitably named secondary class) in order to be an "active
> object" and the active object is the only form of thread routine
> available. A thread creation routine that truly took C++ into
> consideration would allow you to create a thread from any function or
> any member function or even from some arbitrary functor.

Here is the version for functors and functions:

  template <typename Func>
  struct function_thread: thread {
    function_thread(Func func): m_func(func) {}
    int do_run() { return m_func(); }
  private:
    Func m_func;
  };

  template <typename Func>
  void start_thread(Func func) {
    function_thread<Func> *thread = new function_thread<Func>(func);
    thread->run();
  }

The version for member functions is only slightly more complex because
it needs an object the member function is called upon. However, with
the thin wrapper we have the portable base from which to create all
kinds of nifty ways for thread creation. The only open issue is how to
release the thread object... Putting it on the stack does not work
because it would be destroyed when the thread creating function leaves
the copy. Thus, it has to be allocated on the heap. The easiest is
probably to use two versions of 'run()': one which deletes 'this' and
one which does not.

> I'm not sure I agree here. If the mutex isn't copyable this means
> that any class that contains a mutex (to handle internal
> synchronization) then the class is either not copyable or code must
> be supplied within every such class to deal with the issue. Copy
> semantics here have a lot of pros and cons and I think they'll need a
> bit of discussion.

Good point!

=====
<mailto:dietmar_kuehl_at_[hidden]>
<http://www.dietmar-kuehl.de/>

__________________________________________________
Do You Yahoo!?
Kick off your party with Yahoo! Invites.
http://invites.yahoo.com/


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