Boost logo

Boost Users :

Subject: Re: [Boost-users] [ASIO] Where to use CoInitizeEx
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-06-03 10:11:45


On Wed, Jun 3, 2009 at 3:57 PM, Daniele Barzotti <
daniele.barzotti_at_[hidden]> wrote:

> Ovanes Markarian ha scritto:
> >
> > void io_worker_thread(void)
> > {
> > #if (WIN32)
> > CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
> > #endif
> >
> > io_service.run();
> >
> > #if (WIN32)
> > CoUninitialize();
> > #endif
> > };
> >
> >
> > It is better to use at least RAII here. This approach is not exception
> > safe and if io_service.run() throws an exception CoUnintialize() will
> > not be called.
>
> You're right!
> But how can I adapt this code to use RAII?
> Maybe is more simple to add a try-catch statement?
>
> Daniele.
>

Zachary's example does it. You can also use the shared_ptr do it locally in
the function by passing the deleter function.

Or you just write your io_worker_thread() so:

void io_worker_thread()
{
# if(WIN32)
        struct com_init
        {
             com_init() { CoInitializeEx(...); }
             ~com_init() { CoUninitialize(); }
        } initializer_object;
# endif

       io_service.run();
}

That's it. ;)

Regards,
Ovanes



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net