IRTG 2007 Linux programming: THREADS IRTG Bergen, March 2007 - PowerPoint PPT Presentation

Loading...

PPT – IRTG 2007 Linux programming: THREADS IRTG Bergen, March 2007 PowerPoint presentation | free to download - id: 9d5c2-MzQ0N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

IRTG 2007 Linux programming: THREADS IRTG Bergen, March 2007

Description:

THREADS (IRTG Bergen, March 2007) Normal text - click to edit ... Threads synchronisation, interruption (POSIX) ... Threads concurrency problems. Solution: ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 20
Provided by: SB793
Learn more at: http://web.ift.uib.no
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: IRTG 2007 Linux programming: THREADS IRTG Bergen, March 2007


1
IRTG 2007 Linux programmingTHREADS(IRTG
Bergen, March 2007)
2
TOC
  • Threads overview (POSIX)
  • thread creation, thread attributes
  • synchronisation, interruption
  • termination
  • concurrency (problems and solution)
  • other packages (ACE - framework)

3
Threads overview
  • What are threads?
  • ? running path of execution in (quasi) parallel
  • ? used for watchdogs, timeout controls,
    application monitoring
  • ? acceleration of application in multi core
    systems
  • ? usage of idle times for other tasks of the
    same application

4
Threads overview
  • Why threads? Couldnt it be done with fork() as
    well?
  • Yes, but
  • ? less intuitive
  • ? more complicated in interprocess
    communication (pipes, shared memory)
  • ? more overhead in context change (switch
    between both process in CPU)

5
Threads overview
main
Process 1
Process 2
thread 1
fork()
pthread_create()
communication over pipes, semaphores, shared
memory, sockets
communication via variables in same user space,
conditions, (thread-) functions
can terminate whole process
6
Threads creation (POSIX)
  • Nearly all Linux distributions have thread
    frameworks
  • ? e.g. POSIX (widely used standard in thread
    programming), called pthread.
  • Include of required header file
  • ? include ltpthread.hgt
  • Compilation against Pthread library
  • ? gcc lpthread ltsourcegt
  • Debugging tools like gdb or ddd support threads
    as well.

7
Threads creation (POSIX)
  • Before creating a thread
  • write or at least declare a tread function
  • declare a thread handle (data type pthread_t)
  • and prepare the attribute of this thread
  • data type for the attribute pthread_attr_t
  • detach state (PTHREAD_CREATE_JOINABLE,
    PTHREAD_CREATE_DETACHED)
  • scheduling policy and schedule parameters
  • stack size (size in bytes which is reserved for
    this thread in the stack)

8
Threads creation (POSIX)
  • Example
  • void thread_func(void arg)
  • // do something
  • int main()
  • pthread_t t_handle
  • pthread_attr_t attr
  • int status
  • status pthread_attr_init(attr)
  • status pthread_attr_setdetachstate(attr,
    PTHREAD_CREATE_DETACHED)
  • status pthread_create(t_handle, attr,
    (void) thread_func, NULL)
  • status pthread_attr_destroy(attr)
  • // do something else

9
Threads creation (POSIX)
  • Passing of parameters to thread (-function)
  • pthread_create(handle, attr, func, arg)
  • Parameters are handed over to thread function
  • void func(void arg)
  • // use arg and do something
  • // e.g. int num (int) arg

10
Threads creation (POSIX)
  • Access to application data inside thread
    function
  • ? thread function has same access to application
    resources (data, functions) like calling thread
    (main function)
  • ? global variables or pointers (bad ?
    concurrency problems, if not protected)
  • ? dedicated access functions (good, if
    concurrency protection implemented)
  • ? called functions are duplicates (both, main
    and thread func, can call the same function at
    the same time, but they are not working on the
    same stack area)

