pthread_cancel() alternatives in Android NDK?

后端 未结 2 1913
情话喂你
情话喂你 2020-12-24 08:47

I am porting a mid-sized body of C++ code to Android NDK. Unfortunately the pthreads implementation (as of NDK v5, anyway) is incomplete. Specifically, our application relie

相关标签:
2条回答
  • 2020-12-24 09:11

    Possible option that works for this guy: http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html

    Reposting here in case:

    Then I use pthread_kill to trigger a SIG_USR1 signal and use signal handler to exit this pthread and tried it, it works, but still wondering if any drawbacks for this kind of method.

    Timer out:

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

    USR1 handler:

    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);
    }
    

    Looks like the best answer is to rewrite so that threads aren't waiting on IO: http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1

    0 讨论(0)
  • 2020-12-24 09:18

    I made a small library that take care of this.

    It exploits some unused bits of the bionic thread structure.

    I called it libbthread :)

    Enjoy ;)

    0 讨论(0)
提交回复
热议问题