C++ boost:线程崩溃microsoft C++;编译程序

C++ boost:线程崩溃microsoft C++;编译程序,c++,boost,multithreading,C++,Boost,Multithreading,我的问题简述如下: 此代码使编译器崩溃 pThread[0] = new boost::thread( boost::bind( &cGridAnimator::DoJob, // member function this ), // instance of class 0 ); // job number 试图编译此代码时,编译器崩溃。(不是我的程序在运行此代码时崩溃!) 需要修复什

我的问题简述如下:

此代码使编译器崩溃

pThread[0] = new boost::thread(
boost::bind(
    &cGridAnimator::DoJob,  // member function
       this ),                 // instance of class
       0 );                 // job number
试图编译此代码时,编译器崩溃。(不是我的程序在运行此代码时崩溃!)

需要修复什么


问题的长版本

我将一个大的3D网格上的工作分成8个单独的作业,在单独的线程中运行,以便利用8核机器的优势

这非常有效:

    JOB_LOOP {
        pThread[kjob] = new boost::thread( ::DoJob, kjob );
    }
全局自由函数DoJob根据作业编号从cGridAnimator的全局实例读取数据

但是,我不喜欢所有这些全局变量四处浮动,我也不喜欢使用这么多访问器方法来获取必要的数据。使用cGridAnimator的方法会更整洁

因此,代码位于问题的顶部

然而,当我在MSVC++2008上编译它时,编译器发出以下抱怨,然后崩溃

1>Compiling...
1>mfm1.cpp
1>C:\Program Files\boost\boost_1_38_0\boost/bind.hpp(1643) : warning C4180: qualifier applied to function type has no meaning; ignored
1>        C:\Program Files\boost\boost_1_38_0\boost/bind.hpp(1677) : see reference to class template instantiation 'boost::_bi::add_cref<Pm,I>' being compiled
1>        with
1>        [
1>            Pm=void (__thiscall cGridAnimator::* )(int),
1>            I=1
1>        ]
1>        .\mfm1.cpp(158) : see reference to class template instantiation 'boost::_bi::dm_result<Pm,A1>' being compiled
1>        with
1>        [
1>            Pm=void (__thiscall cGridAnimator::* )(int),
1>            A1=cGridAnimator *
1>        ]
1>C:\Program Files\boost\boost_1_38_0\boost/mem_fn.hpp(318) : warning C4180: qualifier applied to function type has no meaning; ignored
1>        C:\Program Files\boost\boost_1_38_0\boost/bind/bind_template.hpp(344) : see reference to class template instantiation 'boost::_mfi::dm<R,T>' being compiled
1>        with
1>        [
1>            R=void (int),
1>            T=cGridAnimator
1>        ]
1>Project : error PRJ0002 : Error result 1 returned from 'C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe'.
1>编译。。。
1> mfm1.cpp
1> C:\ProgramFiles\boost\boost\u 1\u 38\u 0\boost/bind.hpp(1643):警告C4180:应用于函数类型的限定符没有意义;忽略
1> C:\ProgramFiles\boost\boost\u 1\u 38\u 0\boost/bind.hpp(1677):请参阅正在编译的类模板实例化“boost::\u bi::add\u cref”的参考
1> 与
1>        [
1> Pm=void(uu thiscallcgridanimator::*)(int),
1> I=1
1>        ]
1> .mfm1.cpp(158):请参阅对正在编译的类模板实例化“boost::_bi::dm_result”的引用
1> 与
1>        [
1> Pm=void(uu thiscallcgridanimator::*)(int),
1> A1=动画制作者*
1>        ]
1> C:\Program Files\boost\boost\u 1\u 38\u 0\boost/mem\u fn.hpp(318):警告C4180:应用于函数类型的限定符没有意义;忽略
1> C:\ProgramFiles\boost\boost\u 1\u 38\u 0\boost/bind/bind\u template.hpp(344):请参阅正在编译的类模板实例化“boost::\u mfi::dm”
1> 与
1>        [
1> R=空隙(int),
1> T=cGridAnimator
1>        ]
1> 项目:错误PRJ0002:从“C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe”返回的错误结果1。

没有明确的答案;基本上,ICE总是联系编译器供应商的好理由

为了找到真正的原因,尝试找到仍然显示崩溃的最小程序将有所帮助。这意味着要删除尽可能多的代码,删除尽可能多的依赖项,等等。对于一个ICE,我曾经成功地启动了一个新项目,并在我怀疑问题的地方编写了50多行代码

以下变通方法在过去对我有所帮助:

  • 降低优化级别
    • 尤其是链接时间代码生成似乎有点不稳定
  • 包含文件的更改顺序
    • 只有在源代码停止工作时知道它的修订版本时,才是明智的,这样您就可以专注于那里的更改
  • 切换到不同的boost版本
    • boost库的作者可能已经激活了一个变通方法,或者代码已经更改到足以不再引起ICE
  • 将代码更改为:

    pThread[0] = new boost::thread(boost::bind(&cGridAnimator::DoJob, this, 0 ));  
    

    此代码为线程提供了一个
    void(void)
    函数,而不是
    void(int)
    函数和一个额外的参数。

    请注意,如果您使用的是boost::bind,并且参数类型错误,Visual Studio不会提醒您此错误,但是PRJ0002和cl.exe出现了一个错误,那么“崩溃”是什么意思?报告内部编译器错误?我在这里没有看到任何“编译器崩溃”。我甚至没有看到编译器错误!你得到的是C++编译器的2条警告,然后是从代码> VCBuuest中的错误。我强烈怀疑您1)正在使用/W4编译,2)在
    .vcproj
    文件中有一些错误。请显示项目文件。
    cGridAnimator::DoJob()
    的签名也会有帮助。这一点你说得对。ICE通常打印到构建日志中,应用程序崩溃通常不会导致errorlevel=1。。。我傻了。这修复了编译器崩溃。我会做一些测试,如果有效的话,接受这个答案。接得好!答案很明显:你是怎么发现的?你可以在警告信息中看到DoJob的签名。几乎每次参数数量不合适时,我都会遇到相同的编译器崩溃。