Wednesday, 11 December 2013

Threading

What is it?

Sometimes in games, it is very useful and effective to have a variety of sections of code running on one processor. But what if there was a way that programmers can have more than one paths of execution? What if there actually was a way that code could be run on different processors? This is the idea that threading brings. A thread is simply a path of execution. Single path execution run through single-threaded programs whereas multiple paths run through multi-threaded programs.





Single-threads are computed one at a time whereas multi-threads can all run at once. One thing these threads have in common is that both single and multi have a single core. Threads also have to be able to handle their own execution. On another note, they have two callbacks, "suspend" and "resume".

 There are two specific types of threading:
  • Kernel threads
  • User-Level threads

Kernel-Threading & User-level Threading

Kernel-threading is the lowest operating system. It handles the scheduling in the execution process. The thing is with kernel-threading is that they manage the operating system and maps the user-level threads. So in a way kernel can incorporate user-level threads too while being managed by the CPU. User-level threading operates in user-space. These user-level threads are processed using a One-to-One or Many-to-One format.




Why Use threads?

Using threads give more efficiently on single or multiple executions. They allow us to use multiple tasks at once like receiving and computing input.They also take advantage of multiple CPUs. Not to mention they are quite simple to program. Here is an example done in class.

Example
pthread_create(do Physics)

void do Physics()
{

while(1)
{
if (press)
pthread_cancel
}
}

Once the thread exits the loop dies.




No comments:

Post a Comment