SYNOPSIS

#includepthread.h

int

pthread_create(pthread_t*thread, constpthread_attr_t*attr,

void*(*start_routine)(void*), void*arg);

DESCRIPTION

The pthread_create() function is used to create a new thread, with

attributes specified by attr, within a process. If attr is NULL, the

default attributes are used. If the attributes specified by attr are

modified later, the thread's attributes are not affected. Upon

successful completion pthread_create() will store the ID of the created

thread in the location specified by thread.

The thread is created executing start_routine with arg as its sole

argument. If the start_routine returns, the effect is as if there was an

implicit call to pthread_exit() using the return value of start_routine

as the exit status. Note that the thread in which main() was originally

invoked differs from this. When it returns from main(), the effect is as

if there was an implicit call to exit() using the return value of main()

as the exit status.

RETURN VALUES

If successful, the pthread_create() function will return zero. Otherwise

an error number will be returned to indicate the error.

NAME

pthread_join - wait for thread termination

LIBRARY

POSIX Threads Library (libpthread, -lpthread)

SYNOPSIS

#includepthread.h

int

pthread_join(pthread_tthread, void**value_ptr);

DESCRIPTION

The pthread_join() function suspends execution of the calling thread

until the target thread terminates unless the target thread has already

terminated.

On return from a successful pthread_join() call with a non-NULL value_ptr

argument, the value passed to pthread_exit() by the terminating thread is

stored in the location referenced by value_ptr. When a pthread_join()

returns successfully, the target thread has been terminated. The results

of multiple simultaneous calls to pthread_join() specifying the same

target thread are undefined. If the thread calling pthread_join() is

cancelled, then the target thread is not detached.

RETURN VALUES

If successful, the pthread_join()function will return zero. Otherwise an error number will be returned to indicate

the error.

NAME

pthread_exit - terminate the calling thread

LIBRARY

POSIX Threads Library (libpthread, -lpthread)

SYNOPSIS

#includepthread.h

void

pthread_exit(void*value_ptr);

DESCRIPTION

The pthread_exit() function terminates the calling thread and makes the

value value_ptr available to any successful join with the terminating

thread. Any cancellation cleanup handlers that have been pushed and are

not yet popped are popped in the reverse order that they were pushed and

then executed. After all cancellation handlers have been executed, if

the thread has any thread-specific data, appropriate destructor functions

are called in an unspecified order. Thread termination does not release

any application visible process resources, including, but not limited to,

mutexes and file descriptors, nor does it perform any process level

cleanup actions, including, but not limited to, calling atexit() routines

that may exist.

An implicit call to pthread_exit() is made when a thread other than the

thread in which main() was first invoked returns from the start routine

that was used to create it. The function's return value serves as the

thread's exit status.

The behavior of pthread_exit() is undefined if called from a cancellation

handler or destructor function that was invoked as the result of an

implicit or explicit call to pthread_exit().

After a thread has terminated, the result of access to local (auto)

variables of the thread is undefined. Thus, references to local

variables of the exiting thread should not be used for the pthread_exit()

value_ptr parameter value.

The process will exit with an exit status of 0 after the last thread has

been terminated. The behavior is as if the implementation called exit()

with a zero argument at thread termination time.

RETURN VALUES

The pthread_exit() function cannot return to its caller.

/* program name: pthread.c

location: ~bi/2013s/cs358/

compile: cc -lpthreadpthread.c

run: a.out

description: this program shows how to user POXIS thread system calls

to create a multi-thread C program. a global variable (

shared_int) is incremented and decremented by two threads

(running increment_shared_int and decrement_shared_int)

concurrently.

*/

#include <stdio.h

#include <stdlib.h

#include <unistd.h

#include <pthread.h

intshared_int = 100; // shared by the two threads

// function executed by thread 1

void *increment_shared_int( void *ptr )

{

int *incPtr = (int*)ptr;

inti;

//printf("please enter an increment of int to start thread 1\n");

//scanf("%d", incPtr); // enter an int to start thread 1

for (i = 0; i < 10; i++) {

shared_int += *incPtr;

printf("Thread 1 - %d = + %d\n", shared_int, *incPtr);

sleep(2);

}

return NULL;

}

// function executed by thread 2

void *decrement_shared_int( void *ptr )

{

int *decPtr = (int*)ptr;

inti;

for (i = 0; i < 10; i++) {

shared_int -= *decPtr;

printf("Thread 2 - %d = - %d\n", shared_int, *decPtr);

sleep(2);

}

return NULL;

}