11
Threads synchronisation, interruption (POSIX)
  • Realized via condition variables, mutexes or
    semaphores and pthread functions.
  • Conditions
  • pthread_cond_t cond PTHREAD_COND_INITIALIZER
  • ? static initialization
  • int pthread_cond_init(pthread_cond_t cond,
    pthread_condattr_t cond_attr)
  • ? dynamic initialization

12
Threads synchronisation, interruption (POSIX)
  • int pthread_cond_signal(pthread_cond_t cond)
  • ? signals the condition cond to the first waiting
    thread in queue
  • int pthread_cond_wait(pthread_cond_t cond,
    pthread_mutex_t mutex)
  • ? thread waits for the signalisation of condition
    cond, protected by a mutex
  • int pthread_cond_destroy(pthread_cond_t cond)
  • ? cleans up condition cond

13
Threads synchronisation, interruption (POSIX)
  • Mutex (mutual exclusion) usage
  • pthread_mutex_t mutex
  • int pthread_mutex_init(pthread_mutex_t mutex,
    const pthread_mutexattr_t mutexattr)
  • ? initialization of mutex attribute can have
    values like PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
    PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP or
  • NULL
  • int pthread_mutex_destroy(pthread_mutex_t
    mutex)
  • ? deinitialization of mutex

14
Threads synchronisation, interruption (POSIX)
  • Mutex usage
  • int pthread_mutex_lock(pthread_mutex_t mutex)
  • ? locks the mutex this calls is blocking in
    case of an already locked mutex and is only
    continuing when the mutex is unlocked in another
    thread.
  • int pthread_mutex_trylock(pthread_mutex_t
    mutex)
  • ? tries to lock the mutex, but does not block in
    case of already locked.
  • NOTE test the return value to check the former
    state of the mutex (mutex was locked before or
    not) ? error code EBUSY
  • int pthread_mutex_unlock(pthread_mutex_t mutex)
  • ? unlocks a locked mutex

15
Threads concurrency problems
thread 1
thread 2
2) interruption
1) write access
globally accessible memory
3) read access
4) continue write
16
Threads concurrency problems
  • Solution
  • protection of critical areas (commands) mutexes
    or semaphores
  • dedicated access functions (usage of mutex or
    semaphore hidden inside function)
  • example
  • // global variable
  • char aString50
  • pthread_mutex_t aMutex
  • ...
  • pthread_mutex_init(aMutex, NULL)
  • // thread 1
  • pthread_mutex_lock(aMutex)
  • cin gtgt aString
  • pthread_mutex_unlock(aMutex)
  • ...

// thread 2 pthread_mutex_lock(aMutex) cout ltlt
aString ltlt endl pthread_mutex_unlock(aMutex) ..
.
17
Threads termination (POSIX)
  • A thread ends, when
  • main thread function finishes
  • call of void pthread_exit(void exitVal) inside
    thread function
  • call of int pthread_cancel(t_handle) outside of
    thread function
  • Note call of exit() in thread exits whole
    process
  • Request of exit value of a thread
  • int pthread_join(t_handle, exitVal)
  • but terminated thread must have been created with
    PTHREAD_JOINABLE as attribute.
  • ? Can be used for synchronisation waits for
    termination of thread t_handle

18
Threads termination (POSIX)
  • A thread can declare its cancel state
  • enable PTHREAD_CANCEL_ENABLE
  • disable PTHREAD_CANCEL_DISABLE
  • ? int pthread_setcancelstate(int state, int
    oldstate)
  • And also the cancel type
  • immediate cancellation PTHREAD_CANCEL_ASYNCHRONOU
    S
  • cancellation at dedicated cancellation points
    PTHREAD_CANCEL_DEFERRED
  • ? int pthread_setcanceltype(int type, int
    oldtype)
  • ? Normally declared in beginning of thread, but
    can also be changed later on.

19
Threads frameworks
  • POSIX
  • ? in most Linux distributions included
  • ACE
  • ? The ADAPTIVE Communication Environment
  • http//www.cs.wustl.edu/schmidt/ACE.html
About PowerShow.com