Document not found! Please try again

Introduction to UNIX - Google Sites

2 downloads 68 Views 564KB Size Report
The opaque objects can be modified by calls to attribute functions, which deal .... The programmer may optionally specif
Threads (Ch.11, Ch12 of APUE) 2014.1학기 권준호 [email protected]

Slides are base on the web tutorial from http://www.llnl.gov/computing/tutorials/pthreads

Processes and Threads 







In shared memory multiprocessor architectures, such as SMPs, threads can be used to implement parallelism. Historically, hardware vendors have implemented their own proprietary versions of threads, making portability a concern for software developers. For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads.

2

Processes and Threads 



These notes begin with an introduction to concepts, motivations, and design considerations for using Pthreads. Each of the three major classes of routines in the Pthreads API are then covered:   

Thread Management Mutex Variables Condition Variables

What is a Thread? 

 





 

Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. But what does this mean? The concept of a "procedure" that runs independently from its main program may best describe a thread. Imagine a main program (a.out) that contains a number of procedures. Then imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi-threaded" program. How is this accomplished?

Process vs. Thread 

Process



Thread

5

The Pthreads API 

The concept of opaque objects pervades the design of the API



The basic calls work to create or modify opaque objects









The opaque objects can be modified by calls to attribute functions, which deal with opaque attributes

The Pthreads API contains over 60 subroutines We focus on those which are most likely to be immediately useful to the beginning Pthreads programmer For portability, the pthread.h header file should be included in each source file using the Pthreads library.

The Pthreads API 

Thread management: 



create, terminate, join, and detach.

Threads synchronization: 

Barriers: 



Mutexes: 



creating, waiting, and destroying barriers.

mutual exclusion, creating, locking, unlocking, and destroying mutexes.

Condition variables: 

creating, waiting, signaling, and destroying conditional variables 7

Thread management 

Thread creation

int pthread_create( pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void *restrict arg);



Thread termination

void pthread_exit(void *retval);

8

Threading “Hello World!” (1/2) #include #include #include #define NUM_THREADS

5

void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; for(long i=0;isum; hello_msg = my_data->message; printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum); pthread_exit(NULL);

struct thread_data thread_data_array[NUM_THREADS];

15

hello_arg2.c (2/3) int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int *taskids[NUM_THREADS]; int rc, t, sum; sum=0; messages[0] messages[2] messages[4] messages[6]

}

= "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!";

for(t=0;t