Opened 14 years ago

Closed 14 years ago

#1929 closed Bugs (fixed)

Borland/Codegear C++ 2007 creating boost::thread fails at initialisation of membervar boost::thread::thread_info

Reported by: Giel van Schijndel <me@…> Owned by: Anthony Williams
Milestone: Boost 1.36.0 Component: thread
Version: Boost 1.35.0 Severity: Problem
Keywords: Cc: me@…

Description

I'm attempting to create a thread in Borland/Codegear C++ 2007 (win32) by means of code similar to this:

class Threaded
{
    volatile bool _quit;
    boost::thread _thread;

    public:
        Threaded() :
            _quit(false),
            _thread(boost::ref(*this))
        {}

        void operator()()
        {
            while (!_quit)
                /* do stuff here */;
        }
};

The results of several debugging sessions:

However as soon as the constructor of boost::thread gets invoked it will call boost::thread::make_thread. This allocates some heap memory using the Windows API and uses an in-place new to construct a detail::thread_data<T> in that memory. The allocation and construction seem to work out very well.

As soon as boost::thread::make_thread_info returns however it seems to fail to assign the pointer to member variable thread_info in the constructor.

I tried the below patch for debugging purposes, to see what happens. Somehow the new variable (tinfo) remains NULL (all heap memory is initialised to zero while debugging, hence the NULL) however. Thus it seems that this compiler (borland) somehow fails to generate code that properly assigns the pointer to boost::thread's member variables.

However, somehow assigning the value of tinfo to an on-stack variable does yield the correct pointer, while at the time the debugger says that tinfo is still NULL, even though assigning it to another pointer yields a non-NULL (and correct) value. So maybe it's pointer value is kept in a register but only gets assigned to stack based variables but not to member variables?

What's even more strange is that compiling a minimal test case outside of the IDE (using bjam) produces code that works. Unfortunately I cannot use bjam for my project as it depends on Borland's VCL GUI library (if it's possible to use bjam with Borland's VCL anyway please tell me where to look/what to do, as up till now I've only failed miserably and have little experience with bjam anyway).

Thus this leads me to believe that something differs in the compiler options, I haven't got the slightest clue what it could be though. I've got all optimisations turned off. Although I have no clue how to know what command line options the IDE is calling the compiler with, so I cannot compare it.

  • boost/thread/win32/thread.hpp

     
    196196        };
    197197       
    198198        mutable boost::mutex thread_info_mutex;
     199        detail::thread_data_base* tinfo;
    199200        detail::thread_data_ptr thread_info;
    200201
    201202        static unsigned __stdcall thread_start_function(void* param);
     
    214215        }
    215216#else
    216217        template<typename F>
    217         static inline detail::thread_data_ptr make_thread_info(F f)
     218        static inline detail::thread_data_base* make_thread_info(F f)
    218219        {
    219220            return detail::heap_new<thread_data<F> >(f);
    220221        }
     
    238239#else
    239240        template <class F>
    240241        explicit thread(F f):
    241             thread_info(make_thread_info(f))
     242            tinfo(make_thread_info(f)),
     243            thread_info(tinfo)
    242244        {
    243245            start_thread();
    244246        }

Attachments (1)

thread-borland-workaround.patch (1.6 KB ) - added by Giel van Schijndel <me@…> 14 years ago.
Attempt to workaround bug

Download all attachments as: .zip

Change History (7)

comment:1 by Giel van Schijndel <me@…>, 14 years ago

Note, I've tested this with both trunk and 1.35 and it happens with both.

by Giel van Schijndel <me@…>, 14 years ago

Attempt to workaround bug

comment:2 by Giel van Schijndel <me@…>, 14 years ago

After some further experimenting I found that thread-borland-workaround.patch fixes the problem, though only for the construction part of boost::thread. As soon as you call boost::thread::join it'll segfault.

comment:3 by Giel van Schijndel <me@…>, 14 years ago

Btw thread-borland-workaround.patch only works because it moves the pointer assignment out of the IDE compiled code into bjam compiled code.

PS I also found this thread on comp.lang.c++ on what seems to be the exact same problem: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e532172a82d20674

comment:4 by Marshall Clow, 14 years ago

Component: Nonethread
Owner: set to Anthony Williams

comment:5 by Giel van Schijndel <me@…>, 14 years ago

I found that these are the compiler options that are used (on all source files) when I compile using the IDE:

bcc32.exe -D_DEBUG;NO_STRICT -D_RTLDLL;USEPACKAGES -I"c:\program files\codegear\rad studio\5.0\include";"c:\program files\codegear\rad studio\5.0\include\dinkumware";"c:\program files\codegear\rad studio\5.0\include\vcl";..\..\..\Bureaublad\proia3\src\opengltest;"c:\program files\codegear\rad studio\5.0\include\Indy10";C:\boost_1_35_0 -y -v -vi- -k -r- -c  -tWM -tW -H=Debug\borlandgltest.pch -oDebug\gldraw.obj -w-par -Od gldraw.cpp 

This the linker command used:

ilink32.exe -L"c:\program files\codegear\rad studio\5.0\lib\debug";"c:\program files\codegear\rad studio\5.0\lib";"c:\program files\codegear\rad studio\5.0\lib\obj";"c:\program files\codegear\rad studio\5.0\lib\psdk";..\..\..\Bureaublad\proia3\src\opengltest;"c:\program files\codegear\rad studio\5.0\lib";"c:\program files\codegear\rad studio\5.0\lib\Indy10";"C:\Documents and Settings\All Users\Documenten\RAD Studio\5.0\DCP";Debug -j"c:\program files\codegear\rad studio\5.0\lib\debug";"c:\program files\codegear\rad studio\5.0\lib";"c:\program files\codegear\rad studio\5.0\lib\obj";"c:\program files\codegear\rad studio\5.0\lib\psdk";..\..\..\Bureaublad\proia3\src\opengltest;"c:\program files\codegear\rad studio\5.0\lib";"c:\program files\codegear\rad studio\5.0\lib\Indy10";"C:\Documents and Settings\All Users\Documenten\RAD Studio\5.0\DCP";Debug -lDebug -v -Gn -GA"C:\Documents and Settings\Giel van Schijndel\Mijn documenten\C++\proia3\src\opengltest\vfs24D.tmp"="C:\Documents and Settings\Giel van Schijndel\Mijn documenten\C++\proia3\src\opengltest\borlandgltest.res" -GA"C:\Documents and Settings\Giel van Schijndel\Mijn documenten\C++\proia3\src\opengltest\vfs24E.tmp"="C:\Documents and Settings\Giel van Schijndel\Mijn documenten\C++\proia3\src\opengltest\form1.dfm" -aa c0w32.obj vcl.bpi rtl.bpi vclx.bpi memmgr.lib sysinit.obj Debug\borlandgltest.obj Debug\devicecontext.obj Debug\form1.obj Debug\glcontext.obj Debug\gldraw.obj , Debug\borlandgltest.exe , Debug\borlandgltest.map , import32.lib cp32mti.lib , , "C:\Documents and Settings\Giel van Schijndel\Mijn documenten\C++\proia3\src\opengltest\vfs24D.tmp"

comment:6 by Anthony Williams, 14 years ago

Resolution: fixed
Status: newclosed

This issue appears to be due to the lack of abi_prefix.hpp and abi_suffix.hpp. I've added these to trunk in revision 45865, which should fix the problem.

Note: See TracTickets for help on using tickets.