The lock trap in C #, cf

Source: Internet
Author: User

The lock trap in C #, cf

I mentioned it again. Maybe many people may wonder why C # cannot lock a struct? For example:

Public void ProcessTask (int taskid ){
Lock (taskid ){.....}
}

Compile to say that the lock can only use the reference type. Some people are smart (I think I used to be "smart "..), Do this: lock (object) taskid ){...}

However, the actual experience tells me that this will not work. The lock requires reference, and strictly speaking, it requires an instance of the object.

Even if the object is the same in the sense, but if it is not ReferenceEquals, then it will be treated as two instances, then C # lock is not the same thing. That is to say, when you think this lock takes effect, it is actually useless.

In the preceding example, because lock (object) taskid) is packed once every time it is executed, the boxed object is not the same instance (all are completely new instances ), so lock (object) taskid ){...} it's done in white.

Of course, lock (object) 123) {} is also a problem.

But this is better: lock ("helloworld") {}. Why is it just "better", not "no problem. The reason is that DotNet allocates strings. DotNet allocates a fixed space for the strings in each Assembly. So every time you reference "helloworld", it is the same instance. However, this string will not be shared in other assemblies. If several assemblies are written in this way, they each have their own "helloworld"

Good practice:

Lock (this )...
Lock (typeof (ThisType ))
Lock (GetType () // do not mess up unless you understand what it is.
Lock (SomeType. StaticSyncObject)
Lock (someinst. SyncObject)
Other bad practices

Lock ("task:" + id)
Lock (filename)
Of course, the specific lock is a design protocol and specification. However, when using lock, you must specify whether the object is the same instance as you think.

What should I do if I want to lock a variable value in Equals?
This can refer to the HashCodeLock under the http://www.lostinet.com/files/ (many details can be optimized)


In C language ^ how to use a1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is the same as 0 and the bitwise value is the same as 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
The above two circuits, two switches a and B, respectively. The opening status is \ [1]. The closing status is/[0].
If both are enabled or disabled, both circuits are connected.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, when circuit a and circuit B are in the same State, the power is [0], a, and circuit B are powered on at the same time [1].
C Language & |! What are the variables used by the & access operator?
Compared with the definition of variable compilation, the system will allocate space in memory.
Space memory location address & extracted address
E. g int a; the address ratio allocated to the database during compilation is 2000; & a2000
False first defines the integer pointer variable pp = & a; assigns address a 2000 to p to run p = 2000
Scanf ("% d", & a); when entering 3, the system will first find the memory space of a from the address according to & a, and then write 3 into the space.
* The pointer operator is used to retrieve variable values based on the variable address.
Compare to * a value variable a value 3
The following is a summary of definitions and declarations using pointers.
Int * p; Define pointer to integer data
Int * p [n]; the defined pointer array p is composed of n elements pointing to the Integer Data Pointer.
Int (* p) [n]; p points to the array pointer variable containing the nelement dimension
Int * p (); p returns the pointer function. This Pointer Points to integer data.
Int (* p) (); p points to the function pointer. This function returns an integer value.
Int ** p; p pointer variable pointing to Integer Data Pointer variable
For more information about the system, see Tan haoqiang's c Programming (the third edition). This book is easy to understand and learn c language errors.

Related Article

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.