I encountered the following situation: I have two modules, TokenService
and Wifi
which are initialized from main()
. The modules themselves don't know of the existence of each other and function completely independent, yet they need access to the same global resource which in this case is the nonvolatile storage flash, or short nvs
, which must be initialized for the module to function correctly. The flash initialization function however is not idempotent, this means I can't call it twice, yet it should be called at least once when the first of either module goes through initialization. Here's a visualization of the thing:
I have not yet found a suitable elegant way to accomplish this. My current approach is to defer the initialization of the global resource back to the user of the module via a lambda, but it's not satisfying. I imagine that this is not the first time anyone has encountered this problem so I'm looking for the idiomatic approach to solve this.