
#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.outthread startedthread runningthread stopped$
此代码也使用AndroID NDK(r5b)构建.但是,当我adb将生成的可执行文件推送到设备并运行它时,我会在main()运行之前得到一个SIGSEGV.我把问题分离到pthread_create()似乎在我的代码中存在这个调用,从不介意执行,导致我的编程错误.有任何想法吗?
解决方法 它可能不是这个但是尝试使pthread调用的函数创建一个普通的c函数(即声明它为extern“C”)而不是静态成员函数:这是因为从技术上讲,静态成员的调用约定可能与C库pthread使用的C调用约定不同(尽管很多时候它们是相同的(这就是它在你的linux盒子上运行的原因)我认为这不值得移植风险).
extern "C" voID* start_the_thread(voID*);voID* start_the_thread(voID* data){ Foo* theObject = static_cast<Foo*>(data); // In Java if your Foo had been derived from Runable // This is s where theObject->run() would have been called. return Foo::worker(data);}int success = pthread_create( &m_pt,start_the_thread,static_cast<voID*>(this) 总结 以上是内存溢出为你收集整理的android pthread c = SIGSEGV全部内容,希望文章能够帮你解决android pthread c = SIGSEGV所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)