Help Lab Threads
Help Lab Threads
Help material
Lab # 8
1. What is thread?
A thread is a semi-process that has its own stack, and executes a given piece
of code. Unlike a real process, the thread normally shares its memory with
other threads (where as for processes we usually have a different memory
area for each one of them). A Thread Group is a set of threads all executing
inside the same process. They all share the same memory, and thus can
access the same global variables, same heap memory, same set of file
descriptors, etc. All these threads execute in parallel (i.e. using time slices, or
if the system has several processors, then really in parallel).
2. What are pthreads?
Historically, hardware vendors have implemented their own
proprietary versions of threads. These implementations differed
substantially from each other making it difficult for programmers to
develop portable threaded applications. In order to take full advantage
of the capabilities provided by threads, a standardized programming
interface was required. For UNIX systems, this interface has been
specified by the IEEE POSIX 1003.1c standard (1995). Implementations
which adhere to this standard are referred to as POSIX threads, or
Pthreads.
Pthreads are defined as a set of C language programming types and
procedure calls. Vendors usually provide a Pthreads implementation in
the form of a header/include file and a library, which you link with your
program.
3. The pthreads API:
The subroutines which comprise the Pthreads API can be
informally grouped into three major classes:
• Thread management: The first class of functions work directly
on threads - creating, detaching, joining, etc. They include
functions to set/query thread attributes (joinable, scheduling
etc.)
• Mutexes: The second class of functions deal with a coarse
type of synchronization, called a "mutex", which is an
abbreviation for "mutual exclusion". Mutex functions provide
for creating, destroying, locking and unlocking mutexes. They
are also supplemented by mutex attribute functions that set
or modify attributes associated with mutexes.
4. Thread Management Functions:
• Run: ./a.out
To Practice
• Pointers to variables
• Pointer to functions
• Void pointer
• Casting pointers
• thread - returns the thread id. (unsigned long int
defined in bits)
• attr - Set to NULL if default thread attributes are
used.
• void * (*start_routine) - pointer to the function to
be threaded. Function has a single argument:
pointer to void.
• *arg - pointer to argument of function. To pass
multiple arguments, send a pointer to a structure.
Example 1
#include <stdio.h>
#include <pthread.h>
void *kidfunc(void *p)
{
printf ("Kid ID is ---> %d\n", getpid( ));
}
main ( )
{
pthread_t kid ;
pthread_create (&kid, NULL, kidfunc, NULL) ;
printf ("Parent ID is ---> %d\n", getpid( )) ;
pthread_join (kid, NULL) ;
printf ("No more thread!\n") ;
How will we do this?
• Kid ID is ---> 3093
• Parent ID is ---> 3093
• No more thread!
• ----------------------
• No more thread!
• Parent ID is ---> 3093
• Kid ID is ---> 3093
#include <stdio.h>
#include <pthread.h>
int glob_data = 5 ;
void *kidfunc(void *p)
{
printf ("Kid here. Global data was %d.\n", glob_data) ;
glob_data = 15 ;
printf ("Kid Again. Global data was now %d.\n", glob_data) ;
}
main ( )
{
pthread_t kid ;
pthread_create (&kid, NULL, kidfunc, NULL) ;
printf ("Parent here. Global data = %d\n", glob_data) ;
glob_data = 10 ;
pthread_join (kid, NULL) ;
printf ("End of program. Global data = %d\n", glob_data) ;
}
Example 3
#include <pthread.h>
#include <stdio.h>
void *PrintHello(void *threadid)
{
printf("\n%d: Hello World!\n", threadid);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[3];
int rc;
int t;
Continue
for(t=0; t<3; t++)
{
printf("In main: creating thread\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
pthread_join(threads[t], NULL );
}
pthread_exit(NULL);
}
Links
• https://www.tutorialspoint.com/cplusplus/cpp_return_pointer_from_functions.htm
• https://stackoverflow.com/questions/11626786/what-does-void-mean-and-how-to-use-it
• http://www.geeksforgeeks.org/void-pointer-c/
• https://stackoverflow.com/questions/6420090/pthread-concepts-in-linux
• http://www.circuitstoday.com/void-pointers-in-c
• http://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html
• http://documentation.microfocus.com/help/index.jsp?topic=%2Fcom.microfocus.eclipse.info
center.studee60ux%2FHRCLRHCALL0J.html
Assignment 4