A scenario prone to deadlock

Source: Internet
Author: User
Tags call back

Not long ago in the work encountered a deadlock problem, the special record.



Suppose there is such a scenario where you call an interface, in the process of calling it, there are two read and write locks, and the lock order is first A and then B. Then at the bottom of the network IO callback, you will also use these two locks, the lock order is first B after a. So there is a situation. If the thread T1 call the interface, just finished a lock, there is no upper B lock. The underlying callback thread T2 just finished with the B lock and has not been on a lock. At this time, thread T1 to apply for the B lock, but the B lock has been occupied by the thread T2, unable to succeed, T1 thread blocking, unable to release a lock in time, thread T2 to apply for a lock, but because a lock is occupied by the T1 thread, so the T2 thread is blocked, unable to release B lock. This is the problem of waiting for each other to release the lock resource, thus deadlock. Call interface in thread 1
void Callinterface ()
{
Utility::writerlocker Lockera (Mutexa);

Do something
DoSomething1 ();

Utility::writelocker Lockerb (MUTEXB);

Do something
DoSomething2 ();

IO Service

Doioservice ();

}


Ioservice call back in thread 2
void Callback ()
{
Utility::writerlocker Lockerb (MUTEXB);

Do something
DoSomething3 ();

Utility::writelocker Lockera (Mutexa);

Do something
DoSomething4 ();
}

There are two ways to solve this problem.
One, if the lock A and the lock B are really the same module, we can use recursive locks instead of read and write locks, and the characteristic of recursive locks is that the same thread can enter the same lock multiple times, but note that lock A and lock B use the same recursive lock mutex
User Recursive locker

Call interface in thread 1
void Callinterface ()
{
Utility::recursivelocker Lockera (Recursive_mutex);

Do something
DoSomething1 ();

Utility::recursivelocker Lockerb (Recursive_mutex);

Do something
DoSomething2 ();

IO Service

Doioservice ();

}


Ioservice call back in thread 2
void Callback ()
{
Utility::recursivelocker Lockerb (Recursive_mutex);

Do something
DoSomething3 ();

Utility::recursivelocker Lockera (Recursive_mutex);

Do something
DoSomething4 ();
}
Two, lock A and lock B in different modules, such as Lock B is in a DLL. This time can be like this, the following figure


When calling the interface, use lock a thread T1, and then use the process of lock B to place the T2 on another thread. Callback, when using the lock B, the thread T3, when using lock A, the thread T4. This eliminates the problem of waiting for each other to release the lock resource, thereby avoiding deadlocks.


Use Locker A in thread 1
void Callinterface ()
{
Use Locker A
Utility::writerlocker Lockera (Mutexa);

Do something
DoSomething1 ();

User locker B in Anohter thread
M_threads.get_io_service (). Post (Boost::bind (doSomething2));

}

Use Locker B in thread 2
void DoSomething2 ()
{
Utility::writelocker Lockerb (MUTEXB);

Do something
RealDoSomething2 ();

IO Service

Doioservice ();
}


Ioservice call back in thread 3, user locker B
void Callback ()
{
Utility::writerlocker Lockerb (MUTEXB);

Do something
DoSomething3 ();

User locker A in Anohter thread
M_threads.get_io_service (). Post (Boost::bind (DOSOMETHING4));
}


Use Locker A in thread 4
void DoSomething4 ()
{
Utility::writelocker Lockera (Mutexa);

Do something
RealDoSomething4 ();
}

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.