升压thread_interrupted异常终止()s的MinGW的GCC 4.4.0,确定与3.4.5 [英] Boost thread_interrupted exception terminate()s with MinGW gcc 4.4.0, OK with 3.4.5

查看:96
本文介绍了升压thread_interrupted异常终止()s的MinGW的GCC 4.4.0,确定与3.4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在提升线程今天与玩弄作为一个学习的过程,我已经有一个工作的例子,我好几个月前建(前我被打断,不得不放弃多线程一会儿)这就是显示异常行为。

当我最初写的我用GCC MingW平台3.4.5,和它的工作。现在,我使用的是4.4.0,它不会 - 顺便说一句,我试着再次使用3.4.5(我留了版本,它当我安装4.4.0一个单独的文件夹),它仍然工作。

在code是问题的结束; 综上所述它的作用是启动两个计数两个子线程对象的关闭(这些对象是简单的增加一个变量,然后睡了一会儿,重复广告无限 - 他们算),主线程通过 cin.get()等待用户,然后中断两个线程,等待他们加入,然后输出的结果两个计数器。

与它运行正常3.4.5的规定。

与4.4.0已遵守其运行,直到用户输入,然后用像以下信息死亡 - 这似乎中断异常被杀死的全过程


  

抛出的一个实例后终止叫
  此应用程序已请求运行时终止它以一种不寻常的方式。
  请联系应用程序的支持团队以获取更多信息。
  抛出boost :: thread_interrupted


  
  

此应用程序已请求运行时终止它以一种不寻常的方式。
  请联系应用程序的支持团队以获取更多信息。


从我读,我认为这是允许传播出一个子线程会杀死过程中的任何(?)未捕获的异常?但是,我在这里捕捉中断,不是吗?至少我的看起来的使用3.4.5的时候要。

所以,第一,有我知道如何打断作品?结果
而且,任何建议,正在发生什么,以及如何解决?

code:

 的#include<&iostream的GT;
#包括LT&;升压/线程/ thread.hpp>
#包括LT&;升压/ date_time.hpp>//修复了4.4.0升压主题链接错误(不需要3.4.5)
通过谷歌找到//,所以不知道的有效性 - 但修复链接错误。
为externC无效tss_cleanup_implemented(){}类CCounter
{
私人的:
    INT和放大器; numberRef;
    INT一步;
上市:
    CCounter(中间体&放大器;数,诠释setStep):numberRef(编号),步骤(setStep){}    void运算符()()
    {
        尝试
        {
            而(真)
            {
                提高::了posix_time ::毫秒pauseTime(50);
                numberRef + =步骤;
                提高:: this_thread ::睡眠(pauseTime);
            }
        }
        赶上(抛出boost :: thread_interrupted常量与评估)
        {
            返回;
        }
    }
};INT主(INT ARGC,CHAR *的argv [])
{
    尝试
    {
        性病::法院LT&;< 启动二级螺纹专柜\\ n;        INT产品数= 0,
            数字1 = 0;
        CCounter计数器0(number0,1);
        CCounter计数器1(数字1,-1);        提高::线程threadObj0(C0的);
        提高::线程threadObj1(C1的);        性病::法院LT&;< preSS进入停止计数器:\\ n;
        的std :: cin.get();        threadObj0.interrupt();
        threadObj1.interrupt();        threadObj0.join();
        threadObj1.join();        性病::法院LT&;< 计数器停止值:\\ n
                  << - 产品数LT;<的'\\ n'
                  << NUMBER1<<的'\\ n';
    }
    赶上(抛出boost :: thread_interrupted急症)
    {
        性病::法院LT&;< \\ nThread追访捕获到异常\\ n;
    }
    赶上(性病::例外急症)
    {
        性病::法院LT&;< \\ NSTD ::抛出的异常\\ n;
    }
    抓住(...)
    {
        性病::法院LT&;< \\ nUnexpected的异常。\\ n
    }    返回EXIT_SUCCESS;
}


解决方案

解决了。

原来用4.4.0添加编译标志 -static-libgcc中将删除的问题(没有明显的与3.4.5影响) - 或者至少这种情况下,程序返回预期的结果。

I've been "playing around with" boost threads today as a learning exercise, and I've got a working example I built quite a few months ago (before I was interrupted and had to drop multi-threading for a while) that's showing unusual behaviour.

When I initially wrote it I was using MingW gcc 3.4.5, and it worked. Now I'm using 4.4.0 and it doesn't - incidentally, I've tried again using 3.4.5 (I kept that version it a separate folder when I installed 4.4.0) and it's still working.

The code is at the end of the question; in summary what it does is start two Counter objects off in two child threads (these objects simply increment a variable then sleep for a bit and repeat ad infinitum - they count), the main thread waits for the user via a cin.get() and then interrupts both threads, waits for them to join, then outputs the result of both counters.

Complied with 3.4.5 it runs as expected.

Complied with 4.4.0 it runs until the user input, then dies with a message like the below - it seems the the interrupt exceptions are killing the entire process?

terminate called after throwing an instance of ' This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. boost::thread_interrupted'

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

From what I read, I think that any (?) uncaught exception that is allowed to propagate out of a child thread will kill the process? But, I'm catching the interrupts here, aren't I? At least I seem to be when using 3.4.5.

So, firstly, have I understood how interrupting works?
And, any suggestions as to what is happening and how to fix?

Code:

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/date_time.hpp>

//fixes a linker error for boost threads in 4.4.0 (not needed for 3.4.5)
//found via Google, so not sure on validity - but does fix the link error.
extern "C" void tss_cleanup_implemented() { }

class CCounter
{
private:
    int& numberRef;
    int  step;
public:
    CCounter(int& number,int setStep) : numberRef(number) ,step(setStep) { }

    void operator()()
    {
        try
        {
            while( true )
            {
                boost::posix_time::milliseconds pauseTime(50);
                numberRef += step;
                boost::this_thread::sleep(pauseTime);
            }
        }
        catch( boost::thread_interrupted const& e )
        {
            return;
        }
    }
};

int main( int argc , char *argv[] )
{
    try
    {
        std::cout << "Starting counters in secondary threads.\n";

        int number0 = 0,
            number1 = 0;
        CCounter counter0(number0,1);
        CCounter counter1(number1,-1);

        boost::thread threadObj0(counter0);
        boost::thread threadObj1(counter1);

        std::cout << "Press enter to stop the counters:\n";
        std::cin.get();

        threadObj0.interrupt();
        threadObj1.interrupt();

        threadObj0.join();
        threadObj1.join();

        std::cout << "Counter stopped. Values:\n"
                  << number0 << '\n'
                  << number1 << '\n';
    }
    catch( boost::thread_interrupted& e )
    {
        std::cout << "\nThread Interrupted Exception caught.\n";
    }
    catch( std::exception& e )
    {
        std::cout << "\nstd::exception thrown.\n";
    }
    catch(...)
    {
        std::cout << "\nUnexpected exception thrown.\n"
    }

    return EXIT_SUCCESS;
}

解决方案

Solved.

It turns out adding the complier flag -static-libgcc removes the problem with 4.4.0 (and has no apparent affect with 3.4.5) - or at least in this case the program returns the expected results.

这篇关于升压thread_interrupted异常终止()s的MinGW的GCC 4.4.0,确定与3.4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