In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the security risks of multithreading in iOS development?" interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the hidden dangers of multithreading in iOS development?"
I. solution
Solution: use thread synchronization technology (synchronization, that is, coordinated pace, in a predetermined order)
The common thread synchronization technology is: locking
1 、 OSSpinLock
OSSpinLock is called a "spin lock". The thread waiting for the lock will be in a busy-wait state, occupying CPU resources all the time.
At present, it is no longer secure, and priority inversion problems may occur.
If the thread waiting for the lock has a higher priority, it will always occupy CPU resources, and the low-priority thread will not be able to release the lock.
Need to import header file # import
2 、 os_unfair_lock
Os_unfair_lock is used to replace unsafe OSSpinLock and is only supported from iOS10.
From the perspective of the underlying call, the thread waiting for the os_unfair_lock lock will be dormant, not busy, etc.
Need to import header file # import
3 、 pthread_mutex
Mutex is called a "mutex", and the thread waiting for the lock is dormant.
Need to import header file # import
Pthread_mutex- normal lock
Pthread_mutex- recursive lock
Pthread_mutex- condition
4 、 NSLock
NSLock is the encapsulation of mutex common lock.
5 、 NSRecursiveLock
NSRecursiveLock is also the encapsulation of mutex recursive lock. API is basically the same as NSLock.
6 、 NSCondition
NSCondition is an encapsulation of mutex and cond
7 、 NSConditionLock
NSConditionLock is a further encapsulation of NSCondition, and specific condition values can be set.
8 、 dispatch_semaphore
Semaphore is called "semaphore"
The initial value of the semaphore, which can be used to control the maximum number of concurrent access by threads
The initial value of the semaphore is 1, which means that only one thread is allowed to access resources at the same time, ensuring thread synchronization.
9. Dispatch_queue (DISPATCH_QUEUE_SERIAL)
Thread synchronization can also be achieved by directly using the serial queue of GCD.
10, @ synchronized
@ synchronized is the encapsulation of mutex recursive locks
Source code view: objc-sync.mm file in objc4 (official address of Apple source code)
@ synchronized (obj) generates recursive locks corresponding to obj, and then adds and unlocks them.
II. Performance comparison of iOS thread synchronization schemes
Principles:
The performance of ordinary lock is better than that of recursive lock
The more advanced the language, the more logic it encapsulates, and the worse the performance (for all languages)
Actual test
Sort performance from high to low
Os_unfair_lock / / disadvantage: only supported by iOS10
OSSpinLock / / disadvantages: priority reversal may occur. Apple is no longer safe and does not recommend it.
Dispatch_semaphore / / recommended
Pthread_mutex / / advantages: cross-platform mutexes (common locks) are recommended
Dispatch_queue (DISPATCH_QUEUE_SERIAL) / / c
NSLock / / oc
NSCondition / / oc
Pthread_mutex (recursive) / / Recursive lock
NSRecursiveLock / / oc
NSConditionLock / / oc
@ synchronized / / Recursive lock oc
III. Selection of spin lock and mutex lock
Spin lock: waiting state is busy, etc.
Mutex: waiting state is dormant
1. When is it more cost-effective to use a spin lock?
It is expected that the thread will have a short wait for the lock.
Locked code (critical section) is often called, but competition rarely occurs
CPU resources are not tight.
Multicore processor
2. When is it cost-effective to use mutexes?
It is expected that the thread will wait a long time for the lock.
Single core processor
The critical region has IO operation.
The code of the critical area is complex or the amount of circulation is large.
The competition in the critical area is very fierce.
Read-write lock
Scene:
Only one thread can write at a time
Allow multiple threads to read at the same time
At the same time, both write and read operations are not allowed.
The above scenario is a typical "multi-read and single-write", which is often used for reading and writing data such as files. The implementation schemes in iOS are as follows:
1. Read-write lock: pthread_rwlock
The thread waiting for the lock will go to sleep
2 、 dispatch_barrier_async
The concurrent queue passed in by this function must be created by itself through dispatch_queue_cretate
If you pass in a serial or a global concurrent queue, then this function is equivalent to the effect of the dispatch_async function
At this point, I believe you have a deeper understanding of "what are the hidden dangers of multithreading in iOS development?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.