0% found this document useful (0 votes)
63 views

Help Lab Threads

A thread is a semi-process that shares memory with other threads in the same process and executes code in parallel. Pthreads provide a standardized programming interface for threads and are implemented through a header file and library. The pthreads API includes functions for thread management like creation and termination, and for mutex synchronization. Examples demonstrate creating multiple threads that access and modify shared global data.

Uploaded by

AliWaris Haidery
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Help Lab Threads

A thread is a semi-process that shares memory with other threads in the same process and executes code in parallel. Pthreads provide a standardized programming interface for threads and are implemented through a header file and library. The pthreads API includes functions for thread management like creation and termination, and for mutex synchronization. Examples demonstrate creating multiple threads that access and modify shared global data.

Uploaded by

AliWaris Haidery
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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:

The function pthread_create is used to create a new thread, and a thread to


terminate itself uses the function pthread_exit. A thread to wait for termination of
another thread uses the function pthread_join.
Function:
Int pthread_create (pthread_t * threadhandle, pthread_attr_t *attribute,
start_routine, void *arg);
Info:
Request the PThread library for creation of a new thread. The return value is 0 on
success. The return value is negative on failure.
Function:
void pthread_exit ( void *retval /* return value passed as a pointer */ );
Info:
This Function is used by a thread to terminate. The return value is passed as a pointer.
Function:
int pthread_join ( pthread_t threadhandle void *returnvalue /* Return value is
returned by ref. */ );
Info:
Return 0 on success, and negative on failure. The returned value is a pointer returned
by reference. If you do not care about the return value, you can pass NULL for the
second argument.
5. Thread Initialization:
Include the pthread.h library :
#include <pthread.h>
Declare a variable of type pthread_t :
pthread_t the_thread
Compile:
• C compiler: gcc -lpthread pthread1.c

• 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

You might also like