objective-c - 线程同步和异步的区别?IOS中如何实现多线程的同步?
PHP中文网
PHP中文网 2017-04-28 09:05:18
0
3
503

这个关于线程的问题怎么实现?

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
小葫芦

Let me tell you my own understanding. I don’t understand it deeply. Please correct me if I’m wrong.

Thread synchronization occurs under multi-threading. Multiple threads access the same resource at the same time. In order to ensure safe and efficient access, it is necessary to solve a series of problems caused by synchronous access.
For example, two people take apples from a basket. If one takes it and then lets the other take it, there will be no problem, but the efficiency will not be high. (totalTime = timeOfA + timeOfB)
If you let them take it at the same time, you take one and I take one, this will be more efficient, but there will be a problem. If both people grab an apple, who should get it? How many apples can there be at this time? In this way, when A takes the apple, B waits first, and waits until A gets the apple before B takes it. Or A can only take what's on A's side, and B can only take what's on B's side. This will solve the problem.
The most important method on iOS is to lock it during access and unlock it after the access is completed. As for what locks to add and how to lock them, a lot of information on the Internet is quite detailed.

为情所困

同步 异步 线程同步, it’s a different concept

Taking the example from the classmate upstairs

You take an apple, and then I take an apple. This is called 同步执行
你和我同时去拿苹果,这叫两个线程异步执行,这里为了实现异步,使用了多线程的手段,你和我各属于一个线程
然后就会出现问题了,如果你和我伸手拿了同一个苹果怎么办? 这个时候就需要你和我进行线程同步, for example: whoever sees the basket first locks the basket and lets the other person wait, and unlocks it after taking it

左手右手慢动作

Many people think that asynchronous means multi-threading. This is wrong. There is a difference between asynchronous and multi-threading.

Threads are codes, codes that the CPU has to run. Running two pieces of thread code together gives people a feeling of no blocking. But it's still code. The CPU is still waiting there.

Asynchronous is mainly for io, file io, network request io, you don’t want synchronous io to block the process, so there is an asynchronous method to improve the response capability. Asynchronous use requires the operating system to subscribe to the event delegation closure function, and more The same cannot be said for threads.

If you know a little about hardware, you can feel the interruption.

Take the apple everyone talks about as an example...
Asynchronous means that you tell the computer to get me an apple.
The computer will use Apple hardware to respond to this function. Does it have anything to do with the CPU? No more. It won't delay the CPU.
When Apple is requesting from the network, the one who gets Apple is the network card

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!