It is currently Tue Dec 12, 2017 6:36 pm




 Page 1 of 1 [ 3 posts ] 
Author Message
 Post subject: Multithreading
PostPosted: Sun Feb 10, 2008 10:14 pm 

Joined: Thu Oct 25, 2007 9:49 pm
Posts: 33
How exactly does multithreading work? and if your application is multithreaded, does it not function or cause overhead on a non-threaded processor?


Offline
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 8:35 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
There are two things. Multithreaded software, and Multi Core hardware.

Software: when you write a program and compile it, all your written instructions get converted into machine readable code. This code tells the CPU how to behave on each action, which memory registers to use etc. When you write multithreaded software what you are telling the compiler is that the following code can run in parallel rather in series. For example (in pseudocode)

main() {
   function1();
   function2();
}


in the non-multithreaded case, this code would run all the instruction in function1 first and then all the instructions in function 2 second.

if you make this program multithreaded and cause each function to spawn a new thread, that means that function 1 and 2 would appear to run both at the same time. Now on a single core processor you can't run two instructions at once. So what happens is the cpu breaks the code up into time slices and says function1 is allowed to run for so many micro seconds, then function2 is allowed to run, then back to function1 and so on.

That's how multithreaded software works.

Multicore hardware is similar but on the hardware level. Commands that come down from your program to the CPU get diverted into one of the many core's (CPU's) available by a multi core processor. This means that multiple instructions can run in parallel (at the same time) rather than splitting things up into time slices. In the above example, the multithreaded program would be able to stick one function on one processor, and the other function on the other processor and just go.

When doing any multithreaded programming you need to be careful with a few things because you can lock up your programs easily. For example if one thread wants data from the other thread that can be tricky. If you don't handle it right you can corrupt data or worse.

On my long list of things to do, I have a few VMK's I want to make that show how to do multithreaded programming.


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 5:36 pm 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
Marek, could you please tell that to my ICT teacher? 8)
He had to explain it all, but I and another of the more computer literate students had to keep correcting him (at one point I just gave up) when it came to multiprocessing, multitasking and multithreading.



Note that while switching between two processes may cause a loss of time, as the active part of your RAM will have to be redefined - every process has it's own RAM assigned. However, two threads of one process share their RAM, so the switching is only a matter of changing a single pointer, which is no significant loss. This sharing of RAM is also why it's so dangerous, though. Take the following two threads of one process:

function a()
{
    while (bool_almostAlways)
    {
         ....
         array[arraySize] = 'new1';
         arraySize++;
         ....
     }
}

function b()
function a()
{
    while (bool_almostAlwaysAsWell)
    {
         ....
         array[arraySize] = 'new2';
         arraySize++;
         ....
     }
}


Your operating system may switch between the two processes right between setting a new element in the array by a() and a increasing the count. Next b() gets control, just to overwrite the data that a() just wrote wrote to the array. What's worse, though, is that a bit later a() gets control back and increases arraySize by one, effectively just adding whatever happened to be in the right RAM location to the array, this can really be anything.
Likewise you start accessing RAM that is being used for other variables of your program, or even have it overflow into another program's RAM - a thing that no OS ever allows, so it will be translated into an immediate crash.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 3 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron