What is a synchronous function? 什么是同步函数?

What is a synchronous function? 什么是同步函数?

温馨提示:本文最后更新于2025-02-07 10:15:52,某些文章具有时效性,若有错误或已失效,请在下方留言

By default, all Swift functions are synchronous, but what does that mean? A synchronous function is one that executes all its work in a simple, straight line on a single thread. Although the function itself can interact with other threads – it can request that work happens elsewhere, for example – the function itself always runs on a single thread.
默认情况下,所有 Swift 函数都是同步的,但这是什么意思呢?同步函数是在单个线程上以简单的直线执行其所有工作的函数。尽管函数本身可以与其他线程交互(例如,它可以请求工作发生在其他位置),但函数本身始终在单个线程上运行。

So, code like this represents a series of synchronous functions
因此,像这样的代码代表了一系列的同步函数

func functionA() {
    var counter = 0
    functionB()
    // etc
}

func functionB() {
    functionC()
    // etc
}

func functionC() {
    var counter = 0
    // etc    
}

This has a number of advantages, not least that synchronous functions are very easy to think about: when you call function A, it will carry on working until all its work is done, then return a value. As part of that work, function A calls function B, which perhaps functions C, D, and E as well, and it doesn’t matter – they all will execute on the same thread, and run one by one until the work completes.
这有很多优点,尤其是同步函数很容易考虑:当你调用函数 A 时,它将继续工作,直到所有工作完成,然后返回一个值。作为该工作的一部分,函数 A 调用函数 B,函数 B 可能也调用函数 C、D 和 E,这并不重要 – 它们都将在同一个线程上执行,并逐个运行,直到工作完成。

Internally this is handled as a function stack: whenever one function calls another, the system creates what’s called a stack frame to store all the data required for that new function – that’s things like its local variables, for example. The stack frame for function A will store its local variable, for example.xxxx
在内部,这是作为函数堆栈处理的:每当一个函数调用另一个函数时,系统都会创建一个所谓的堆栈帧来存储该新函数所需的所有数据——例如,它的局部变量之类的东西。函数 A 的堆栈帧将存储其局部变量,例如 .xxxx

That new stack frame gets pushed on top of the previous one, like a stack of Lego bricks, and if that function calls a third function then another stack frame is created and added above the others. Eventually the functions finish, and their stack frame is removed and destroyed in a process we call popping, and control goes back to whichever function the code was called from.
该新堆栈帧被推送到前一个堆栈帧的顶部,就像一堆乐高积木一样,如果该函数调用第三个函数,则会创建另一个堆栈帧并添加到其他堆栈帧的上方。最终,函数完成,它们的堆栈帧在我们称为 popping 的进程中删除并销毁,控制权返回到调用代码的函数。

This approach means that both function A and C have a variable called counter, but they don’t clash – Swift tracks them independently.
这种方法意味着函数 A 和 C 都有一个名为 counter 的变量,但它们不会发生冲突 – Swift 会独立跟踪它们。

Synchronous functions have an important downside, which is that they are blocking. If function A calls function B and needs to know what its return value is, then function A must wait for function B to finish before it can continue doing its work.
同步函数有一个重要的缺点,即它们会阻塞。如果函数 A 调用函数 B 并且需要知道其返回值是什么,则函数 A 必须等待函数 B 完成,然后才能继续执行其工作。

This sounds self-evident, I know, but blocking code is problematic because now you’ve blocked a whole thread – it might be sitting around for a second or more waiting for some data to return.
我知道这听起来不言而喻,但阻塞代码是有问题的,因为现在您已经阻塞了整个线程 – 它可能会等待一秒钟或更长时间,等待一些数据返回。

You’re probably thinking that waiting for a second is nothing at all, but in computing terms that’s an ice age! Keep in mind that the Neural Engine in Apple’s M4 CPU – just one part of the chip that powers a modern Mac – is capable of doing 38 trillion things per second, so if you block a thread for even a second that’s 38,000,000,000,000 things you could have done but didn’t.
您可能在想,等待一秒钟根本不算什么,但从计算的角度来看,这就是一个冰河时代!请记住,Apple M4 CPU 中的神经引擎(只是为现代 Mac 提供动力的芯片的一部分)每秒能够执行 38 万亿次作,因此,即使你阻塞一个线程哪怕一秒钟,那也是 38,000,000,000,000 件你可以做但没有做的事情。

One solution is to say “Well, if we’ve got a thread that’s currently blocked, we should just launch a new thread.” That’s definitely a solution, but it’s also a path towards thread explosion where the system creates so many threads it becomes a burden.
一种解决方案是说“好吧,如果我们有一个当前被阻止的线程,我们应该启动一个新线程。这绝对是一个解决方案,但这也是一条通往线程爆炸的道路,系统会创建太多线程,从而成为一种负担。

So, although synchronous functions are easy to think about and work with, they aren’t very efficient for certain kinds of tasks. To make our code more flexible and more efficient, it’s possible to create asynchronous functions instead.
因此,尽管同步函数很容易考虑和使用,但它们对于某些类型的任务并不是很有效。为了使我们的代码更灵活、更高效,可以改为创建异步函数。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容