intmain()

{

pthread_t thread1, thread2, thread3;

int increment = 10;

int decrement = 5;

int iret1, iret2, iret3;

// create and start the two threads

iret1 = pthread_create( &thread1, NULL, increment_shared_int,

(void*) &increment);

iret2 = pthread_create( &thread2, NULL, decrement_shared_int,

(void*) &decrement);

// iret3 = pthread_create( &thread3, NULL, increment_shared_int,

// (void*) &increment);

printf("Main wating for child thread #1\n");

pthread_join( thread1, NULL);

printf("Main wating for child thread #2\n");

pthread_join( thread2, NULL);

// printf("Main wating for child thread #3\n");

// pthread_join( thread3, NULL);

printf("both threads have terminated\n");

exit(0);

}

/* Program name: pthread-local-variable.c

location: ~bi/2013s/cs358/

compile: cc -lpthread pthread-local-variable.c

execute: a.out

Descrption: this program is used to show how local variables

may be used in threads. the program contains a

producer and a consumer and they share a buffer

of 10 integers. The buffer is created

as a local variable in main() and a pointer to

the buffer is passed to the producer and the

consumer.

since each thread has its own stack and main()

is running in its own thread and its local

variables are invalidated when the function

completes. Thus, it is possible that the main()

may finish before the two other threads and

that would invalidate the buffer. To make

sure the two threads would not use invalid

data, the main() uses pthread_wait to wait for

the two threads before it terminates.

*/

#include <stdio.h

#include <stdlib.h

#include <pthread.h

#include <unistd.h

typedef struct BUFFER {

intarray[10];

intfirstItem;

int count;

} Buffer;

void *producer(void *ptr ) {

Buffer *buffer = (Buffer*)ptr;

inti;

for (i = 0; i < 10; i++) {

buffer->array[(buffer->firstItem + buffer->count) % 10] = i + 100;

buffer->count = buffer->count + 1;

printf("thread 1: item = %d at %d\n", i+100, i);

sleep(2);

}

return NULL;

}

// function executed by thread 2

void *consumer(void *ptr ) {

Buffer *buffer = (Buffer*)ptr;

inti;

int item;

for (i = 0; i < 10; i++) {

if (buffer->count > 0) {

item = buffer->array[buffer->firstItem];

buffer->firstItem = (buffer->firstItem + 1) % 10;

buffer->count = buffer->count - 1;

printf("thread 2: item = %d \n", item);

sleep(3);

}

}

return NULL;

}

intmain()

{

pthread_t thread1, thread2;

Buffer buffer;

int iret1, iret2;

buffer.firstItem = 0;

buffer.count = 0;

// create and start the two threads

iret1 = pthread_create(&thread1, NULL, producer, (void*) &buffer);

iret2 = pthread_create(&thread2, NULL, consumer, (void*) &buffer);

printf("Main wating for child thread #1\n");

pthread_join(thread1, NULL);

printf("Main wating for child thread #2\n");

pthread_join(thread2, NULL);

printf("both threads have terminated\n");

exit(0);

}

//

// how to return a value from a thread to the main

#include <stdio.h

#include <stdlib.h

#include <pthread.h

intshared_int = 100; // shared by the two threads

// function executed by thread 1

void *increment_shared_int( void *ptr )

{

int *increment = (int*)ptr;

inti;

for (i = 0; i < 10; i++) {

shared_int += *increment;

printf("Thread 1 - %d\n", shared_int);

sleep(2);

}

//return (void*)3456;

pthread_exit((void*)3456);

}

// function executed by thread 2

void *decrement_shared_int( void *ptr )

{

int *decrement = (int*)ptr;

inti;

for (i = 0; i < 10; i++) {

shared_int -= *decrement;

printf("Thread 2 - %d\n", shared_int);

sleep(2);

}

}

main()

{

pthread_t thread1, thread2;

int increment = 10;

int decrement = 5;

int iret1, iret2;

intreturned_value = 1000;

// create and start the two threads

iret1 = pthread_create( &thread1, NULL, increment_shared_int, (void*) &increment);

iret2 = pthread_create( &thread2, NULL, decrement_shared_int, (void*) &decrement);

printf("Main wating for child thread #1\n");

pthread_join( thread1, (void*) &returned_value);

printf("returned from thread #1 = %d\n", returned_value);

printf("Main wating for child thread #2\n");

pthread_join( thread2, NULL);

printf("both threads have terminated\n");

exit(0);

}