Hey all. I'm working on a simple Key Value LSM tree in Nim as an exercise, and I'm running into some issues. I am looking to make it so that an instantiating an instance of my store will also create a background thread that will invoke its compaction logic on a set interval, but I also want to make sure that if the object gets garbage collected, the thread will recognize this and terminate; in other languages I would use something like a Weak Reference.
Here's an arbitrary example (https://github.com/eileen-code4fun/LSM-Tree/blob/4f9224f96750ebdcebdddbc7ff7e2fb955b98b51/lsmt.go#L27) I found of a toy KV LSM tree in Go- this line would start a goroutine (coroutine) for every LSM Tree object. Here's part of my code, where I try to implement weakrefs: https://gist.github.com/MattAlp/e9b941584188db86d508a12614f3db78. However, this seems to cause a crash (https://i.imgur.com/GmrlqXU.png). Where do I begin implementing this or correcting my implementation?
First thing to confirm is that you're using orc or arc if compiling with Nim 1.x, or that you're using Nim 2.0. Not sure what your compiler version and compile command looks like.
Second, I see some ref objects being referenced in multiple threads which is generally not doable. The ref count is not atomic so it cannot safely be used in this way. If you want a shared pointer type you may find https://github.com/nim-lang/threading/blob/master/threading/smartptrs.nim helpful or you'll want to use non-GC memory (manually managed).