2011-07-02

複数のmutexが欲しい状況

複数のmutexが必要な状況を考えてみた。

// 排他的にアクセスするリソース
class exclusive_resource
{
public :
    std::vector<int> v ;
private :
    std::mutex m ;
public :
    void lock() { m.lock() ; }
    void try_lock() { m.try_lock() ; }
    void unlock() { m.unlock() ; }
} ;

exclusive_resource res1, res2 ;

void thread1()
{// res1のみを操作
    std::lock_guard< exclusive_resource > guard( res1 ) ;
    res1.v.push_back(0) ;
}

void thread2()
{// res2のみを操作
    std::lock_guard< exclusive_resource > guard( res2 ) ;
    res2.v.push_back(0) ;
}

void thread3()
{// res1, res2両方を操作
    std::lock( res1, res2 ) ; // デッドロックを回避するためstd::lockを使用すること
    // res1, res2両方を操作、実際には途中のreturnや例外に気をつけること
    res1.unlock() ; res2.unlock() ;
}

void thread4()
{// thread3と同じく、res1, res2の両方を操作、thread3とは異なる処理
    std::lock( res1, res2 ) ; // デッドロックを回避するためstd::lockを使用すること
    // res1, res2両方を操作、実際には途中のreturnや例外に気をつけること
    res1.unlock() ; res2.unlock() ;
}

今、異なるスレッド間で操作したいオブジェクトが複数あるとする。しかし、常に全オブジェクトを操作する必要はないものとする。とすれば、この複数のオブジェクトを、単一のmutexで排他的にアクセスするのは、非効率的である。したがって、アクセスしたい単位ごとに、別々のmutexを用意するのは当然である。

しかしまた、ある状況では、複数の単位に、同時にアクセスしたい場合もあるとする。この場合、複数のmutexをlockしなければならない。しかし、普通に一つづつlockしたのでは、デッドロックに陥る可能性がある。このため、デッドロックを避けるように気をつけつつロックしなければならない。

std::lockは、lock(), try_lock(), unlock()を組み合わせた一連の呼び出しにより、デッドロックに陥らない方法で、すべてのLockable要求を満たす型のオブジェクトをロックしてくれる。

まとめとしては、複数のmutexを同時にlockする際には、デッドロックを避けるために、std::lockを使うべきである。

203 comments:

1 – 200 of 203   Newer›   Newest»
deepak said...

nice online tutor

Anonymous said...

nice online tutor

deepak said...

nice online tutor

deepak said...

nice online tutor

deepak said...

nice online tutor

kartik said...

nice online tutor

kartik said...

nice online tutor

deepak said...

Very Niceonline tutoring for high school students

motivation456 said...

Very Niceonline coding tutor

motivation456 said...

Very Niceonline tutoring for high school students

motivation456 said...

Very Niceonline tutoring sites for computer science

motivation456 said...

Very Niceengineering tutor online

motivation456 said...

Very Niceengineering tutor online

motivation456 said...

Very Nicebe an online english tutor

deepak said...

Very Niceonline coding tutor

deepak said...

Very Niceonline english language tutor

deepak said...

Very Niceonline english language tutor

deepak said...

Very Niceonline english language tutor

deepak said...

very nice paid online tutoring

deepak said...

Very Nicecollege algebra tutor online

deepak said...

very nice best online tutoring sites

motivation456 said...

very nice best online tutoring sites

motivation456 said...

very nice math homework help online

motivation456 said...

very nice math homework help online

kartik said...

very nice precalculus tutoring online

kartik said...

Very Nicebest ai courses online

kartik said...

Very Nicebest ai courses online

motivation456 said...

Very Nicebest online coaching for csir net chemical science

motivation456 said...

Very Nicejavascript online classes

deepak said...

Very Nicesara's cooking class

motivation456 said...

Very Niceonline algebra course

motivation456 said...

Very Niceblack desert mobile classes

motivation456 said...

Very Niceelder scrolls online best class

deepak said...

Very Nicebest machine learning course online

deepak said...

Very Nicesara's cooking class

motivation456 said...

Very Nicesara's cooking class

