12 (to avoid deadlocks)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Precautions for preventing deadlocks:

(1) Write MultithreadingProgramFirst, write the correct program and then port it to multiple threads.

(2) check whether the program you write forgets to release the lock when it jumps out.

(3) If your module may use a lock again, we recommend that you use a nested lock.

(4) For some locksCodeDo not temporarily re-compile. We recommend that you use the locks in the library or the locks you have written.

(5) If a business needs to obtain multiple locks, it must be ensured that the locks are obtained in a certain order; otherwise, a deadlock will occur.

(6) write a simple test case to verify whether there is a deadlock

(7) write a program to verify deadlocks and avoid deadlocks from the source.

 

First, define basic data structures and macros,

[CPP] View plaincopy
  1. Typedef Struct_ Lock_info
  2. {
  3. CharLockname [32];
  4. HandleHlock;
  5. } Lock_info:
  6. Typedef Struct_ Thread_lock
  7. {
  8. IntThreadid;
  9. Lock_info * plockinfo [32];
  10. } Thread_lock;
  11. # Define critical_section_type 1
  12. # Define mutex_lock_type 2
  13. # Define semaphore_lock_type 3
  14. # Define normal_lock_type 4
  15. # Define waitforsingleobject (a, B )\
  16. Waitforsingleobject_stub ((Void*) A, normal_lock_type)
  17. # Define entercriticalsection ()\
  18. Waitforsingleobject_stub ((Void*) A, critical_section_type)
  19. # Define releasemutex ()\
  20. Releaselock_stub ((Void*) A, mutex_lock_type ))
  21. # Define releasesemaphore (a, B, c )\
  22. Releaselock_stub ((Void*) A, semaphore_lock_type ))
  23. # Define leavecriticalsection ()\
  24. Releaselock_stub ((Void*) A, critical_section_type ))

Then, rewrite the lock application function,

[CPP] View plaincopy
  1. VoidWaitforsingleobject_stub (Void* Hlock,IntType)
  2. {
  3. /* Step 1 */
  4. Waitforsingleobject (hdbglock );
  5. /* Check if Lock Loops arounds threads */
  6. Releasemutex (hdbglock );
  7. /* Step 2 */
  8. If(Normal_lock_type = type)
  9. Waitforsingleobject ((Handle) Hlock, infinite );
  10. Else If(Critical_section_type = type)
  11. Entercriticalsection (lpcritical_section) hlock );
  12. Else
  13. Assert (0 );
  14. /* Step 3 */
  15. Waitforsingleobject (hdbglock );
  16. /* Add lock to specified threadid list */
  17. Releasemutex (hdbglock );
  18. }

Finally, you need to rewrite the lock release function.

[CPP] View plaincopy
  1. VoidReleaselock_stub (Void* Hlock,IntType)
  2. {
  3. /* Step 1 */
  4. Waitforsingleobject (hdbglock );
  5. /* Remove lock from specified threadid list */
  6. Releasemutex (hdbglock );
  7. /* Step 2 */
  8. If(Mutex_lock_type) = type)
  9. Releasemutex (Handle) Hlock );
  10. Else If(Semaphore_lock_type = type)
  11. Releasesemaphore ((Handle) Hlock, 1, null );
  12. Else If(Critical_section_type = type)
  13. Leavecriticalsection (lpcritical_section) hlock );
  14. Assert (0 );
  15. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.