CS代写 Concurrent Threads

Concurrent Threads
Dr. Bystrov
School of Engineering Newcastle University
Concurrent Threads

Copyright By PowCoder代写 加微信 powcoder

Threads (underlined topics examined)
Concept Modelling Examples
Shared data
Condition variables Semaphores
Concurrent Threads

Read YoLinux Tutorial: POSIX thread (pthread) libraries http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
The POSIX threads standard API for C/C++. Spawning a concurrent process flow.
Most effective on multiprocessor systems;
gaining speed through parallel processing.
Less overhead than “forking” or spawning a new process
the system does not initialise a new system virtual memory space and environment
threads within a process share the same address space.
Concurrent Threads

Thread operations include
data management and process interaction.
Thread Basics
thread creation,
termination,
synchronisation (joins,blocking), scheduling,
A thread does not maintain a list of created threads, nor does it know the thread that created it.
pthread functions return “0” if OK.
Concurrent Threads

Threads in the same process share: address space;
process instructions;
most data;
open files (descriptors); signals and signal handlers; current working directory; user and group id.
Threads share
Exiting the process ends all its threads!
Concurrent Threads

Each thread has a unique: thread ID;
Threads don’t share
set of registers, stack pointer;
stack for local variables, return addresses; signal mask;
return value: errno .
Concurrent Threads

#include int pthread_create (
Thread forking
pthread_t * thread,
pthread_attr_t * attr,
void * (*start_routine)(void *),
void * arg);
On success, the identifier of the newly created thread is stored in the location pointed by the thread argument, and a 0 is returned. On error, a non-zero error code is returned.
Attributes (attr): detachable or joinable; scheduler control.
Concurrent Threads

#include int pthread_join(
Thread joining
pthread_t thread, void **retval);
pthread_join suspends the execution of the calling thread until the thread identified by th terminates, either by calling pthread_exit or by being cancelled.
The joined thread th must be in the joinable state: it must not have been detached.
pthread_join must be called once for each joinable thread created to avoid memory leaks.
At most one thread can wait for the termination of a given thread.
Concurrent Threads

Fork-join example
Concurrent Threads

Thread synchronisation
The threads library provides three synchronisation mechanisms (we focus on the first two):
join – Make a thread wait until others are completed (illustrated earlier).
mutex – Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
conditionvariables –datatypepthread_cond_t
Concurrent Threads

are used to prevent data inconsistencies due to race conditions.
A race condition often occurs when two or more threads need to perform operations on the same memory area, but the results of computations depends on the order in which these operations are performed.
Mutexes are used for serialising shared resources.
Anytime a global resource is accessed by more than one thread the resource should have a Mutex associated with it.
One can apply a mutex to protect a segment of memory (“critical region”) from other threads.
Concurrent Threads

Mutex example
Concurrent Threads

Experiments on fairness
Run two threads with the following thread function. Distinguish between them by passing them different parameters.
Output: 11111111112222222222 Uncomment the delay.
Output: 12121212121212121212
Concurrent Threads

Condition variables
A condition variable is used with the appropriate functions for waiting and later, process continuation.
A condition variable must always be associated with a mutex.
A deadlock when one thread is preparing to wait and another thread signals the condition. The thread will be perpetually waiting for a signal that is never sent.
Any mutex can be used, there is no explicit link between the mutex and the condition variable.
Concurrent Threads

Functions used with condition variables
Creating / Destroying:
pthread_cond_init
pthread_cond_t cond =
PTHREAD_COND_INITIALIZER;
pthread_cond_destroy
Waiting on condition:
pthread_cond_wait pthread_cond_timedwait – bloking time limit
Waking thread based on condition:
pthread_cond_signal
pthread_cond_broadcast – wake up all threads blocked by the condition variable.
Concurrent Threads

Condition wait example
Concurrent Threads

mutex_lock
mutex_lock
mutex_unlock
mutex_unlock
Condition wait model
* cond_signal
Concurrent Threads

Semaphore operations:
Semaphores
Built upon mutexes and condition variables.
An integer value is associated with a semaphore.
semaphore_init – create a semaphore with value 1
semaphore_up – increments the semaphore
semaphore_down – blocks if its value ≤ 0, decrements the semaphore value
semaphore_decrement – decrements without blocking
semaphore_destroy – destroys a semaphore
Concurrent Threads

Semaphore example
Concurrent Threads

Conclusions
Fork-join, choice-merge, arbitration when accessing common resource
Two types of dependency between threads
imposed by start-termination control imposed by data communication
Modelling concurrent computations with Petri nets Interesting effects – counting errors, fairness, deadlock
Concurrent Threads

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com