SolmyR's small piece series: a pair

zhaozj2021-02-17  167

"~~~~!"

A file clamping a beautiful arc, crossing four tables, two partition walls, a walkway, unbiased through the passers-by and passerby B, accurate hit goals. Looking at the company up and down, there is this throwing method, only SolmyR, and his goal, nature is Zero.

"Hey!", Zero touched the back of the back, half of the sigh, sigh, sigh: don't ask, he must have any handle to grab it by Solmyr.

"What is wrong with this time?" Zero hurriedly interrupted a happy chat between the only female programmer Pisces within 50 meters, came to SolmyR to see where it was wrong.

"The code you just submit will cause the thread to kill", Solmyr points to a function submitted by Zero:

Void Some_Func () {pthread_mutex_lock (& ​​MTX); ... ...... Pthread_Mutex_Unlock (& ​​MTX);}

"Will it? I obviously releases the mutual exclusive variable at the end of the function?"

Solmyr looked at ZERO, the expression is clear: the dead wood is not angry. He told the two lines of code in the middle of the function:

Void Some_Func () {pthread_mutex_lock (& ​​MTX); ... if (status == e_fail) returnck; ... pthread_mutex_unlock (& ​​mtx);}

"OOPS!", Zero took a look, "I know, I know, I will change it."

"Do you know? Tell me what mistakes you made?"

"I forgot to return to the middle of the function returns."

"Then how you are going to solve this problem,"

"Well ... very simple, add a line of code here, like this:"

IF (status == e_fail) {pthread_mutex_unlock (mtx); Return;}

SolmyR shakes his head: "You are a headache doctor, the foot pain is a doctor. If you don't have a lock in this function, not only one return point, how do you plan to do? Do you do this?"

"Well ... Do you mean that I should follow a function of only one return point?", Zero scratched the head, some are less sure.

"I don't mean this. In some cases, only one return point will result in a huge IF / ELSE structure, reduce the readability of the code. And, even if your function has only one return point, you still have a possibility To this question. Consider such a function: ", SolmyR is fast to type:

Void some_func () {pthread_mutex_lock (& ​​MTX); ... // There is no other return point in the middle ... foo (); // Function implemented by other programmers ... pthread_mutex_unlock (& ​​MTX);

"It doesn't look like a problem, but if the function of foo throws an exception, what happens?"

"Well ... If there is no to capture this anomalies in our function ... it will cause the Some_Func function to interrupt the foo ... Oh ...", Zero found the problem. "Then you can only call all the exceptions in each function that may throw an exception, then ...", Zero is more and smaller, "... then unlock in catch, then throw it ..." ZERO Stopping, the troubles scratched, found that he didn't convince yourself: such a solution is too cumbersome, too easy to introduce a mistake. "Ok?"

"Well, I admit that I don't know what to do, Solmyr, what should I do?"

"Remind, what are you discussing on the dinner table in the first two days?" (See "Solmy ''s small piece series" previous period, "garbage collection")

"Do you say garbage collection? Hey ... but ... Is that handled memory leak? What is the relationship with this question?"

"I don't mean the specific solution," Solmyr shakes his head. "The key is the universal principle introduced in the last discussion, that is, ..." Solmyr stopped, turned to look at Zero.

"... ......"

"Hey ...", Solmyr imitates the helpless expression in others - according to his own statement, this is the accumulation of years of training work - sighing: "I said Zero, you are still very young, will not be so early Will the memory have recession? "

... is really a hateful guy, and it is hated in Zero.

SolmyR's voice rang again in Zero approaches the crash edge: "If you want to ensure that something is paired, please use ..."

"Constructor and the destructor!", Zero is afraid to miss the opportunity to show yourself is not "memory recession".

"Don't call it so much." Solmyr frowned, "You are frightened with the front-row audience."

"? !!!", ZERO quickly turned, found that when I didn't know how to surround the company, everyone "normal" is doing their own things, but the action is slightly smoother ...

After solving the "audience", ZERO returned to the monitor, confidence was full: "I know Solmyr, here we can use and the last processing / release memory very similar means to handle the lock / unlock, as long as you write A very simple class is OK, like this: ", Zero said, while type:

class auto_lock {public: auto_lock (pthread_mutex_t mtx): m_mtx (mtx) {pthread_mutex_lock (& ​​m_mtx); // constructing lock} ~ auto_lock () {pthread_mutex_unlock (& ​​m_mtx); // unlock destructor}

PRIVATE: PTHREAD_MUTEX_T & M_MTX;}

Void some_func () {auto_lock (mtx); ... // return, foo, casually, do things ... // End, do not unlock}

"So, I have solved the problems I have encountered before, I can realize my function, no matter when I return or encounter an abnormal, I can affirm that MTX will be unlocked, don't worry about the thread dead lock problem."

"Well, yes." SolmyR thumbs up nodded and started to summarize: "This is actually a very common means, in addition to the two cases we discussed, it can also be applied in many occasions. For example, network access Establish a connection and disconnection, login and exit login in database access, easy to use it to implement a measuring a function of a function of time-consuming testing tools, and so on, etc. However, if you don't leave it, you all Behind the application is a unified principle ... "SolmyR took a meal, Zero Head took the gods:

"If you want to guarantee some things to appear, use the constructor and the destructor."

转载请注明原文地址:https://www.9cbs.com/read-31552.html

New Post(0)