iOS Core notes-multi-threaded-atomic/non-atomic properties

Source: Internet
Author: User

1, Atomic properties: 1-1, Nonatomic and Atomic:
    • nonatomic: Non-atomic properties;
    • atomic: Atomic properties;
      • Thread-safe, property modifiers for multithreaded design, are the default values.
      • Ensure that only one thread can write at the same time, but multiple threads can read at the same time;
      • Single-write multi-read: single thread write write , multiple threads can read read ;
      • atomicThere is a lock in itself 自旋锁 .
1-2. nonatomicAnd atomicContrast:
    • nonatomic: Non-thread-safe, suitable for small memory mobile devices;
    • atomic: Thread safety requires a lot of resources. Performance is worse than non-atomic properties.
1-3, the development of IOS recommendations:
    • All attributes are declared to nonatomic be of higher performance;
    • Try to avoid multi-threading to rob the same piece of resources;
    • As far as possible, locking, resource-grabbing business logic to the server-side processing, reduce the pressure of mobile clients.
2. Simulate atomic properties: 2-1, define attributes:
1./// 非原子属性
[email protected] (nonatomic,strong) NSObject *obj1;
3./// 原子属性:内部有"自旋锁"
[email protected] (atomic,strong) NSObject *obj2;
5./// 模拟原子属性
[email protected] (atomic,strong) NSObject *obj3;

?了解:To override non-atomic properties setter and getter methods:

?了解:1. After overriding the method of the atomic attribute, setter it will overwrite the inner of the atomic attribute and 自旋锁 invalidate it; then we join to 互斥锁 simulate 单写多读 .

?了解:2, after overriding the attributes setter and getter methods, the system will not help us to generate the underlined member variable, using the composition directive @synthesize , you can manually generate the underlined member variable.

Sample program:

1.// 合成指令
[email protected] obj3 = _obj3;
3.
4./// obj3的setter方法
5.- (void)setObj3:(NSObject *)obj3
6.{
7. @synchronized(self) {
8. _obj3 = obj3;
9. }
10.}
11.
12./// obj3的getter方法
13.- (NSObject *)obj3
14.{
15. return _obj3;
16.}
2-2. Performance test:
  1.///tests for non-atomic properties, mutex, spin lock performance 
2.-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: ( Uievent *) Event
3.{
4. Nsinteger largenum = 1000*1000;
5.
6. NSLog (@ "Non-atomic attribute");
7. Cfabsolutetime start = Cfabsolutetimegetcurrent ();
8. for (int i = 0; i < Largenum; i++) {
9. Self.obj1 = [[NSObject alloc] init];
Ten.}
11. NSLog (@ "non-atomic attribute = =%f", cfabsolutetimegetcurrent ()-start);
.
13. NSLog (@ "Atomic attribute");
. Start = Cfabsolutetimegetcurrent ();
. for (int i = 0; i < Largenum; i++) {
16. Self.obj2 = [[NSObject alloc] init];
.}
18. NSLog (@ "Atomic properties =%f", cfabsolutetimegetcurrent ()-start);
.
20. NSLog (@ "Analog atomic attribute");
. Start = Cfabsolutetimegetcurrent ();
for (int i = 0; i < Largenum; i++) {
23. SELF.OBJ3 = [[NSObject alloc] init];
.}
25. NSLog (@ "Analog atomic properties = =%f", cfabsolutetimegetcurrent ()-start);
.}
3, mutual exclusion lock and spin lock comparison: 3-1, Common:
    • can guarantee that at the same time, only one thread executes the code that locks the range
3-2, different points:
    • 互斥锁: If another thread is found to be executing the locked code, the thread enters the 休眠 state, waits for the other thread to finish, the thread re-enters the state after the lock is opened, and 就绪 waits for the CPU to be dispatched again.
    • 自旋锁: If you find that another thread is executing the locked code, the thread will be in 死循环 the same way; wait until the lock code execution is complete.
4, development recommendations:
      1. All properties are declared to be nonatomic almost identical to the performance of atomic and non-atomic properties.
      2. Try to avoid multiple threads robbing the same piece of resources.
      3. To achieve thread safety, it must be used ; no matter what locks, there is performance consumption.
      4. Spin locks are more suitable for executing very short code, and the inside of a dead loop is not suitable for writing complex code.
      5. As far as possible to lock, resource-grabbing business logic to the server-side processing, reduce the pressure of mobile clients.
      6. For a smooth user experience, the threads of the Uikit class library are unsafe, so we need to update the UI on the main thread (the UI thread).
      7. All NSMutable of the contained classes are thread insecure, and when doing multithreaded development, you need to be aware of the thread-safety issues of multi-threaded simultaneous manipulation of mutable objects.

iOS Core notes-multi-threaded-atomic/non-atomic properties

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.