歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android bionic缺失pthread_cancel的解決方法

Android bionic缺失pthread_cancel的解決方法

日期:2017/3/1 10:19:04   编辑:Linux編程

在native code中使用多線程好處多多,但是Android的bionic並沒有完全實現標准POSIX線程庫的所有API,例如pthread_cancel()。但是google這樣做肯定有原因,被cancel的thread不一定已經把自己擁有的資源釋放掉,因此很可能帶來內存洩露,鎖沒有釋放等問題。這些問題在移動設備上更加突出。

首先介紹一個指標的方法,使用signal替代cancel調用:

當worker thread超時時,在主線程(或者是監視進程)中調用

  1. if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
  2. {
  3. printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
  4. }

在worker thread中加入對SIGUSR1信號的處理

  1. struct sigaction actions;
  2. memset(&actions, 0, sizeof(actions));
  3. sigemptyset(&actions.sa_mask);
  4. actions.sa_flags = 0;
  5. actions.sa_handler = thread_exit_handler;
  6. rc = sigaction(SIGUSR1,&actions,NULL);
  7. void thread_exit_handler(int sig)
  8. {
  9. printf("this signal is %d \n", sig);
  10. pthread_exit(0);
  11. }

最根本的解決方法是重寫worker thread,使用poll或者select等處理IO操作防止stuck的發生,下面是Android源碼system/libsysutils/src/SocketListener.cpp的處理方法

1,創建worker thread前先創建通訊管道

  1. if (pipe(mCtrlPipe)) {
  2. SLOGE("pipe failed (%s)", strerror(errno));
  3. return -1;
  4. }
  5. if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
  6. SLOGE("pthread_create (%s)", strerror(errno));
  7. return -1;
  8. }

2,在worker thread的大循環中使用select同時監控管道和IO fd

  1. while(1){ // 一般工作進程都帶一個大循環
  2. FD_SET(mCtrlPipe[0], &read_fds);
  3. if (mCtrlPipe[0] > max)
  4. max = mCtrlPipe[0];
  5. if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
  6. SLOGE("select failed (%s)", strerror(errno));
  7. sleep(1);
  8. continue;
  9. } else if (!rc)
  10. continue;
  11. // 有人喊停了
  12. if (FD_ISSET(mCtrlPipe[0], &read_fds))
  13. break;
  14. }

3,需要退出時通過管道通知worker thread

  1. if (write(mCtrlPipe[1], &c, 1) != 1) {
  2. SLOGE("Error writing to control pipe (%s)", strerror(errno));
  3. return -1;
  4. }
Copyright © Linux教程網 All Rights Reserved