最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

lazy initialization - How does Rust protect access on a LazyLock? - Stack Overflow

programmeradmin4浏览0评论

I'm guessing the most efficient way might be that when a thread enters the function it:

  1. Either locks a mutex or atomically writes a variable to signal it's locked.

  2. Checks whether it's been initialised, if not it initialises the value.

  3. Either unlocks the mutex or atomically writes to the signal variable.

That would seem to me that each access is at minimum two atomic variable read/writes?

With a LazyCell, which works only for single thread uses and is used inside functions I guess one single atomic variable read/write can be used, something like if bHasBeenInitialised.

I mean the "Lock" part in the LazyLock name means that it's synchronised, so essentially it's the same as a Mutex. The fact that it's a fact just means that you don't have to have a second mutex/atomic to check initialisation, so a LazyLock is essentially like accessing a Mutex< > variable, right?

Also, I'm assuming the lazy_static and the other variations work the same?

发布评论

评论列表(0)

  1. 暂无评论