Thread%20API - PowerPoint PPT Presentation

About This Presentation
Title:

Thread%20API

Description:

status: 0 if succeed and thread would contain the new thread id, otherwise EAGAIN or EINVAL. ... status = pthread_cond_wait(&cond,&mutex); wait on a condition ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 12
Provided by: Syste98
Category:
Tags: 20api | status | thread

less

Transcript and Presenter's Notes

Title: Thread%20API


1
Thread API
Xiaohua Lu Office CS 3310 Tel. 2621721 Email
lxh_at_cs.wisc.edu Office hours 11-12,3-5 T,TR
2
Pthread API
  • thread creation termination
  • thread synchronization
  • mutex
  • condition variable
  • semaphore

3
Thread creation
status pthread_create(thread,attr,start_functi
on,arg) int status pthread_t thread pthread_att
r_t attr void start_function(void
arg) status 0 if succeed and thread would
contain the new thread id, otherwise EAGAIN or
EINVAL.
4
An example of thread creation
int status pthread_t thread_id pthread_attr_t
thread_attr void request_process(void
arg) if ((statuspthread_attr_init(thread_
attr) ) ! 0) ... if ((status
pthread_create(thread_id,thread_attr,request_pro
cess,NULL)) ! 0) ...
5
Thread join exit
void pthread_exit(returnvalue) status
pthread_join(thread_id, returnvalue_ptr) pthread_
t thread_id int status void returnvalue void
returnvalue_ptr
6
Mutex
pthread_mutex_t mutex const pthread_mutexattr_t
attr int status status pthread_mutex_init(mu
tex,attr) status pthread_mutex_destroy(mutex)
status pthread_mutex_unlock(mutex) status
pthread_mutex_lock(mutex) - block status
pthread_mutex_trylock(mutex) - unblock
Thread i
lock(mutex) critical region unlock(mutex)

7
Condition variables
int status pthread_condition_t cond const
pthread_condattr_t attr pthread_mutex
mutex status pthread_cond_init(cond,attr) st
atus pthread_cond_destroy(cond) status
pthread_cond_signal(cond) status
pthread_cond_broadcast(cond)
8
Condition variables
status pthread_cond_wait(cond,mutex) wait on
a condition variable. First, a thread needs to
get the lock of the mutex. Then it could check
whether or not a condition is satisfied. If so,
it could continue its work and unlock the mutex
later. Otherwise, it would wait until some thread
use pthread_signal to notify it that the
condition is satisfied. It will release the mutex
while waiting and automatically regain it when
awoken.
9
An example of pthread_cond_wait()
If there are lots of sending threads and each
needs first check whether or not the sending
buffers are available. If so, they could send,
otherwise they should wait for the next available
buffer.
// For exclusive accessing of the number of
available buffers if ( pthread_mutex_lock(
m_mutexSend ) lt 0 ) SHOW_ERROR( "Locking mutex
failed" ) return RTP_ERROR // Check if
Sending buffers are available while (
m_nOutWndBufs lt 0 ) if (
pthread_cond_wait( m_condSend, m_mutexSend ) lt
0 ) SHOW_ERROR( "Waiting on condition
variable failed" ) pthread_mutex_unlock(
m_mutexSend ) return RTP_ERROR
10
Semaphore
int status,pshared sem_t sem unsigned int
initial_value status sem_init(sem,pshared,init
ial_value) status sem_destroy(sem) status
sem_post(sem) status sem_wait(sem) -
block status sem_trywait(sem) - unblock
11
Clarification of assignment 1
  • Timestamp files last modification time?
  • At most 32 concurrent client requests.
  • client server_address filepathfilename server
    address should allow formats like localhost,
    nova23.cs.wisc.edu and 128.105.120.123
Write a Comment
User Comments (0)
About PowerShow.com