multithreading

This is a discussion on multithreading within the C++ Programming forums, part of the General Programming Boards category; How does multithreading work? I know it doesn't actually send two things to the processer at once... it probably just ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    multithreading

    How does multithreading work? I know it doesn't actually send two things to the processer at once... it probably just keeps switching between them.

    My reasoning is I want to do mouse input, but I want it to check without pausing for input.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Yep, multithreading just simulates simultaneous operation by breaking each task up into manageable chunks and switching between those chunks until both are finished.

    >My reasoning is I want to do mouse input, but I want it to check without pausing for input.
    Use threads or spawn another process if that feature is available to you.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    I can't tell you how to do multithreading in C++ <i could in delphi> but i can explain the theroy of it

    When your program starts it creates a main thread. This thread contains the code that is run threw the processor.
    Now in a multitasking enviorment <windows 95 and beyond> the OS uses a sheduler which uses thread priority among other things to cycle difrent threads threw the processor at a rate of about 20 a second
    So what you do is basicly create another threw within your program, assign it a priority <higher means its run at a higher importance IE more processor time>

    Follow me?

    So you have a thread thats checking for mouse input. Its runing almost at the same time that your main thread's code is running but theres a slight delay.
    You can have it so that lets say when the mouse moves it fires off a message to the main thread and then that starts the main threads reaction to the event.
    Multithread has its advantages, but it can be precarious as you need to use certain commands to allow communication between your child thread and Main thread.


    As an example of multithreading i give you this

    I recently wrote a small chat Server for fun. What i had it do was this.
    Upon a connection to server it created a thread for the clien that connects. After thread was established i had it enter Two things into an Array of Records. Firstly the Username logged on with, secondly a Pointer to the thread for that user. i did this for a simple reason. When using port blocking < a method for ocmmunication over internet> the thread of the connection awaits a responce from the connection beforer it does much of anything, such as user interface updates and the like, so having each connection in a difrent thread allowed me to not freeze my interface. Now thats not a great example of when to use multithread but it shows how it can be usefull. Another example could be lets say you are writing a piece of software that does multi tier datbase operations. users log in , a thread is created, that way for every user their Commands are processed sooner then if everyone had to wait in line for processor time < think of it like 3 people are Queyring the databse, on a non threaded program the first query would complete then the second then the third, in a multi threaded program the first starts, the first is pause d my OS, the second Starts, Second is poaused first resumes, first is pauses third starts.... you get the idea...
    Now lets say sudenly your CEO log in, you can give that thread a very high priority over others, that way his stuff gets done sooner then the rest, So he'll go Hmm what a nice fast server he's <or she's> written, lets give this fine coder a raise!

    hope you get the idea

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    thanks

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    No prob wish i knew code for MTing in c++ :S

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >No prob wish i knew code for MTing in c++ :S
    It depends on what implementation of threads you use. pthreads can be used for Unix systems (check the man pages), or you can do it with Windows.
    p.s. What the alphabet would look like without q and r.

  7. #7
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Forks and threads sorry i assumed windows
    <i'm ever so tierd heh>

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    For creating and manipulating threads you can start with the simple things, like the high level libraries that encapsulate all the heavy code...
    SDL works with threads, for example... www.libsdl.org
    MFC is still to "advanced" for me, so I use SDL
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    boost provides some threading classes, and AFAIK there are implementations for both POSIX-based and Windows systems. So for your portable multithreading, go there!

    If your system has multiple CPUs (and the OS to support them) several threads actually can run really at the same time (each on one of the processors).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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, 08: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, 08:58 AM
  4. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 04:53 AM
  5. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21