0
並行線程
///////// // //////////////////////*pthread_mutex_trylock?在Windows
pthread_mutex_t stop = PTHREAD_MUTEX_INITIALIZER;
int a = 1;
void* decrement(void* arg)
{
pthread_mutex_trylock(&stop);
if(a > 0) { a--; }
cout << "Esecuzione thread tid" << endl;
pthread_mutex_unlock(&stop);
pthread_exit(NULL);
}
int main()
{
pthread_t tid;
pthread_attr_t tattr;
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &tattr, decrement, NULL);
pthread_mutex_lock(&stop);
if(a > 0) { a--; }
cout << "Esecuzione thread main" << endl;
cout << a << endl;
pthread_exit(NULL);
return 0;
}
爲什麼線程分離到主線程繼續執行,而不是使用EBUSY返回給調用者?
您不在任何地方檢查任何錯誤代碼,那麼如何判斷它們是否被退回? –