Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] C++/CLI C# mixed code: linking to libthread yields runtime exception
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-02-04 10:54:40


Markus Werle <numerical.simulation_at_[hidden]> writes:

> OK, here the facts:
>
> Win32, XP SP3, using Visual Studio 2008 SP1 with boost_1_37_0
>
> I create a solution containing a C++/CLI project and a C# project, which
> references the C++/CLI project.
> Now if the C++/CLI project simply links to libboost_thread*, either
> via autolink or via explicit configuration, the compiler creates an
> executable which fails to start in both configurations, Debug and Release.
> The debugger catches a System.BadImageFormatException.
> Something went plain wrong.

> OTOH adding the source files contained in \libs\thread\src\win32
> to the project, defining BOOST_ALL_NO_LIB and adding
>
> ---
> #pragma managed(push, off)
> extern "C" void tss_cleanup_implemented(void) {}
> #include <boost/thread/thread.hpp>
> #pragma managed(pop)
> ---
>
> in a cpp file yields a working executable.
> Scratching my head.
>
> Do you have any hint why linking to libboost_thread leads to this weird
> behaviour? Any idea what I might try?
> I can provide a stripped down project solution as zip-file on request.

The problem is that the static boost thread library tries to hook the
native win32 PE TLS callbacks in order to ensure that the thread-local
data used by boost thread is cleaned up correctly. This is not
compatible with a C++/CLI executable.

Your workaround explicitly omits the automatic TLS cleanup (and declares
that you're doing it yourself by calling on_thread_exit at the
appropriate time).

Anthony

-- 
Author of C++ Concurrency in Action | http://www.manning.com/williams
just::thread C++0x thread library   | http://www.stdthread.co.uk
Just Software Solutions Ltd         | http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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