Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ realize that it takes a short time after acquiring the mutex lock?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how C++ can achieve the short time spent after obtaining the mutex lock". In daily operation, I believe that many people have doubts about the short time spent after C++ how to obtain the mutex lock. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "how does C++ achieve the short time spent after obtaining the mutex lock?" Next, please follow the editor to study!

CP.43: minimize the time spent in the critical area

Reason (reason)

The less time is spent with a mutex taken, the less chance that another thread has to wait, and thread suspension and resumption are expensive.

The shorter the time it takes to acquire the mutex lock, the less chance that other threads will have to wait. The cost of thread blocking and awakening is too high.

Example (sample)

Void do_something () / / bad

{

Unique_lock lck (my_lock)

Do0 (); / / preparation: does not need lock

Do1 (); / / transaction: needs locking

Do2 (); / / cleanup: does not need locking

}

Here, we are holding the lock for longer than necessary: We should not have taken the lock before we needed it and should have released it again before starting the cleanup. We could rewrite this to

Here, we have held the lock for more than necessary: we should not acquire the lock when we do not need it; on the other hand, we should release the lock before we start cleaning. We can rewrite the code like this:

Void do_something () / / bad

{

Do0 (); / / preparation: does not need lock

My_lock.lock ()

Do1 (); / / transaction: needs locking

My_lock.unlock ()

Do2 (); / / cleanup: does not need locking

}

But that compromises safety and violates the use RAII rule. Instead, add a block for the critical section:

But this approach makes compromises in terms of security and violates RAII guidelines. As an improvement, you can add a code block to the critical area:

Void do_something () / / OK

{

Do0 (); / / preparation: does not need lock

{

Unique_lock lck (my_lock)

Do1 (); / / transaction: needs locking

}

Do2 (); / / cleanup: does not need locking

} Enforcement (implementation recommendations)

Impossible in general. Flag "naked" lock () and unlock ()

It's not usually possible. Mark exposed lock and unlock operations.

This is the end of the study on "it takes a short time for C++ to acquire a mutex lock". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report