Post AJGViOmQZjGLPgnUoq by mburakov@mastodon.social
 (DIR) More posts by mburakov@mastodon.social
 (DIR) Post #AJFbo4PvbOJcYzOvU8 by wolf480pl@mstdn.io
       2022-05-08T18:36:57Z
       
       0 likes, 0 repeats
       
       tfw. I have a concurrent map A -> List<B>but sometimes it'd be nice to go the other way around, so map B -> List<A>but if they're separate concurrent maps, how do you synchronize this?would be nicer to have just a set of tuplesbut then how do you synchronize that? a global lock?or I could just use a postgresexcept this is small ephemeral state, and I want it to be fast
       
 (DIR) Post #AJFbrBEEjYdmYcPoYa by wolf480pl@mstdn.io
       2022-05-08T18:37:31Z
       
       0 likes, 0 repeats
       
       I wonder how Linux kernel deals with similar situations
       
 (DIR) Post #AJFcBhM10Af9z5pzHM by wolf480pl@mstdn.io
       2022-05-08T18:40:50Z
       
       0 likes, 0 repeats
       
       Why did I start writing this stuff in a multithreaded way in the first place?Oh, because I wanted feedback on whether the protocol design is hard to implement in a thread-safe way...
       
 (DIR) Post #AJFfasaafOx9voM0GG by michcio@raru.re
       2022-05-08T19:19:01Z
       
       0 likes, 0 repeats
       
       @wolf480pl sqlite?
       
 (DIR) Post #AJFfgUVf6fmozIiytU by wolf480pl@mstdn.io
       2022-05-08T19:20:24Z
       
       0 likes, 0 repeats
       
       @michcio more like software transactional memory
       
 (DIR) Post #AJFlmtgV1HZSWpEPUO by Mek101@mstdn.io
       2022-05-08T20:28:47Z
       
       0 likes, 0 repeats
       
       @wolf480pl > but then how do you synchronize that? a global lock?A R/W lock?
       
 (DIR) Post #AJFlrCt9YrL7FOCmmW by wolf480pl@mstdn.io
       2022-05-08T20:29:34Z
       
       0 likes, 0 repeats
       
       @Mek101 so only one thread can modify the whole structure at the time? sounds lame
       
 (DIR) Post #AJFls6UcpEzfhX9zKC by wolf480pl@mstdn.io
       2022-05-08T20:29:43Z
       
       0 likes, 0 repeats
       
       @Mek101 at a* time
       
 (DIR) Post #AJFmKAIOHT5JbSompM by DickForgetsHeRunsServices@paypig.org
       2022-05-08T20:34:50Z
       
       0 likes, 0 repeats
       
       @wolf480pl yeah use a semaphore.Why use a map I would think vectors might be easier for you to use if you're trying to do more than simple iteration?I dunno your actual implementation, though so my mental interpretation could be incredibly incorrect.
       
 (DIR) Post #AJFmREimE0RXM8X2lU by DickForgetsHeRunsServices@paypig.org
       2022-05-08T20:36:07Z
       
       0 likes, 0 repeats
       
       @wolf480pl I can tell you driver level handling; which is usually spinlocks with timeouts waiting for irq's to fire.
       
 (DIR) Post #AJFoKDUiaTnrQsjjJw by Mek101@mstdn.io
       2022-05-08T20:57:13Z
       
       0 likes, 0 repeats
       
       @wolf480pl Does ypur code have that much contention?
       
 (DIR) Post #AJFqq7PGxOobj6WroG by wolf480pl@mstdn.io
       2022-05-08T21:25:23Z
       
       0 likes, 0 repeats
       
       @Mek101 idk haven't benchmarked yet, but I'm a perfectionist and this is a side project
       
 (DIR) Post #AJFroFIQkiSFZS02im by wolf480pl@mstdn.io
       2022-05-08T21:36:16Z
       
       0 likes, 0 repeats
       
       @DickForgetsHeRunsServices I assume by vectors you mean C++ vectors, not Java Vectors (those are horrible, use ArrayList instead).Vectors wouldn't work in my usecase / would make everything O(n²). It's a many-to-many relationship of clients and channels (like in IRC), which I need to be able to quickly look up both by client and by channel.I'd rather avoid touching low-level synchronization primitives (like semaphores or compare-and-swap) directly.1/
       
 (DIR) Post #AJFs1lp18ORiDBgtrE by wolf480pl@mstdn.io
       2022-05-08T21:38:43Z
       
       0 likes, 0 repeats
       
       @DickForgetsHeRunsServices wrt. parallelism, ideally I'd use something lock-free that uses compare-and-swap under the hood, but failing that, some per client + per channel locking scheme would do.A global RW lock for the whole data structure sounds like a copout.A global semaphore for the whole data structure would make the whole thing effectively single-threaded.
       
 (DIR) Post #AJFtayPuOkV1MdIRyi by Mek101@mstdn.io
       2022-05-08T21:56:16Z
       
       0 likes, 0 repeats
       
       @wolf480pl Then, maybe you can try and break up the map into N chunks with N = threads?
       
 (DIR) Post #AJFtrrpI9fudqYE0P2 by wolf480pl@mstdn.io
       2022-05-08T21:59:20Z
       
       0 likes, 0 repeats
       
       @Mek101 yeah it's called ConcurrentHashMap
       
 (DIR) Post #AJFuqSc4VmgJJVvzto by wolf480pl@mstdn.io
       2022-05-08T22:10:16Z
       
       0 likes, 0 repeats
       
       @Mek101 actually I think concurrenthashmap in Java is even sneakier, using CAS to have lock-free reads.Anyway, my problem is, I kinda need two keys.Maybe having two ConcutrentHashMaps, one in each direction, will work after all, but it'll require some thinking.
       
 (DIR) Post #AJGViOmQZjGLPgnUoq by mburakov@mastodon.social
       2022-05-09T05:03:23Z
       
       0 likes, 0 repeats
       
       @wolf480pl you mentioned it’s small ephemeral state. So I’d start with evaluating a performance impact from a global lock in a real scenario. Maybe it is a way to go…
       
 (DIR) Post #AJGcYcdetUR7B6ysme by DickForgetsHeRunsServices@paypig.org
       2022-05-09T06:20:05Z
       
       0 likes, 0 repeats
       
       @wolf480pl there's always a lock.Yeah, what I had in my mind was quite different if you're looking at an O that big lol.I would just deal with the low level stuff if you don't wanna migrate everything to something else's responsibility :PIt's not that hard to maintain.