Good morning quotes for Wednesday said...

Very Nicejava online course

Good morning quotes for Wednesday said...

Very Nicebest machine learning course online

deepak said...

Very Niceblack desert online classes

deepak said...

Very Niceelder scrolls online classes

motivation456 said...

very nice online tutoring services

deepak said...

very nice online tutoring sites

motivation456 said...

very nice nettutor

motivation456 said...

Very Nicesara's cooking class

motivation456 said...

Very Nicetally course near me

motivation456 said...

Very Nicejava courses near me

motivation456 said...

Very Niceelder scrolls online classes

deepak said...

very nice online tutoring sites

deepak said...

Very Niceorganic chemistry ii online

deepak said...

Very Nicems word online course

motivation456 said...

Very Nicedungeon fighter online best class

motivation456 said...

Very Nicedungeon fighter online best class

bhanu said...

very nice precalculus tutoring online

bhanu said...

Very Nicebest online coaching for csir net chemical science

bhanu said...

Very Nicejavascript online classes

deepak said...

Very Nicelive math tutor

motivation456 said...

Very Niceedx ap chemistry

motivation456 said...

Very Nicefinancial accounting online course harvard

motivation456 said...

very nice online chemistry tutor

deepak said...

Very Niceonline tutor for mechanical engineering

deepak said...

Very Nicetissnet online coaching

deepak said...

Very Nicemicrosoft access classes online

deepak said...

Very Nicebest machine learning course coursera

motivation456 said...

Very Nicestanford free machine learning course

bhanu said...

Very Nicebasic accounting course

durga said...

Very Niceselenium online training

bhanu said...

very nice quran tutor at home

durga said...

Very Niceblack desert online classes

bhanu said...

very nice live homework help

bhanu said...

very nice tutor online from home

bhanu said...

very nice precalculus tutoring online

bhanu said...

Very Nicejava online classes

deepak said...

Very Nicebasic accounting course

bhanu said...

very nice quran tutor at home

bhanu said...

very nice live homework help

bhanu said...

very nice tutor online from home

durga said...

Very Niceselenium online training

bhanu said...

very nice precalculus tutoring online

bhanu said...

Very Nicejava online classes

durga said...

Very Nicesql training online

deepak said...

Very Nicewarhammer online age of reckoning classes

bhanu said...

Very Niceonline accounting classes for cpa

bhanu said...

Very Niceonline accounting classes for cpa

durga said...

Very Niceblack desert classes

bhanu said...

Very Nicebasic maths courses for adults

deepak said...

online tutoring

deepak said...

online tutoring

deepak said...

online tutoring

deepak said...

online tutoring

deepak said...

online tutoring

bhanu said...

online tutoring

deepak said...

online tutoring

bhanu said...

online tutoring

bhanu said...

online tutoring

bhanu said...

online tutoring

deepak said...

Very Niceorganic chemistry ii online

bhanu said...

online tutoring

deepak said...

Very Nicebasic maths courses for adults

durga said...

Very Niceonline tally course

deepak said...

Very Niceprinciples of accounting 1 online course

deepak said...

Very Nicefree level 1 maths courses online

bhanu said...

Very Nicefree online coaching for ugc net in hindi

durga said...

Very Nicesql online course

durga said...

very nice online english tutor

bhanu said...

Very Nicedatabase administrator courses online free

bhanu said...

Very Niceoracle dba course online

deepak said...

Very Nicecabal force blader

bhanu said...

Very Nicebest machine learning course coursera

deepak said...

Very Niceblack desert ps4 classes

bhanu said...

very nice online tutoring sites

bhanu said...

very nice nettutor

bhanu said...

Very Niceonline math classes

deepak said...

Very Niceblack desert best class

deepak said...

very nice gmat tutor

bhanu said...

Very Niceselenium online training

bhanu said...

Very Niceblack desert online classes

bhanu said...

Very Nicebookkeeping courses near me

deepak said...

very nice online math tutor

bhanu said...

Very Niceselenium online training

deepak said...

Very Nicecsir net online coaching

motivation456 said...

Very Nicestanford online courses machine learning

