中規模の C++ コードを Android NDK に移植しています。残念ながら、pthreads の実装 (少なくとも NDK v5 の時点では) は不完全です。具体的には、アプリケーションはワーカー スレッドを強制終了するために pthread_cancel() に依存しています。NDK は pthread_cancel() を実装していません。ワーカー スレッドが正常に応答している場合は、他の明らかな答えがあります。しかし、ワーカー スレッドが応答していない場合 (たとえば、無限ループ)、プロセス全体を強制終了せずにキャンセルするにはどうすればよいでしょうか。
ベストアンサー1
この人に有効な可能性のあるオプション:http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html
念のためここに再投稿します:
次に、pthread_kill を使用して SIG_USR1 シグナルをトリガーし、シグナル ハンドラーを使用してこの pthread を終了して試してみましたが、動作はしますが、この種の方法に欠点があるかどうかはまだわかりません。
タイムアウト:
if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
{
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
}
USR1 ハンドラー:
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);
}
スレッドが IO を待機しないように書き直すのが最善の答えのようです:http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1