1 / 11

Thread API

Thread API. Xiaohua Lu Office : CS 3310 Tel. : 2621721 Email : lxh@cs.wisc.edu Office hours: 11-12,3-5 T,TR. Pthread API. thread creation & termination thread synchronization mutex condition variable semaphore. Thread creation.

maik
Télécharger la présentation

Thread API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thread API Xiaohua Lu Office : CS 3310 Tel. : 2621721 Email : lxh@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_function,arg); int status; pthread_t thread; pthread_attr_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 ((status=pthread_attr_init(&thread_attr) ) != 0){ ... } if ((status = pthread_create(&thread_id,&thread_attr,request_process,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(&mutex,&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); status = 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 ) < 0 ){ SHOW_ERROR( "Locking mutex failed" ); return RTP_ERROR; } // Check if Sending buffers are available while ( m_nOutWndBufs <= 0 ) { if ( pthread_cond_wait( &m_condSend, &m_mutexSend ) < 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,initial_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 = file’s last modification time? • At most 32 concurrent client requests. • client server_address filepath+filename server address should allow formats like localhost, nova23.cs.wisc.edu and 128.105.120.123

More Related