免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2091 | 回复: 8

才发现pthread_cond_wait原来这么烂 [复制链接]

论坛徽章:
0
发表于 2009-02-05 14:36 |显示全部楼层
不能unlock 类型为PTHREAD_MUTEX_RECURSIVE的mutex.

论坛徽章:
0
发表于 2009-02-05 14:39 |显示全部楼层
只有被仅 lock 过一次的 recursive mutex, 可以由pthread_cond_wait来unlock。.

论坛徽章:
0
发表于 2009-02-05 14:42 |显示全部楼层
原帖由 太平绅士 于 2009-2-5 14:36 发表
不能unlock 类型为PTHREAD_MUTEX_RECURSIVE的mutex.

you sure?

论坛徽章:
0
发表于 2009-02-05 14:43 |显示全部楼层
原帖由 太平绅士 于 2009-2-5 14:39 发表
只有被仅 lock 过一次的 recursive mutex, 可以由pthread_cond_wait来unlock。.

那是当然啦,对recursive mutex加了几次锁就应当unlock几次,很符合常理嘛

论坛徽章:
0
发表于 2009-02-05 14:44 |显示全部楼层
原帖由 alexhappy 于 2009-2-5 14:42 发表

you sure?


sure. 我有测试代码
或者pthread_condattr有什么属性可以设?


  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <pthread.h>

  4. pthread_mutex_t mutex;
  5. pthread_cond_t cond;


  6. void *thread( void *arg )
  7. {
  8.         printf( "Entering thread\n" );

  9.         pthread_mutex_lock( &mutex );
  10.         pthread_mutex_lock( &mutex );

  11.         printf( "Before condition wait\n" );
  12.        
  13.         pthread_cond_wait( &cond, &mutex );

  14.         printf( "After condition wait\n" );

  15.         pthread_mutex_unlock( &mutex );
  16.         pthread_mutex_unlock( &mutex );

  17.         return NULL;       
  18. }


  19. int main( )
  20. {
  21.         pthread_t thread_id;

  22.         /* Create recursive mutex */
  23.         pthread_mutexattr_t attr;
  24.         pthread_mutexattr_init( &attr );
  25.         pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
  26.         pthread_mutex_init( &mutex, &attr );
  27.         pthread_mutexattr_destroy( &attr );

  28.         pthread_cond_init( &cond, NULL );

  29.         /* Create a thread to wait on the condition */
  30.         pthread_create( &thread_id, NULL, &thread, NULL );

  31.         sleep( 1 );
  32.         printf( "Before mutex Lock\n" );
  33.         pthread_mutex_lock( &mutex );

  34.         printf("After mutex Lock\n");
  35.         pthread_cond_signal( &cond );

  36.         pthread_mutex_unlock( &mutex );

  37.         pthread_join( thread_id, NULL );

  38.         pthread_cond_destroy( &cond );
  39.         pthread_mutex_destroy( &mutex );

  40.         return 0;
  41. }

复制代码

论坛徽章:
0
发表于 2009-02-05 14:48 |显示全部楼层
原帖由 alexhappy 于 2009-2-5 14:43 发表

那是当然啦,对recursive mutex加了几次锁就应当unlock几次,很符合常理嘛


为符合condition_wait的目的,condition_wait应该将mutex完全unlock,以便其他线程可以lock.
我认为pthread_cond_wait这样做完全是设计错误。

论坛徽章:
0
发表于 2009-02-05 15:48 |显示全部楼层
原帖由 太平绅士 于 2009-2-5 14:48 发表


为符合condition_wait的目的,condition_wait应该将mutex完全unlock,以便其他线程可以lock.
我认为pthread_cond_wait这样做完全是设计错误。

那么你也没有考虑其它加锁的地方呢?
这个recursive lock加了锁的地方现在全被你用一个pthread_cond_wait给解了,那么它们并不知道啊,等于是锁了也白锁

论坛徽章:
0
发表于 2009-02-05 23:20 |显示全部楼层
原帖由 太平绅士 于 2009-2-5 14:48 发表


为符合condition_wait的目的,condition_wait应该将mutex完全unlock,以便其他线程可以lock.
我认为pthread_cond_wait这样做完全是设计错误。


无理

论坛徽章:
0
发表于 2009-02-06 04:13 |显示全部楼层
设计理念问题,LZ觉得不对可以反馈给nptl开发组的。

不过这种问题,我觉得差下posix关于线程方面的标准可能好些。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP