Thursday, 23 May 2013

Threads and Multithreading

Threads are just a light-weight process. It's actually a process but it runs under a process, literally a subsets of a process, where in this process, threads can share resources like memory. Threads in this subject is distinct from multi-core systems, which deals with instructions that are sent in the hardware resources like instruction set pass from USB to processor, stuff like that, in which multi-core can empower an OS to implement multi-threading. So multi-core is more of dealing with central processing units to execute program instructions.

Multithreading aims to increase utilization of a single ore by using thread-level as well as instruction-level parallelism. As the two (multithreading in cores and threads in OS) techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores.


Advantages
Some advantages include:
  • If a thread gets a lot of cache misses, the other thread(s) can continue, taking advantage of the unused computing resources, which thus can lead to faster overall execution, as these resources would have been idle if only a single thread was executed.
  • If a thread cannot use all the computing resources of the CPU (because instructions depend on each other's result), running another thread can avoid leaving these idle.
  • If several threads work on the same set of data, they can actually share their cache, leading to better cache usage or synchronization on its values.
Disadvantages
Some criticisms of multithreading include:
  • Multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers (TLBs).
  • Execution times of a single thread are not improved but can be degraded, even when only one thread is executing. This is due to slower frequencies and/or additional pipeline stages that are necessary to accommodate thread-switching hardware.
  • Hardware support for multithreading is more visible to software, thus requiring more changes to both application programs and operating systems than multiprocessing.

Threads
========

Pre-emptive multitasking - temporarily interrupting a task being carried by a computer system without requiring cooperation and with the intention of resuming the task at a later time.


Context switch - This is what is happening when doing a pre-emptive multitasking. It is normally carried out by a privileged task or a part of the system known as a preemptive scheduler.


Pre-emptive multitasking is carried out by privileged task.


Race conditions - where something like data are bumping. Like for example, two variables might get into racing who comes first to get the change the state of such value where these two variables are in different threads/task.

There are two kinds of race conditons:

critical race occurs when the order in which internal variables are changed determines the eventual state that the state machine will end up in.

non-critical race occurs when the order in which internal variables are changed does not alter the eventual state. In other words, a non-critical race occurs when moving to a desired state means that more than one internal state variable must be changed at once, but no matter in what order these internal state variables change, the resultant state will be the same.

No comments:

Post a Comment