在机器人的dlopen比赛条件()? [英] Race condition in android dlopen()?
问题描述
我的Android应用程序有一个简单的装载机NativeActivity的使用非常简单的 android_main()
只加载不同的共享对象,并将其控制:
My Android app has a simple "loader" NativeActivity with a very simple android_main()
which only loads a different shared object and passes control to it:
typedef void (*Tandroid_main)( android_app*);
void android_main( android_app* state )
{
void* glib = dlopen("libmain.so", RTLD_NOW);
void* fmain = dlsym(glib, "android_main");
Tandroid_main libmain = (Tandroid_main)fmain;
libmain(state)
}
这工作得很好..大约一半的时间。因为的dlopen其他时候它崩溃()
失败,返回NULL,并将errno = 2(没有这样的文件)。
由于奇怪的不一致发生这种情况的我怀疑是一个时间问题,事实上,加入睡眠(1)
在的dlopen()
从发生停止它。一些更强大的比睡眠(1)
将只是想在一个循环:
This works well.. about half of the times. Other times it crashes since dlopen()
fails and return NULL with errno=2 (No such file).
Due to the strange inconsistency of this occurrence I suspected a timing issue and indeed, adding a sleep(1)
before dlopen()
stopped it from happening. Something more robust than sleep(1)
would be just trying it in a loop:
int count = 0;
void* glib = dlopen(soName, RTLD_NOW);
while(glib == NULL) {
sched_yield();
++count;
glib = dlopen(soName, RTLD_NOW);
}
我是从这个循环获得的计数通常是一些在10-70的范围内我的设备上。但是,这是一个hackish的丑陋的解决方案。
The count I'm getting from this loop is usually something in the range of 10-70 on my device. But this is a hackish ugly solution.
什么是真正回事?为什么我只能加载其它共享对象只是轻微的NativeActivity的启动后?有没有更好的办法找到的时候,当它是安全的加载?
What is really going on here? How come I can only load other shared objects only slightly after the NativeActivity starts? Is there a better way to find when when it's safe to load it?
应该指出的是,我也打电话的System.loadLibrary(主)
从我的NativeActivity的的的onCreate()
It should be noted that I am also calling System.loadLibrary("main")
from my NativeActivity's onCreate()
推荐答案
不知道,但建议从静态初始化调用LoadLibrary():
Not sure, but it is recommended to call loadLibrary() from a static initializer:
public class MainActivity extends Activity {
static {
System.loadLibrary("main")
}
...
}
帮助?
这篇关于在机器人的dlopen比赛条件()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!