The following is an error. You are welcome to make a brick.
A few people have dinner together, a few dumplings have a belly, and it's time to settle the bill. The dumplings are about 15 yuan a copy, and some others are added with others ..
Because there is no change, most of them only 50 yuan, 20 yuan, only a 5 yuan.
Next, the chaos arises. When a calculates with B, C and B calculate again. C does not know whether B calculates with a or just calculates with himself, and even sees the table, because I have paid for interview D and E on the desk.
...... Is it a bit messy ..
This involves the problems we may encounter in our work, because it is a data sharing problem in a non-well-handled multi-threaded environment. (Threads are the checkout behavior between each other, and data is the inseparable full money on the desktop)
Think about the general checkout process and method (here we do not consider the situation where one person pays the bills for all and then handles them separately ):
Do (P)
{
Lock (money on the desktop) // Lock Operation
{
If (the money on the desktop can be combined with P [I] to settle the account)
Do (settle P's account );
Else
Do );
}
}
Void checkout ()
{
Do, A collection of people who owe others' accounts is not convenient.
If (P. Count = 0) // you can clear the money on the table and ask the waiter to come or send it to the cashier.
Return;
For (I = 1; I <= P. Count; I ++)
{
Do (P [I]);
}
}