Boost thread library wrapper

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Boost thread library wrapper

Dhanvi Kapila-2
Hi all
  This mail is in continuation to the earlier mail I had written asking
for help in writting a thread wrapper for boost.threads. We already have
an existing interface to which we would like our wrapper for
boost.threads to adhere to.
   We have a runnable class which is inherited by the applications
wanting to create threads and they override the run method. This is how
our application currently creates threads.

  Thanks Peter for your earlier mail explaining on how to use
boost::shared_ptr and boost::bind

  Your code for a Runnable interface
> struct Runnable
> {
>    virtual ~Runnable() {}
>    virtual void run() = 0;
> };
We have a class Runnable with nearly the same code as above.
To make a dummy implementation, I made the run methd a normal function.
Nothing derives from it.. run is what I want to execute in a thread.
>
> void execute_in_thread( boost::shared_ptr<Runnable> pr )
> {
>     boost::thread th( boost::bind( &Runnable::run, pr ) );
> }
I am creating a thread in MyThread class using the following code.

--- MyThread.h ----
typedef boost::shared_ptr<Runnable> myPtr;

class MyThread
{
  public:
   MyThread( );
   ~MyThread( );
  private:
   myPtr p1;
}
--- MyThread.cpp ----
MyThread::MyThread( )
  : p1( new Runnable() )
{
   cout << "MyThread::MyThread( ) constructor " << endl;
   boost::thread th( boost::bind( &Runnable::run, p1) );
}
---- Runnable.cpp ---
// this is the only function in Runnable.h
void Runnable::run()
{
    cout << "Runnable::run() YUPPIE! "<< endl;
}
------ end -----------
The code compiles, but nothing happens.. I am missing something here ??
Is there something else I need to do to start my thread??

Thanks
Dhanvi
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Delfin Rojas
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> [hidden email]
>
> <snip>
>
> --- MyThread.h ----
> typedef boost::shared_ptr<Runnable> myPtr;
>
> class MyThread
> {
>   public:
>    MyThread( );
>    ~MyThread( );
>   private:
>    myPtr p1;
> }
> --- MyThread.cpp ----
> MyThread::MyThread( )
>   : p1( new Runnable() )
> {
>    cout << "MyThread::MyThread( ) constructor " << endl;
>    boost::thread th( boost::bind( &Runnable::run, p1) ); }
> ---- Runnable.cpp ---
> // this is the only function in Runnable.h void Runnable::run() {
>     cout << "Runnable::run() YUPPIE! "<< endl; }
> ------ end -----------
> The code compiles, but nothing happens.. I am missing
> something here ??
> Is there something else I need to do to start my thread??
>

How are you preventing your program from ending before the thread finishes
(or even starts up)? In other words, what do you do after you create an
instance of MyThread? Do you wait for some time?

-delfin

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Dhanvi Kapila-2
Hi,
Delfin Rojas wrote:
> How are you preventing your program from ending before the thread finishes
> (or even starts up)? In other words, what do you do after you create an
> instance of MyThread? Do you wait for some time?
No.. I am not.
The code works after I call join() on the thread.  Thanks for pointing
that out.

Another question : Does boost have exit handlers for threads ?

-Dhanvi
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Delfin Rojas
> Dhanvi Kapila wrote:
>
> > How are you preventing your program from ending before the thread
> > finishes (or even starts up)? In other words, what do you do after you
> > create an instance of MyThread? Do you wait for some time?
> No.. I am not.
> The code works after I call join() on the thread.  Thanks for
> pointing that out.
>

You're welcome.

> Another question : Does boost have exit handlers for threads ?

What do you mean with "exit handlers"? Can you provide an example?

-delfin

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Dhanvi Kapila-2
Hi,
Delfin Rojas wrote:
>>Another question : Does boost have exit handlers for threads ?
>
> What do you mean with "exit handlers"? Can you provide an example?
by exit handlers I mean some special function that would be gauranteed
to be called whenever the thread is exiting.
This is where my application can place specific code to be done on
thread exit ?

I Hope I am clear here..

Dhanvi
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Sebastian Redl
Dhanvi Kapila wrote:

>by exit handlers I mean some special function that would be gauranteed
>to be called whenever the thread is exiting.
>  
>
Technically, this depends entirely on whether the underlying operating
system provides such a mechanism. Win32 doesn't. A thread killed by
TerminateThread, or simply ended by ExitThread, is dead immediately, and
no code is ever again executed on its stack. The only thing that still
gets executed (not necessarily on the existing thread) are the DLL entry
functions.
Thus, Boost.Thread cannot possibly supply a reliable version of such a
feature across all platforms, and as such, it's probably best that it
doesn't supply the feature at all.

If you want to guarantee execution of a function on thread end under
normal circumstances (i.e. normal return or exception stack unwinding),
you should use a sentry object that executes that code in the destructor:

void thread_entry()
{
  thread_sentry sentry;

  // Code here

}

You can easily make a reusable object for this:

class thread_sentry : public boost::noncopyable
{
  boost::function<void ()> exit_func_;
public:
  thread_sentry(boost::function<void ()> exit_func)
    : exit_func_(exit_func)
  {}

  ~thread_sentry()
  {
    if(exit_func_) exit_func_();
  }
};

Now you can do:

void exit_function()
{
  // Cleanup code.
}

void thread_entry
{
  thread_sentry sentry(exit_function);

  // Code here

}

Usually such code can be avoided by using RAII objects for all resources
in the first place.

Sebastian Redl
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost thread library wrapper

Delfin Rojas
In reply to this post by Dhanvi Kapila-2
> Dhanvi Kapila wrote:
> >
> > What do you mean with "exit handlers"? Can you provide an example?
> by exit handlers I mean some special function that would be
> gauranteed to be called whenever the thread is exiting.
> This is where my application can place specific code to be
> done on thread exit ?
>
There is something like that in boost::thread but it is only supported for
win32. See boost/thread/detail/tss_hooks.hpp

On the other hand, if what you want is to call a function when the thread is
exiting "normally" (when the thread proc exits via normal flow or exception)
then I think Sebastian's idea of a sentry object is the best and it is more
portable. You can pass the "callback function" as a parameter to your thread
proc (using bind) and initialize the local sentry object with it.

-delfin

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users