- 論壇徽章:
- 0
|
本帖最后由 729926317 于 2011-05-07 22:15 編輯
最近在學(xué)POSIX,有些地方不理解
還望大家?guī)蛶兔?br />
謝了~
unlock失敗我上網(wǎng)找了下應(yīng)該是在當(dāng)前線程不擁有鎖的情況下會(huì)失敗。
然后我這段代碼那個(gè)“Fail”怎么都打印不出來(lái),不知道什么。- #include <pthread.h>
- #include <time.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct my_struct_tag {
- pthread_mutex_t mutex; /* Protects access to value */
- int value; /* Access protected by mutex */
- } my_struct_t;
- my_struct_t data = {
- PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
- void *wait_thread (void *arg)
- {
- int status;
- status = pthread_mutex_lock (&data.mutex);
- sleep (5);//等待5秒,讓另一個(gè)線程解鎖互斥量,來(lái)造成解鎖失敗
- status = pthread_mutex_unlock (&data.mutex);
- return NULL;
- }
- int main (int argc, char *argv[])
- {
- int status;
- pthread_t wait_thread_id;
- struct timespec timeout;
- status = pthread_create (&wait_thread_id, NULL, wait_thread, NULL);
- sleep(1);//等待1秒,讓其他線程鎖住互斥量
- status = pthread_mutex_unlock (&data.mutex);
- printf("%d\n",status);
- if(status!=0)
- {
- printf("Fail\n");
- }
- return 0;
- }
復(fù)制代碼 status輸出顯示是0
解鎖成功?!。。。不可能啊 |
|