Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Nust College of Eme Nismah Saleem

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

NUST COLLEGE OF EME Nismah Saleem

Threading in C++
April 16, 2015
Abstract
The purpose of this assignment was to make us familiar with the concept of threading in c++. We were asked to write a program in c++
and demonstrate the concept of threading in it. We had to write the
same program then without using threads to see the difference between
the outputs of the two pieces of codes.

Introduction

Threading or multithreading is a technique by which a single set of code can


be used by several processors at different stages of execution. Multithreading
is the ability of a program or an operating system process to manage its use
by more than one user at a time and to even manage multiple requests by the
same user without having to have multiple copies of the programming running
in the computer. Each user request for a program or system service (and here a
user can also be another program) is kept track of as a thread with a separate
identity. As programs work on behalf of the initial request for that thread and
are interrupted by other requests, the status of work on behalf of that thread is
kept track of until the work is completed.

Explaining Multithreading

Multithreading is a type of execution model that allows multiple threads to exist


within the context of a process such that they execute independently but share
their process resources. A thread maintains a list of information relevant to
its execution including the priority schedule, exception handlers, a set of CPU
registers, and stack state in the address space of its hosting process.
Multithreading is also known as threading. Threading can be useful in a
single-processor system by allowing the main execution thread to be responsive
to user input, while the additional worker thread can execute long-running tasks
that do not need user intervention in the background. Threading in a multiprocessor system results in true concurrent execution of threads across multiple
processors and is therefore faster. However, it requires more careful programming to avoid non-intuitive behavior such as racing conditions, deadlocks, etc.
2

Operating systems use threading in two ways:


Pre-emptive multithreading, in which the context switch is controlled by
the operating system. Context switching might be performed at an inappropriate time, Hence, a high priority thread could be indirectly preempted by a low priority thread.
Cooperative multithreading, in which context switching is controlled by
the thread. This could lead to problems, such as deadlocks, if a thread is
blocked waiting for a resource to become free.
The 32- and 64-bit versions of Windows use pre-emptive multithreading in which
the available processor time is shared such that all the threads get an equal
time slice and are serviced in a queue-based mode. During thread switching,
the context of a pre-empted thread is stored and reloaded in the next thread
in the queue. The time slice is so short that the running threads seem to be
executing in parallel.

Advantages of Threading
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 others 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. High unused computing resources

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.

Table 1: Creating Threads

An opaque attribute object that may be used to set thread attributes. You can specify a thread attribut
A single argument that may be passed to startr outine.Itmustbepassedbyref erenceasapointerc
The mileage thus varies; Intel claims up to 30 percent improvement with its HyperThreading technology,[1] while a synthetic program just performing a loop
of non-optimized dependent floating-point operations actually gains a 100 percent speed improvement when run in parallel. On the other hand, hand-tuned
assembly language programs using MMX or Altivec extensions and performing data pre-fetches (as a good video encoder might), do not suffer from cache
misses or idle computing resources. Such programs therefore do not benefit
from hardware multithreading and can indeed see degraded performance due to
contention for shared resources.
Hardware techniques used to support multithreading often parallel the software techniques used for computer multitasking of computer programs.
Thread scheduling is also a major problem in multithreading.

Implementation

A major area of research is the thread scheduler which must quickly choose
among the list of ready-to-run threads to execute next as well as maintain the
ready-to-run and stalled thread lists. An important sub-topic is the different
thread priority schemes that can be used by the scheduler. The thread scheduler might be implemented totally in software, totally in hardware, or as a
hardware/software combination.
Another area of research is what type of events should cause a thread switch
- cache misses, inter-thread communication, DMA completion, etc.
If the multithreading scheme replicates all software visible state, include
privileged control registers, TLBs, etc., then it enables virtual machines to be
created for each thread. This allows each thread to run its own operating system
on the same processor. On the other hand, if only user-mode state is saved, less
hardware is required which would allow for more threads to be active at one
time for the same die-area/cost.

Creating Threads

You might also like