I am designing a chat server in C that uses a thread-pool + event polling. When a read event is detected, a thread processes that message, but I’m not sure how to store it.
Right now I have a thread-safe hash map I designed that contains user structs that contain fields for a message queue of incoming messages to that user. So when a write event is detected, a thread will query the hash map (which maps usernames to user structs), dequeue all messages in that user’s message queue and use send() to write to that user’s socket. This approach is fine for private messaging, but if 10 people are in a group, then this poses a problem as my hash map has a read-write lock on every bucket, so each time someone sends a group message I’d have to lock/unlock 10 times for the bucket, and then again for the message queues. I was wondering if there is a better way to fix this design problem?
1