Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads CSIT 402 Data Structures II. many to oneone to one many to many Multithreading models.

Similar presentations


Presentation on theme: "Threads CSIT 402 Data Structures II. many to oneone to one many to many Multithreading models."— Presentation transcript:

1 Threads CSIT 402 Data Structures II

2 many to oneone to one many to many Multithreading models

3 Thread Functions functiondescription thr_createCreate a thread thr_selfGet the thread identifier thr_yieldYield thread execution thr_exitTerminate a thread thr_joinWait for thread termination thr_suspendSuspend thread execution thr_continueContinue thread execution thr_setprioSet the thread priority thr_getprioGet the thread priority

4 Thread Functions thr_create(void *stack_base, size_t stack_size, void*(*start_routine)(void*), void* arg, long flag, thead_t * new_thread); stack_base - contain the address for the stack, if it is NULL, thr_create will allocate a stack for the new thread with at least stack_size bytes. stack_size - contain the size, in number of bytes, for the stack that the new thread uses. start_routine - contain the function which the new thread begins execution. arg – Can be anything that is described by void, which is typically any 4-bytes value. Note that you can supply only one argument. To get your procedure to take multiple arguments, encode them as one structure.

5 Thread Functions thr_create(void *stack_base, size_t stack_size, void*(*start_routine)(void*), void* arg, long flag, thead_t * new_thread); flags – Specifies attributes for the created thread. THR_SUSPENDED THR_DETACHED THR_BOUND THR_NEW_LWP THR_DAEMON new_thread – Points to a location where the ID of the new thread is stored

6 Thread Functions Get the thread identifier thread_t thr_self(void); Yield thread execution void thr_yield(void); void thr_exit(void* status); Terminate a thread

7 thr_join(thread_t tid, thread_t *departedid, void **status); Wait for thread termination Thread Functions thread_t tid; thread_t departedid; int ret; int status; ret = thr_join( tid, &departedid, (void*) status); //join tid thread with status ret = thr_join(tid, &departedid, NULL); //join tid thread without status ret = thr_join(tid, NULL, NULL); //join tid thread without return id and status ret = thr_join(NULL, &departedid, NULL);

8 Thread Functions int thr_suspend(thread_d tid); Suspend thread execution Continue thread execution int thr_continue(thread_t tid);

9 Set the thread priority Thread Functions Thread_t tid; int ret; int newprio = 20; ret = thr_create(NULL, NULL, func, arg, THR_SUSPEND, &tid); // suspend created thread ret = thr_setprio(tid, newprio); // set the new priority to the new thread ret = thr_continue(tid); //suspended thread starts to execute with the new priority int thr_setprio(thread_t tid, int newprio); Get the thread priority int thr_getprio(thread_t tid, int * newprio);

10 Critical Section P1: X++; Load M(0x300) INC Store M(0x300); P1: load M(0x300) P1: INC P2: load M(0x300) P2: DEC P1: Store M(0x300) P2: Store M(0x300) P2: X--; Load M(0x300) DEC Store M(0x300);

11 Synchronization Functions Mutual Exclusion Lock. Initialize a mutex. Destroy a mutex. Acquire a mutex. Release a mutex. Try to acquire a mutex

12 Synchronization Functions Initialize a mutex mutex_t mp; int mutex_init(mutex_t *mp, int type, void *arg); TYPE: 1.USYNC_PROCESS : the mustex can be used to synchronize threads and other processes. 2.USYNC_PROCESS_ROBUST : the mutex can be used to robustly synchronize threads in this and other processes. 3.USYNC_THREAD : the mutex can be used to synchronize threads in this process only.

13 . Destroy a mutex int mutex_destroy(mutex_t* mp);. Acquire a mutex int mutex_lock(mutex_t* mp);. Release a mutex int mutex_unlock(mutex_t* mp);. Try to acquire a mutex int mutex_trylock(mutex_t* mp); Synchronization Functions

14 Condition Variables - Initialize a condition variable - Destroy a condition variable - Wait for a condition variable - Wait for an absolute time - Signal one condition variable - Signal all condition variable

15 Synchronization Functions Initialize a condition variables cond_t cv; int cond_init(cond_t *cv, int type, int arg); TYPE: 1. USYNC_PROCESS : the condition variables can be used to synchronize threads and other processes. 2. USYNC_THREAD : the condition variables can be used to synchronize threads in this process only.

16 Synchronization Functions Destroy a condition variables int cond_destroy(cond_t *cv); Wait for a condition cond_t cv; int cond_wait(cond_t *cv, mutex_t *mp);

17 Synchronization Functions cond_timewait(cond_t* cv, mutex_t *mp, timestruct_t abstime) Wait for an absolute time Signal one condition variable int cond_signal(cond_t *cv); int cond_broadcast(cond_t * cv);

18 Synchronization Functions Semaphores - initialize a semaphore - increment a semaphore - block a semaphore count - decrement a semaphore count - destroy a semaphore state

19 Synchronization Functions int sema_init(sema_t *sp, unsigned int count, int type, void* arg); Initialize a semaphore sema_t sp; int ret; int count; count = 4; ret = sema_init(&sp, count, USYNC_THREAD, 0); ret = sema_init(&sp, count, USYNC_PROCESS, 0);

20 Synchronization Functions Increment a semaphore count int sema_post(sema_t *sp); Decrement a semaphore count int sema_trywait(sema_t* sp); Destroy a semaphore int sema_destroy(sema_t * sp);

21 Synchronization Functions Read-Write Locks. Initialize a read-write lock. Acquire a read lock. Try to acquire a read lock. Acquire a write lock. Try to acquire a write lock. Unlock a read-write lock. Destroy read-write lock state

22 Synchronization Functions Initialize a read-write lock rwlock_init(rwlock_t * rwlp, int type, void* arg); rwlock_t rwlp; int ret; ret = rw_lock_init(&rwlp, USYNC_THREAD, 0); ret = rw_lock_init(&rwlp, USYNC_PROCESS, 0); int rw_rdlock(rwlock_t *rwlp); int rw_tryrdlock(rwlock_t *rwlp); int rw_wrlock(rwlock_t *rwlp); int rw_trywrlock(rwlock_t *rwlp); int rw_unlock(rwlock_t *rwlp); int rw_destory(rwlock_t *rwlp);


Download ppt "Threads CSIT 402 Data Structures II. many to oneone to one many to many Multithreading models."

Similar presentations


Ads by Google