Systems programming Thread management Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash
Threads Thread Ya3ni 5ai6
A process , in the simplest terms, is an executing program. One or more threads run in the context of the process . A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread
Thread management in C All C programs using pthreads need to include the pthread.h header file (i.e.: #include <pthread.h> ). There are four steps for creating a basic threaded program: 1. Define thread reference variables 2. Create an entry point for the thread 3. Create the thread 4. Join everything back up
Defi fine thread reference variables • The variable type pthread_t is a means of referencing threads. • A pthread_t variable must exist for every created thread. Example: pthread_t thread0;
Create an entry ry point for the thread • The thread must point to a function for it to start execution. • The function must return void * and take a single void * argument . For example: void * my_entry_function( void * param); A void pointer can hold address of any type and can be typcasted to any type
Create a thread • Once the pthread_t variable has been defined and the entry point function created, we can create the thread using pthread_create . • Four arguments are needed: 1. a pointer to the pthread_t variable, 2. extra attribute (don’t worry about this for now – just set it to NULL) 3. a pointer to the function to call (ie: the name of the entry point) 4. The pointer being passed as the argument to the function. Example: pthread_create(&thread0, NULL, my_entry_function, ¶m); If successful , the pthread_create() function returns a zero value. Otherwise, an error number is returned to indicate the error.
Jo Join every rything back up • When the newly- created thread has finished doing it’s bits, we need to join everything back up. • This is done by the pthread_join function which takes two parameters: 1. The pthread_t variable used when pthread_create was called (not a pointer this time) 2. A pointer to the return value pointer (don’t worry about this for now – just set it to NULL). Example: pthread_join(thread0, NULL);
All together • #include <pthread.h> • Define a worker function • void *foo(void *args) • Initialize pthread attribute to NULL • Create a thread • pthread t thread; • pthread create (&thread, &attr, worker function, arg); • Join everything back up • Exit current thread • pthread exit(status)
First example int main() { #include <pthread.h> int x = 0, y = 0; printf("x: %d, y: %d\n", x, y); #include <stdio.h> pthread_t inc_x_thread; /* this function is run by the second thread */ void *inc_x(void *x_void_ptr) if(pthread_create(&inc_x_thread, NULL, inc_x, &x)) { { fprintf(stderr, "Error creating thread\n"); return 1; /* increment x to 100 */ } int *x_ptr = (int *)x_void_ptr; while(++(*x_ptr) < 100); while(++y < 100); printf("y increment finished\n"); printf("x increment finished\n"); if(pthread_join(inc_x_thread, NULL)) { return NULL; fprintf(stderr, "Error joining thread\n"); } return 2; } x: 0, y: 0 /* show the results - x is now 100 thanks to the second y increment finished thread */ x increment finished printf("x: %d, y: %d\n", x, y); x: 100, y: 100 return 0; }
Recommend
More recommend