Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: running 2 instructions simultaneously..

  1. #1
    Junior Member
    Join Date
    Sep 2005
    Posts
    10

    running 2 instructions simultaneously..

    hi..i m really stuck up on this 1..
    how do u run 2 insrtuctions in a program at once?i mean suppose in a shooting game in a console application the computer waits for our input while the target keeps moving..thus two instructions r happening simultaneously..
    plz help..

  2. #2
    Banned
    Join Date
    Jul 2005
    Posts
    511
    Which programming language?

    Search for threads in the helpfiles for your preferred language. In C/C++ you can use the fork statement. The Windows API has something called CreateThread and Delphi has a TThread component that wraps around this API function.

    Be aware that if your system has only one processor then it won't execute two instructions at the same time. (The processor can handle only one instruction every time.) But by using multiple threads, long actions will appear to be executing at the same time.

    The use of multiple threads in your application does increase the risks of bugs in your code, some of which that can be real nasty. The most common error is when two threads are writing data to the same memory location. Or one is reading while the other is writing to some memlory location. This results in corrupt data that can crash your application.

  3. #3
    Junior Member
    Join Date
    Sep 2005
    Posts
    10
    where do i get the help files?i cudnt find them..
    plz explain the fork statement with an small example..that will be a real help..
    also dont all the action games(like racing,shooting,strategy) work on the multiple-instruction- execution principle..

  4. #4
    Junior Member
    Join Date
    Sep 2005
    Posts
    10
    sorry i forgot to tell..i m using c++(dev c++)..

  5. #5
    Banned
    Join Date
    Jul 2005
    Posts
    511
    There should be some helpfiles with Dev C++ but I'm unfamiliar with Dev C++ so I don't know where they are. Sorry.
    There's a Wiki about the Fork command at http://en.wikipedia.org/wiki/Fork_%2...ting_system%29 where you can see a C++ example of this statement, with some additional information.

    About game development, you don't need to use multiple threads unless you have to wait for some input device all the time. But in general, the keyboard, the mouse and other devices just send messages to your application. Your main application would just be running a message loop, receiving incoming messages and taking actions based on those messages. And when there are no new messages it has time for some background actions. Using multiple threads tend to make applications a bit more complicated so many developers try to avoid them.

    Dev C++ also has a Wiki at http://en.wikipedia.org/wiki/Dev-C_plus_plus and I'd advise you use both Wili links to search a bit further. And maybe find a good book about C++ to read that handles these more complex issues in C++.

  6. #6
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Erm, fork doesn't create threads, fork creates a new process. You'd be better off looking at pthreads or an environment specific threading model if you are doing c/c++.

    Google will return plenty of references for pthreads.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  7. #7
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    You may want to look into reading this book - http://www.rudyrucker.com/computergames/
    and downloading the example framework if you want to see one way of implementing your example.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  8. #8
    Banned
    Join Date
    Jul 2005
    Posts
    511
    Originally posted here by Juridian
    Erm, fork doesn't create threads, fork creates a new process. You'd be better off looking at pthreads or an environment specific threading model if you are doing c/c++.
    I never said fork creates a new thread. But it is a way to run two sets of instructions at the same time. (So why someone toild me in private that I was wrong is beyong my comprehension.) But maybe I didn't explain fully.

    The problem with C++ is that the language standard has no threading support. This because threading mechanisms are pretty dependant of the operating system underneath the code while standard C++ has been created to be platform-independant. I'm not too sure but I think I heard that Unix/Linux in the past did not even support multiple threads within a single process so the fork was the only solution. (Some more information about the history of threads in Linux can be found here.) Thus, many older Unix applications had to use fork if they want to run multiple instructions at the same time. This was very common for TCP/IP server application where every connection would generate a new process on the server.

    When programming on Windows with a C++ compiler you would use the CreateThread Windows API, of course. And O'Reilly has an interesting book about that called "Win32 multithreaded programming", ISBN 1-56592-296-4.

    I should also mention the threads Wiki but since the fork Wiki refers to it, I didn't bother to add it before. This article also mentions that "modern" operating systems (after 1995) do support multiprocessing, multithreading and even multifibers.

  9. #9
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    That's all well and good, but it doesn't really work for his example or even really do what he's asking. So basically, you ARE wrong. Forking out the process creates a new process, you'd have to work out the details of interprocess communication to get them to cooperate with anything....and you generally don't fork things out on a game console or a client side game.

    You may build your multiplayer online game architecture to fork out your game server process for specific missions, games, etc, but that is a bigger architecture question than what he presented.

    As I said before, look at your os specific solutions as well as the cross platform solutions such as pthreads.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  10. #10
    Banned
    Join Date
    Jul 2005
    Posts
    511
    If we're considering the game example, you could even wonder if he really needs multiple threads. It can all be handled from a single thread if you use a messageloop with appropiate messages from the keyboard, mouse and other devices. Keyboard/mouse events combined with a timer for the movements for the target can already make everything appear as running at the same time while it's all done from a single thread. The advantage of a messageloop is that you don't have to worry about multi-threading issues either.

    It's not just the OS requirements that are important, btw. The game itself might prefer a certain setup over others. Forking the game could have as advantage that the game engine can just continue to run even if the player-control engine crashes. All it would have to do is create a new player-control process when it discovers it's gone. By forking a process you can also avoid some thread-related issues like synchronising access to memory.
    The drawback is of course that it does generate a new process and thus you'll need inter-process techniques to communicate between the processes. This can be done with shared memory, named pipes, mailslots or just simple TCP/IP. Or even with messages being send between processes.

    But these techniques do depend heavily on the operating system beneath it.

    And tomalex, what operating system are you using? Dev C++ runs on both Windows and Linux so if we knew more, we might provide more specific help here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •