免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1479 | 回复: 0
打印 上一主题 下一主题

《Python标准库》thread多线程同步互斥部分疑惑探讨求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-02 22:54 |只看该作者 |倒序浏览
周末阅读《Python标准库》一书的threading部分,以我vxworks为主的c语言开发经验,对标准库中的task同步互斥理解有些疑问,不得要领。
在此把我的疑惑讲出来,求得各位有何见解和真知。

案例代码:
1.1同步线程 使用 condition
  1. logging.basicConfig(
  2.     level=logging.DEBUG,
  3.     format='%(asctime)s (%(threadName)-2s) %(message)s',
  4.     )

  5. def consumer(cond):
  6.     """wait for the condition and use the resource"""
  7.     logging.debug('Starting consumer thread')
  8.     t = threading.currentThread()
  9.     [color=Red]with cond:[/color]
  10.         [color=Red]cond.wait()[/color]
  11.         logging.debug('Resource is available to consumer')

  12. def producer(cond):
  13.     """set up the resource to be used by the consumer"""
  14.     logging.debug('Starting producer thread')
  15.     [color=Red]with cond:[/color]
  16.         logging.debug('Making resource available')
  17.         [color=Red]cond.notifyAll()[/color]

  18. condition = threading.Condition()
  19. c1 = threading.Thread(name='c1', target=consumer,
  20.                       args=(condition,))
  21. c2 = threading.Thread(name='c2', target=consumer,
  22.                       args=(condition,))
  23. p = threading.Thread(name='p', target=producer,
  24.                      args=(condition,))

  25. c1.start()
  26. time.sleep(2)
  27. c2.start()
  28. time.sleep(2)
  29. p.start()
复制代码
1.2线程间传递信号 使用event
  1. logging.basicConfig(level=logging.DEBUG,
  2.                     format='(%(threadName)-10s) %(message)s',
  3.                     )
  4.                     
  5. def wait_for_event(e):
  6.     """Wait for the event to be set before doing anything"""
  7.     logging.debug('wait_for_event starting')
  8.     event_is_set = [color=Red]e.wait()[/color]
  9.     logging.debug('event set: %s', event_is_set)

  10. def wait_for_event_timeout(e, t):
  11.     """Wait t seconds and then timeout"""
  12.     while not e.isSet():
  13.         logging.debug('wait_for_event_timeout starting')
  14.         event_is_set = e.wait(t)
  15.         logging.debug('event set: %s', event_is_set)
  16.         if event_is_set:
  17.             logging.debug('processing event')
  18.         else:
  19.             logging.debug('doing other work')


  20. e = threading.Event()
  21. t1 = threading.Thread(name='block',
  22.                       target=wait_for_event,
  23.                       args=(e,))
  24. t1.start()

  25. t2 = threading.Thread(name='nonblock',
  26.                       target=wait_for_event_timeout,
  27.                       args=(e, 2))
  28. t2.start()

  29. logging.debug('Waiting before calling Event.set()')
  30. time.sleep(3)
  31. [color=Red]e.set()[/color]
  32. logging.debug('Event is set')
复制代码
疑问:
我初略理解为condition就是lock+event。
所以同步线程采用的代码结构是
  1.     with cond:
  2.         cond.wait()

  3.     with cond:
  4.         cond.notifyAll()
复制代码
而线程间传递信号的代码结构是
  1. e.wait()

  2. e.set()
复制代码
我觉得cond用法中的lock是重复的,反正就需要cond.wait了(忽略e.set()和cond.notifyAll()的细微差别)。我还要with cond(请求锁)干什么呢。
就像vxworks中semTake()和semGive()一样。这个cond.wait()天生就是包含了阻塞等待的意思。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP