Thread: Multithreading...

  1. #16
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    So my theory is a multithreaded server to handle connections from players. Currently lets make this a generalization... You would have a player class that handles the player's actions. I would also have something like a world class containing you guessed it the world... now if you have multiple players interacting with the world would it be better to lock the memory or something if someone is already interacting with something? Or should I make a while(function_locked) loop to check the status, but this thought seems it would block and that is the last thing I would want to do. I could see creating a thread from a thread, but it would only blip into existance and then back out probably loosing any efficency that would ever give me. I am thinking if two threads try to add into an array at the same time there would be problems. I would likely use vectors or arrays even a possible linked list from what I have read might be a very viable solution. Back to the problem "pushing" two projectiles onto a list of projectiles at the same time could damage a linked list if they were editing the same space. In an array I would probably get some garbage that would violate a check and be deleted and zeroed out I think a vector would have a similar reaction as with the array. So how do you prevent this, but still make sure the command gets through without tieing a thread up waiting on another thread to finish? Yes it would be a quick function so I could "probably" let it tie itself up and not have huge problems, but on the other hand I want to use threads so I am not waiting on something else to finish before I can do another task.

  2. #17
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I just read something that made me think back to this post and figured it would be relevant information...

    Sample _beginthreadex

    Use of the Win32 APIs CreateThread() and EndThread() directly is incompatible with the C Runtime (CRT) Library. In particular, use of CreateThread() and EndThread() directly can cause loss of memory that the CRT allocates. Instead, we must use either _beginthread() and _endthread() or _beginthreadex() and _endthreadex().

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Re: portability: use boost. Heck, always use Boost, unless what you need is part of the standard library or not part of boost. Boost.Thread is portable and C++ only, so no tricky casting or stuff.

    Basically,
    boost::thread thread(&my_function, param1, param2, param3, ...);

    And that's it.
    No need to have a specific function signature (except to recieve the parameters, of course). And it does not have to be a function. It can be a functor, or a class function, etc.
    Read more on boost's web site.

    As for modifying, in the beginning, take it easy. Multi-threading is hard.
    Follow these guidelines, and you will be fine:

    - When you try modify something, always lock.
    - When reading something, always lock.
    - After locking, do not try to lock something else.
    - Unlock as soon as you're done reading or writing. Now you may lock some other lock.
    Yes, it might mean threads waiting for access, but it greatly simplifies code. Boost also have locking mechanisms. Good stuff to read up on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

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