Re: a really simple C++ abstraction around pthread_t...
"Ulrich Eckhardt" <doomster@knuut.de> wrote in message
news:6nh95gFlmtc6U1@mid.uni-berlin.de...
Responding late, it was a rough week...
[...]
In any case, you can write an equivalent program using Boost.Thread. If
you
want, you can wrap stuff into a class, e.g. like this:
struct active_person
{
explicit active_person(queue& out_):
out(out_),
th( bind( &handle_requests, this))
{}
~active_person()
{ th.join(); }
void push_question( std::string const& str)
{ in.push(str); }
std::string pop_answer()
{ return out.pop(); }
private:
void handle_requests()
{
while(true)
{
std::string question = in.pop();
if(question=="QUIT")
return;
out.push(p.ask(question));
}
out.push("QUIT");
}
queue in;
queue& out;
person p;
// note: this one must come last, because its initialisation
// starts a thread using this object.
boost::thread th;
};
[...]
I would create the active template using Boost like:
// quick sketch - may have typo
_______________________________________________________________________
template<typename T>
class active : public T {
boost::thread m_active_handle;
public:
active() : T(), m_active_handle(bind(&on_active, (T*)this)) {}
~active() { m_active_handle.join(); }
template<typename P1>
active(P1 p1) : T(p1),
m_active_handle(bind(&on_active, (T*)this)) {}
template<typename P1, typename P2>
active() : T(P1, P2),
m_active_handle(bind(&on_active, (T*)this)) {}
// [on and on for more params...]
};
_______________________________________________________________________
Then I could use it like:
struct person {
void on_active() {
// [...]
}
};
int main() {
active<person> p[10];
return 0;
}
I personally like this construction; I think its "cleaner" than using the
Boost interface "directly".