安卓+的pthread + C ++ = SIGSEGV [英] android + pthread + c++ = SIGSEGV

查看:166
本文介绍了安卓+的pthread + C ++ = SIGSEGV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code编译并运行在标准的Linux:

The following code compiles and runs on standard linux:

#include <iostream>
#include <pthread.h>

using namespace std;

class Foo
{
    public:
        Foo();
        void go_thread();
        void stop_thread();
    private:
        static void* worker( void* param );
        pthread_t m_pt;
};

Foo::Foo()
{
    m_pt = 0;
}

void Foo::go_thread()
{
    int success = pthread_create( &m_pt, NULL, worker, static_cast<void*>(this) );

    if( success == 0 )
    {
        cout << "thread started" << endl;
    }
}

void Foo::stop_thread()
{
    int success = pthread_join( m_pt, NULL );

    if( success == 0 )
    {
        cout << "thread stopped" << endl;
    }
}

void* Foo::worker( void* p )
{
    cout << "thread running" << endl;
    return 0;
}

int main()
{
    Foo f;
    f.go_thread();
    f.stop_thread();
    return 0;
}

和输出如下:

$ ./a.out
thread started
thread running
thread stopped
$

这code还建立了Android NDK(R5B)。然而,当我adb中推生成的可执行文件到设备并运行它,我的main()运行之前得到一个SIGSEGV。我已经将问题降到在pthread_create()这似乎仅仅存在这一呼吁在我的code的,没关系的执行,使我的前卫赛格故障。任何想法?

This code also builds with the Android NDK (r5b). However, when I adb push the resulting executable to a device and run it, I get a SIGSEGV before main() even runs. I've isolated the issue down to pthread_create() It seems the mere existence of this call in my code, never mind execution, causes my prog to seg fault. Any ideas?

推荐答案

这个问题似乎是结合使用的iostream的pthread。我经历了,并更换了所有COUTS用的printf()类,去掉了使用条款,并删除了iostream头。在code编译并运行的设备上没有问题。我不知道这是什么谷歌应该知道的?

The issue seems to be combining iostream with pthread. I went through and replaced all couts with printf()s, removed the using clause, and removed the iostream header. The code compiled and ran with no issue on the device. I wonder if this is something Google should be made aware of?

最后的(工作)code是这样的:

The final (working) code looks like:

#include <pthread.h>
#include <stdio.h>

class Foo
{
    public:
        Foo();
        void go_thread();
        void stop_thread();
    private:
        static void* worker( void* param );
        pthread_t m_pt;
};

Foo::Foo()
{
    m_pt = 0;
}

void Foo::go_thread()
{
    int success = pthread_create( &m_pt, NULL, worker, static_cast<void*>(this) );

    if( success == 0 )
    {
        printf( "thread started\n" );
    }
}

void Foo::stop_thread()
{
    int success = pthread_join( m_pt, NULL );

    if( success == 0 )
    {
        printf( "thread stopped\n" );
    }
}

void* Foo::worker( void* p )
{
    printf( "thread running\n" );
    return 0;
}

int main()
{
    Foo f;
    f.go_thread();
    f.stop_thread();
    return 0;
}

这篇关于安卓+的pthread + C ++ = SIGSEGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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