質問

中規模の C++ コード本体を Android NDK に移植しています。残念ながら、pthreads の実装は (NDK v5 の時点では) 不完全です。具体的には、アプリケーションは pthread_cancel() に依存してワーカー スレッドを強制終了します。NDK は pthread_cancel() を実装していません。ワーカー スレッドが正常に応答している場合には、明らかな答えが他にもあります。ただし、ワーカー スレッドが応答しない場合 (例:無限ループ)、プロセス全体を強制終了せずにキャンセルするにはどうすればよいですか?

役に立ちましたか?

解決

この男に有効なオプションは次のとおりです。 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

他のヒント

私はこれの世話という小さなライブラリを作っています。

これは、生体スレッド構造のいくつかの未使用のビットを利用する。

私はそれを呼ばれる libbthread に:)

お楽しみ;)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top