awm*_*129 4 c++ android pthreads
以下代码在标准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;
}
Run Code Online (Sandbox Code Playgroud)
并产生以下输出:
$ ./a.out
thread started
thread running
thread stopped
$
Run Code Online (Sandbox Code Playgroud)
此代码也使用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, NULL, start_the_thread, static_cast<void*>(this)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5189 次 |
| 最近记录: |