• 请不要在回答技术问题时复制粘贴 AI 生成的内容
James369
V2EX  ›  程序员

协程在使用中应注意什么,是否会出现对变量循环引用?

  •  
  •   James369 · May 21, 2023 · 1566 views
    This topic created in 1115 days ago, the information mentioned may be changed or developed.

    用协程来写异步调用,代码确实好看,不过还是担心驾驭不了。

    对于变量的循环引用,分 2 种情况:

    1. 如果几个协程工作在同一个线程中,是否会出现循环引用?
    2. 如果几个协程工作在不同的线程中,是否会出现循环引用?
    3 replies    2023-05-21 16:44:17 +08:00
    Leviathann
        1
    Leviathann  
       May 21, 2023
    循环引用,有什么问题
    rust ?
    James369
        2
    James369  
    OP
       May 21, 2023
    @Leviathann
    就是在 swift 中,异步的回调闭包的使用中,如果对象对闭包的强引用 和 闭包中对对象的强引用(此处一般要 weak 进行弱化,不然可能构成循环引用),导致内存无法释放的问题。
    但是,如果用了协程来进行调用时,我不确定闭包以什么方式工作,所以不确定是否也存在这样的问题?
    neoblackcap
        3
    neoblackcap  
       May 21, 2023
    @James369 go 的垃圾回收是 tracing gc ,会 stop the world 然后扫描内存,该类型垃圾回收算法没有环形引用导致无法进行内存回收的问题。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1097 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 23:00 · PVG 07:00 · LAX 16:00 · JFK 19:00
    ♥ Do have faith in what you're doing.