bhanu said...

very nice paid online tutoring

bhanu said...

Very Nicebasic accounting course online

bhanu said...

Very Nicenational 5 maths online course

bhanu said...

Very Nicehuman biology course online

deepak said...

Very Nicebasic bookkeeping course

bhanu said...

very nice quran tutor at home

bhanu said...

very nice quran tutor at home

bhanu said...

very nice live homework help

bhanu said...

very nice tutor online from home

bhanu said...

very nice paid online tutoring

bhanu said...

Very Nicejava online classes

deepak said...

Very Nicesara's cooking class unblocked

bhanu said...

Very Nicewarhammer online age of reckoning classes

deepak said...

Very Nicecabal force blader

bhanu said...

Very Niceedx ap chemistry

bhanu said...

Very Nicefinancial accounting online course harvard

deepak said...

Very Niceorganic chemistry ii online

deepak said...

Very Nicebasic maths courses for adults

deepak said...

Very Nicebest selenium online training

bhanu said...

Very Niceandroid development course free online

bhanu said...

Very Niceandroid development course free online

bhanu said...

Very Nicesword art online integral factor classes

bhanu said...

Very Niceblack desert ps4 launch classes

deepak said...

Very Niceugc net online coaching commerce

deepak said...

Very Nicebest machine learning course coursera

bhanu said...

Very Niceudacity free courses web development

bhanu said...

Very Niceneverwinter online druid

deepak said...

Very Niceelder scrolls online best class

deepak said...

Very Niceonline biology courses

bhanu said...

very nice online tutoring sites

bhanu said...

very nice nettutor

bhanu said...

Very Niceonline math classes

bhanu said...

Very Niceaccounting classes online

deepak said...

Very Niceblack desert mobile classes

deepak said...

Very Niceelder scrolls online necromancer

deepak said...

very nice online tutoring services

bhanu said...

Very Niceselenium online training

bhanu said...

Very Niceblack desert online classes

bhanu said...

Very Nicebookkeeping courses near me

bhanu said...

Very Niceselenium online training

bhanu said...

Very Nicewarhammer online age of reckoning classes

deepak said...

Very Nicewarhammer online age of reckoning classes

bhanu said...

Very Nicems word online course

deepak said...

Very Niceedx ap chemistry

bhanu said...

Very Niceorganic chemistry summer course online

bhanu said...

Very Nicetissnet online coaching

deepak said...

Very Nicelatale new class

deepak said...

Very Niceandroid development course free online

deepak said...

Very Nicesword art online integral factor classes

deepak said...

Very Niceblack desert ps4 launch classes

bhanu said...

Very Nicemicrosoft access classes online

deepak said...

Very Niceudacity free courses web development

deepak said...

Very Niceneverwinter online druid

bhanu said...

Very Nicebest machine learning course coursera

bhanu said...

Very Niceblack desert classes

bhanu said...

Very Niceblack desert online classes

bhanu said...

Very Niceblack desert ps4 classes

bhanu said...

very nice nettutor

deepak said...

Very Nicestanford free machine learning course

bhanu said...

Very Nicetissnet online coaching

bhanu said...

Very Nicemicrosoft access classes online

bhanu said...

Very Nicebest machine learning course coursera

bhanu said...

very nice paid online tutoring

deepak said...

Very Niceoracle online course

deepak said...

Very Niceml courses online

bhanu said...

Very Niceonline chemistry classes

bhanu said...

very nice quran tutor at home

deepak said...

Very Niceonline chemistry courses community college

bhanu said...

very nice online tutoring for college students

bhanu said...

very nice tutor online from home

bhanu said...

Very Nicejava online classes

bhanu said...

Very Niceugc net online coaching

deepak said...

very nice online math tutor

bhanu said...

Very Nicewarhammer age of reckoning classes

deepak said...

very nice online math tutor

deepak said...

Very Niceblack desert ps4 classes

deepak said...

Very Nicebookkeeping course online

bhanu said...

Very Nicejavascript online course

bhanu said...

Very Niceaccounting courses near me

«Oldest ‹Older   1 – 200 of 203   Newer› Newest»