私は次のような状況にあります
-Thread A-
Lock Mutex
If ActionA() == ERROR
Stop = True
Unlock Mutex
-Mainthread-
Lock Mutex
If ActionB() == ERROR
Stop = True
If Stop == True
Cancel ThreadA
Join ThreadA
Exit program
Unlock Mutex
Mutex が同期に使用されます。ワーカー スレッド 'Thread A' とメイン スレッドの両方がエラー状態になり、ローカル変数 'Stop' を設定する可能性があります。これにより、スレッド A がキャンセルされ、メイン プログラムが終了します。Mutex は、Stop (および他の共有オブジェクト) にアクセスする際の競合状態を防ぐために使用されます。
残念ながら、ターゲット スレッドが Mutex を待機している場合、 pthread_cancel() の呼び出しは機能しないようです。
#include <pthread.h>
pthread_mutex_t mutex;
void *thread(void *args){
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_mutex_lock(&mutex);
return NULL;
}
int main(int argc, const char * argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
pthread_t t;
pthread_create(&t, NULL, thread, NULL);
pthread_cancel(t);
pthread_join(t, NULL);
//Execution does not get here
}
私が他に何を試すことができるか考えていますか?
前もって感謝します