Android bionic缺失pthread_cancel的解决方法
2014-06-11 14:54
260 查看
在native code中使用多线程好处多多,但是Android的bionic并没有完全实现标准POSIX线程库的所有API,例如pthread_cancel()。但是google这样做肯定有原因,被cancel的thread不一定已经把自己拥有的资源释放掉,因此很可能带来内存泄露,锁没有释放等问题。这些问题在移动设备上更加突出。
首先介绍一个指标的方法,使用signal替代cancel调用:
当worker thread超时时,在主线程(或者是监视进程)中调用
if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
{
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
}
在worker thread中加入对SIGUSR1信号的处理
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
rc = sigaction(SIGUSR1,&actions,NULL);
void thread_exit_handler(int sig)
{
printf("this signal is %d \n", sig);
pthread_exit(0);
}
最根本的解决方法是重写worker thread,使用poll或者select等处理IO操作防止stuck的发生,下面是Android源码system/libsysutils/src/SocketListener.cpp的处理方法
1,创建worker thread前先创建通讯管道
if (pipe(mCtrlPipe)) {
SLOGE("pipe failed (%s)", strerror(errno));
return -1;
}
if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
SLOGE("pthread_create (%s)", strerror(errno));
return -1;
}
2,在worker thread的大循环中使用select同时监控管道和IO fd
while(1){ // 一般工作进程都带一个大循环
FD_SET(mCtrlPipe[0], &read_fds);
if (mCtrlPipe[0] > max)
max = mCtrlPipe[0];
if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
SLOGE("select failed (%s)", strerror(errno));
sleep(1);
continue;
} else if (!rc)
continue;
// 有人喊停了
if (FD_ISSET(mCtrlPipe[0], &read_fds))
break;
}
3,需要退出时通过管道通知worker thread
if (write(mCtrlPipe[1], &c, 1) != 1) {
SLOGE("Error writing to control pipe (%s)", strerror(errno));
return -1;
}
首先介绍一个指标的方法,使用signal替代cancel调用:
当worker thread超时时,在主线程(或者是监视进程)中调用
if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
{
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
}
在worker thread中加入对SIGUSR1信号的处理
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
rc = sigaction(SIGUSR1,&actions,NULL);
void thread_exit_handler(int sig)
{
printf("this signal is %d \n", sig);
pthread_exit(0);
}
最根本的解决方法是重写worker thread,使用poll或者select等处理IO操作防止stuck的发生,下面是Android源码system/libsysutils/src/SocketListener.cpp的处理方法
1,创建worker thread前先创建通讯管道
if (pipe(mCtrlPipe)) {
SLOGE("pipe failed (%s)", strerror(errno));
return -1;
}
if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
SLOGE("pthread_create (%s)", strerror(errno));
return -1;
}
2,在worker thread的大循环中使用select同时监控管道和IO fd
while(1){ // 一般工作进程都带一个大循环
FD_SET(mCtrlPipe[0], &read_fds);
if (mCtrlPipe[0] > max)
max = mCtrlPipe[0];
if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
SLOGE("select failed (%s)", strerror(errno));
sleep(1);
continue;
} else if (!rc)
continue;
// 有人喊停了
if (FD_ISSET(mCtrlPipe[0], &read_fds))
break;
}
3,需要退出时通过管道通知worker thread
if (write(mCtrlPipe[1], &c, 1) != 1) {
SLOGE("Error writing to control pipe (%s)", strerror(errno));
return -1;
}
相关文章推荐
- android bionic缺失pthread_cancel的解决方法
- Android bionic缺失pthread_cancel的解决方法
- android bionic缺失pthread_cancel的解决方法
- android bionic缺失pthread_cancel的解决方法
- android bionic缺失pthread_cancel的解决方法
- android bionic缺失pthread_cancel的解决方法
- Android中使用Toast.cancel()方法优化toast内容显示的解决方法
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- AndroidStudio R文件莫名其妙缺失的解决方法
- 关于AndroidStudio R文件莫名其妙缺失的快速解决方法
- android上Bionic不支持pthread_cancel等函数
- Android Studio sdk tools文件夹下文件缺失问题以及解决方法
- Cocos2dx通信(Http&Socket)相关编译到Android细节总结 编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_in
- 调用pthread_cancel阻塞问题解决方法.
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- 关于在Android或Java中精度缺失的解决方法
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- 相关编译到Android细节总结 编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- cocos2d-2.0-x-2.0.3 批处理文件create-android-project.bat 创建工程缺失lib解决方法
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i