Unsafely share memory between threads (single sender, multiple receivers)

That is achievable. Just be careful not to assume bounds on the skew between threads, since it’s possible for one thread to load a value, increment it, and then be preempted, resulting in an indefinite delay before the result is stored back, during which other threads may have repeatedly modified the value.

Based on your suggestions I posted my implementation-draft under Code review: array-based shared memory queue for discussion.

I just noticed that UnsafeCell is !Sync so I cannot use it in multiple threads at all, right?

By itself an UnsafeCell shouldn’t be used by multiple threads at the same time, but in your case you should impl Sync for Producer, the compiler is not doing any promise, you are. Like a Mutex, you tell the compiler that you handle the sync part manually.
You might want restrictions on T also.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.