Turns out the segfaults were an unrelated issue, but yes, I think you'll need to add some locking for thread safety. For instance, say a broadcast message comes in on the websocket thread. You handle it in GvChat::eventBroadcast, doing send_to_char to everybody, but what if your main thread is also doing a send_to_char to the same player at the same time? 2 threads writing to the same memory at the same time is no bueno
.
Easiest way to handle it I think is to put the incoming messages in a queue that you periodically come and handle from the main thread. I thought you might have had that in mind since you added _receivedQueue (though unused). You use a mutex to lock access to the queue. When new message comes in, lock the mutex and add it to the queue. Somewhere in your main thread (game_loop) you call into a function that locks the mutex and processes all the queued messages. Anyway, that's the general idea and seems to work for me. However, this introduces a new wrinkle, which is that (at least in our game) the main thread sleeps when nobody is logged in, so after a while of not answering the heartbeat events, grapevine connection will drop. Probably makes most sense to close connection from our end before we go into the "no player" sleep, and reconnect once somebody logs in.
Just to note, there are some other bugs in there as well. For instance, doing gvtell with no arg crashes.