0

I am making a small 2d online game and I currently have drawing, character moving and socket handling all done in the main thread. I think that it would be more convenient to create a own thread for those things, especially for the socket handling.

It's my first multi threaded application so should I basically just make the threads with this? I read that I just use mutexes or criticalsections to prevent two threads from accessing the data at the same time, which should I use?

Samuli Lehtonen
  • 3,840
  • 5
  • 39
  • 49
  • What are you trying to ask? How to do threading? Or how to best handle data-sharing between threads? The confusion arises from the '?' after your statement of '...basically just make the threads with this?'. If it's about data-sharing, use Critical Sections as they don't go through Kernel mode and are faster than Mutexes. – Vite Falcon Jul 05 '13 at 19:29
  • 1
    Are you using C++11? (If so, you can use `std::thread` and friends to save you some grief and help with portability.) You should also read up on multithreading before you start, because a lot can go wrong in ways that are hard to debug! A critical section, btw, is an implementation of mutual exclusion. – Cameron Jul 05 '13 at 19:31

1 Answers1

1

Yes, you should create new threads with CreateThread method. Don't forget to create a message pump in each thread that is supposed to process messages (e.g. asynchronous socket handling).

As for mutexes vs. critical sections: critical sections would be more appropriate as you need to synchronize threads of only one process. For more details, see What is the difference between mutex and critical section? .

It's also convenient to use messages for communication between threads (see PostThreadMessage).

Community
  • 1
  • 1
nullptr
  • 11,008
  • 1
  • 23
  • 18