Thread: Multithreading

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Multithreading

    I did a few searches on here and got alot of info I need to look over some more just a quick question for a MUD/MMORPG setting should each character/connection have it's own thread? The last server I wrote for a game like this ran fine with a few people 1-10 but once more people started logging in it started getting bogged down maybe it was bad code in some areas or something just exploring different solutions before I settle on something that is going to take weeks if not months to implement and test.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Threads do not magically create CPU time for you to use, so if your normal code ran at 100% then it's pretty likely that a threaded answer would reach 100% sooner.

    Not only that, you need to be very careful about accessing shared memory objects (say one character talking to another). I could easily imagine you having a situation where one characer (in a thread) tried to talk to another character (in another thread). One character dies and the other then starts looking in free'd memory.

    Some people swear by threads - others just swear at them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Not that I thought they made more cpu cycles... I ment by using the priority my combat wouldn't be as bogged down as say moving or updating the client. As for one play talking to a dead player (or targeting a item/mob) that isn't there... Players respawn after death. The dead monster's destroyed item's memory is freed and all pointers to it would be removed thus no dangling pointers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://www.kuro5hin.org/story/2002/11/18/22112/860
    http://www.softpanorama.org/People/O...ads/sld001.htm

    Make of that what you will.
    For me, the amount of interaction you have between objects would make having a thread per object far too tricky to be effective. If you end up locking everything most of the time, all the 'benefit' will have been lost, but you'll still have all the problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Wouldn't it be alright to have a thread to monitor each connection? Not for movement and such just to look at the connection and see if it is live or dead and if there is a command waiting to be run? Also is it possible for two or more threads to access a single funtion at the same time i.e a global function? Will look at the links here soon these 12 hour days are killing me though.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you are using windows, you can use asynchronous sockets so all the threading is basically done for you. All you have to do is handle the messages in the message loop (FD_READ, FD_WRITE, etc)

    Here's a link to a short tutorial.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. multithreading in C++
    By manzoor in forum C++ Programming
    Replies: 19
    Last Post: 11-28-2008, 12:20 PM
  3. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 07:58 AM
  4. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  5. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM