It's all written on the Internet.Private Object Thislock = new Object ();Lock (Thislock){...}So what's the syntax in lock () brackets, what curly braces {} should be writtenIf I have a funlog () method and then there are multiple threads that use this method, how do I lock this method?Do not put big theory, the theory of Cardinal himself went to MSDN study.Lock is to lock the code snippet in {} To prevent multiple threads from running the code snippet at the same time.Usage: private void do (
1. There are two points of view for the const modified member functions: logic constness and bitwise constness.
Bitwise constness's point of view is: "It does not change any bit of the object". In this way, the compiler only needs to assign values to the member variables. This is the definition of read-only. Therefore, the const member function cannot change any non-static member variable in the object.
Eg1:
Class TextBlock
{
Public:
............
Char operator [] (std: size_t position );
Const
to ensure thread synchronization Private Static ReadOnly ObjectLocker =New Object(); //define private constructors so that the outside world cannot create instances of that class PrivateSingleton () {}/// ///defining public methods provides a global access point, and you can also define public properties to provide global access points/// /// Public StaticSingleton getinstance () {//when the first thread runs here, the
method is:
Private Static readonly locker = new object (); // or write like this. // Private Static locker = new object (); lock (locker) {If (... = NULL ){...}}
The above practice is feasible, but there is a problem that the locker should be locked first each time a response is returned, which sacrifices some perfor
//call a method that is not lock + C1. Donotlockme (); A //call the Lock method and attempt to dismiss the deadlock theC1. Lockme (false); + } - } $}In the T1 thread, Lockme calls lock (this), which is the C1 in the main function, when you call Lock (C1) in the main thread, you must wait for the lock block in T1 to complete before you can access C1, that is, all C1-related operations cannot be completed. So we see even C1. None of the Donotloc
1, Qmutexqmutex mutex;void func () {Mutex.lock (), ... mutex.unlock ();} 2, Qmutex teamed qmutexlocker in complex functions or exception handling, the lock () and unlock () operation of the mutex will be very complex, the entry point to lock (), at all the jumping point to unlock (), think of the egg hurts! Forgetting unlock () will be a tough thing to do, so QT introduced Qmutexlocker to avoid lock () and unlock () operations. qmutex mutex;void Complexfunc () {Qmutexlocker
Inadvertent use of weakhashmap leads to memory overflow analysis: There are several applications in the community that use similar locker code, essentially extracted from the messaging system engine, except that the ID in the locker used in the message is of type long. The ID is automatically converted to long by the JVM as the key is put into the map, so that the key in Weakhashmap is referenced by the va
t3 = new Thread2 ();
Thread2 T4 = new Thread2 ();
T1.start ();
T2.start ();
T3.start ();
T4.start ();
}
The following code does not achieve the effect of synchronization, although the method uses the synchronized modifier, but each time it comes out of the new keyword is a different object, their monitor objects are different, yes. So how do you make these four objects (T1, T2, T3, T4) Use the same monitor to achieve the sync effect?
Class Somethread extends Thread {
Object
Encrypt the folder contents of the Windows system, the encrypted folder content can only be seen after the password has been entered. First on the code:CLS@ECHO OFFtitle Folder Privateif EXIST "Locker" goto UNLOCKif not EXIST Private goto Mdlocker: CONFIRMEcho is you sure-want to lock the folder (y/n)set/p "Cho=>"if%cho%==y goto LOCKif%cho%==y goto LOCKif%cho%==n goto ENDif%cho%==n goto ENDecho Invalid choice.Goto CONFIRM: LOCKren Private "
I. Thread synchronization in multithreaded programmingThe Lock keyword in 1.c#The lock keyword may be the most common way we encounter the need for thread synchronization, but lock is just a syntactic sugar, so why do you say that, slowly?(1) The equivalent code of lock is actually the Enter and exit two methods of the Monitor classPrivate Object locker = new Object (); public void works () { lock (
: typedef qmultihash
guardhash;
the hash table stores pointer values and pointer addresses, so the code added is as follows:
void qmetaobject: addguard (qobject ** PTR)
{ return;
guardhash * hash = guardhash ();
If (! Hash) {
* PTR = 0;
return;
} hash-> insert (* PTR, PTR);
}
Why not just save one pointer? It was originally intended to prevent accidental deletion. Let's look at the deleted code: void qmetaobject: removeguard (qobject ** PTR)
Office 3. Use the central stairway to go to the second floor and get the "bent needle" on the table in the women's locker room ", in the blood-stained white clothing of men's locker room, get "examinarion room kry" 4. Return to the consultation room on the first floor, use "examinarion room kry" to open the door and then enter the password (question 3) for the third-floor door. 5. Enter examining room 3
In Win7 and above systems, folders can be encrypted and hidden, in the following steps:
1, create a new text file, the location at random, enter the following content:
Cls
@ECHO off
Title Folder Private
If EXIST "HTG Locker" goto UNLOCK
If not EXIST Private goto Mdlocker
: CONFIRM
Echo, are you sure you want to encrypt the hidden private folder? (y/n)
set/p "Cho=>"
If%cho%==y goto LOCK
If%cho%==y goto LOCK
If%cho%==n Goto end
If%cho%=
)); }
}//Initialize connection pool
voidConnectionpool::initconnectpool (intInitialSize) {//Locking, add a connection
STD::lock_guardSTD::mutex> Locker (lock);
Addconn (initialsize); }//Destroy a connection
voidConnectionPool::d estoryoneconn () {//Smart pointer plus std::move transfer "ownership" of a connection, when out of scope, automatically call Close connect
STD::shared_ptrSTD:: Move (Conlist.front ());
Sp->close ();
--poolsize; }//
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.