CS 160 Lab 2

 

Threads Programming with Examples

Objective:       1. know the framework of a multi-threaded program

                        2. be familiar with the Pthread functions.

                        3. know why and how to “ wrapper”

                        4. able to write own multi-thread programs

Outline : 

1        Review the homework of Lab 1 :
               Write a program, preferably using threads, which enables users to set alarms. When the alarm time is up, a user-set message will be printed out on the display.
 
2        Three example solutions to homework of lab 1
a.       alarm1.cc     /* example using synchronous method, (single process) */
b.      alarm2.cc     /* example using multiple processes */
c.       alarm3.cc     /* example using multiple threads */
d.      Makefile
 
3        Examine the three examples.
 
4        Introduction to Pthread functions
 
a.   pthread_t       thread;
b.   int              pthread_create( pthread_t *tid, const pthread_attr_t *attr, void *(*start)(void *), void *arg);
c.   int             pthread_detach( pthread_t tid);
d.   int             pthread_equal( pthread_t t1, pthread_t t2);
e.   int             pthread_exit( void *value_ptr);
f.   int             pthread_join( pthread_t tid, void **value_ptr);
g.   pthread_t pthread_self(void );
h.   int             pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
i.   int             pthread_mutex_destroy(pthread_mutex_t *mutex);
j.   int             pthread_mutex_lock(pthread_mutex_t *mutex);
k.   int             pthread_mutex_unlock(pthread_mutex_t *mutex);
l.   int             pthread_mutex_trylock(pthread_mutex_t *mutex);
 
5        C++ Wrapper
         Why wrapper it?
         Then, how ?
         See wrapper example test_pthread.cc
 
6        Make file again
 
 
 
  7.     Objects in the assignment 1


The following links are online tutorials.

Lecture Notes of Lab 2