


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Process A Thread 1 Global Variables
Code
Stack
Process A Thread 2
Stack
pthread_create()
Each process can include many threads.
All threads of a process share:
pthread_create(
**pthread_t tid, const pthread_attr_t attr, void (func)( **void *), void *arg );**
func is the function to be called.
When func() returns the thread is terminated
usually an unsigned int. When debugging, it's often useful to do something like this:
struct { int x,y } 2ints;
**void *blah( void *arg) { struct 2ints foo = (struct 2ints ) arg; printf("%u sum of %d and %d is %d\n", pthread_self(), foo->x, foo->y, foo->x+foo->y); return(NULL); }
int pthread_join( pthread_t Docsity.com
**int counter=0; void pancake(void arg) { counter++; printf("Thread %u is number %d\n", pthread_self(), counter); } main() { int i; pthread_t tid; for (i=0;i<10;i++) pthread_create(&tid,NULL,pancake,NULL); }
pthread_mutex_t counter_mtx= PTHREAD_MUTEX_INITIALIZER;