zoukankan      html  css  js  c++  java
  • 线程同步之mutex和event区别

    之前只是用过 关键段来对同进程不同线程进行互斥,防止对同一份资源或代码段的竞争;

    mutex可以理解为不同进程或者同一进程内防止对同一份资源的竞争;

    event更多的是同步,当然也是不同进程或者同一进程,用于事件的同步,要先做A再做B,这时候就可以用Event来通知;

    嗯,我立即的可能还是有问题的,还是收录些大牛的评论吧:

    You use a mutex to ensure that only one thread of execution can be accessing something. For example, if you want to update a list that can potentially be used by multiple threads, you'd use a mutex:

    acquire mutex
    update list
    release mutex

    With a mutex, only one thread at a time can be executing the "update list".

    You use a manual reset event if you want multiple threads to wait for something to happen before continuing. For example, you started multiple threads, but they're all paused waiting for some other event before they can continue. Once that event happens, all of the threads can start running.

    The main thread would look like this:

    create event, initial value false(not signaled)
    start threads
    do some other initialization
    signal event

    Each thread's code would be:

    do thread initialization
    wait forevent to be signaled
    do thread processing
  • 相关阅读:
    数据库应用-java+sqlserver(十)StuInfo
    数据库应用-java+sqlserver(九)StudentMain和StudentManager
    数据库应用-java+sqlserver(八)StoreScore
    数据库应用-java+sqlserver(七)SqlManager
    小学期学习总结二
    小学期学习总结一
    四则运算小程序
    第二小组第七周学习心得
    需求分析
    MSF
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3305578.html
Copyright © 2011-2022 走看看