- 论坛徽章:
- 0
|
本帖最后由 729926317 于 2011-05-07 22:15 编辑
最近在学POSIX,有些地方不理解
还望大家帮帮忙
谢了~
unlock失败我上网找了下应该是在当前线程不拥有锁的情况下会失败。
然后我这段代码那个“Fail”怎么都打印不出来,不知道什么。- #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秒,让另一个线程解锁互斥量,来造成解锁失败
- 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;
- }
复制代码 status输出显示是0
解锁成功?!。。。不可能啊 |
|