Thread: Multithreading

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738

    Multithreading

    Yesterday i built a little test program that created 10 threads, where in each i called a function which run a quite big loop. After multiple testings i realised that when running each function in a different thread, and waiting for all of them to finish of course, that it would terminate at almost half the time as when running the function 10 times in the same process. How's that possible? I know it can be done in multiprocessor system, but mine is Intel Core 2 Duo ( Two cores but still one processor ). Am i missing something? Can multicored processor run faster when using multithreading?
    Devoted my life to programming...

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    It can, yes - additionally, it depends on what your function's doing. If it's making some system call that might block, it will be much more efficient to make it ten times concurrently than it will be to make it ten times consecutively. If the call is for blocking I/O, for example, where most of the time will be spent waiting for hardware to do whatever it's doing, this may well be the case.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok, but can i say that those 10 thread, or some of them, run at exactly the same time?
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Sipher View Post
    Ok, but can i say that those 10 thread, or some of them, run at exactly the same time?
    Up to two of them may do, on a dual-core processor, but that would be an accident and not generally something you'd care to know about. They only run "at the same time" in a conceptual sense - it's a useful way to think about them at a certain level of abstraction (e.g. when you want to make ten system calls concurrently), but in actual fact they probably won't literally run at the same time.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    This may be a stupid question, but it has happened to me: Are you sure the function is thread safe? If it's not, the concurrency could mess it up, causing some threads' execution of the function to prematurely return.

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. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM