1

I'am developing currently game server, and currently I have one design problem.

Game server have World class (it's singleton) and when i changed from single I/O service to I/O service per core, I think i should use mutex in my World class. Because packet handler from client sometimes perform operations using World's functions.

Is there way to avoid mutex in that singleton ?

  • Well, don't use code which breaks things if used from multiple threads? :) – user3159253 Apr 19 '14 at 10:54
  • Actually, it's a good practice to instantiate your singleton before you enter multithread mode, somewhere near the start of the application. And then you should use locks where they're needed indeed. W/o knowledge of your service logic it's hard to make more precise suggestions. – user3159253 Apr 19 '14 at 10:57

1 Answers1

0

You can use the Active Object pattern.

So make the World implementation an active object, in effect running all operations on its own thread.

A sample of the Active Object pattern with Boost Asio is here: boost::asio and Active Object

Whether it is (performance-wise) appropriate for you to make World an active object, I cannot tell. I have a feeling you should /simply/ not access World from many threads, instead only running computation-intensive stuff in the background that you don't need locks to coordinate, or where the locking overhead isn't significant.

In my comment here I showed an alternative approach to dispatching jobs with a lock-free queue implementation.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633