In this studio, you will:
void*
pointers
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to dferry_submit@slu.edu with the phrase Threads in the subject line.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
pthread_create()
. The third
parameter to this function specifies a new thread's entry point- the
location at which the new thread begins execution.This parameter will look a little unusual, because it's a special data type
called a function pointer. The function pointer type is given as:
return_type (* function_name ) ( argument_types )
What is the return type and the expected arguments for the thread entry function?
thread_entry
with the signature
specified in the previous exercise. This function should print a message
and then return NULL.
main()
function and make a call to the
function pthread_create()
. For the time being, leave the
second and fourth parameters of this function NULL
. Be sure to
check the return value for errors. Note that this function does not set
errno
and you cannot use perror()
.
After you make a call to pthread_create()
, make a call
to pthread_join()
to wait for the created thread to finish.
The first argument of this function needs the value from parameter one
of pthread_create()
. The second argument of this function
can be left NULL.
In order to compile your program you will need to add the
-pthread
option to GCC. Copy and paste your program output.
void*
passing. To do this we need to define a
struct
, which is a collection of variables that are grouped
together. The syntax for defining a struct is as follows:
struct struct_name{
arg1_type arg1;
arg2_type arg2;
...
argN_type argN;
};
And you can then declare a struct for use in your program and access its members as such:
int main(){
struct struct_name variable_name
variable_name.arg1 = value1; //direct access
variable_name.arg2 = value2; //direct access
struct struct_name *struct_ptr = &variable_name;
struct_ptr->arg1 = value1; //pointer access
struct_ptr->arg2 = value2; //pointer access
...
}
Define a struct with three members: an integer called arg1
,
a character buffer called arg2
, and another integer called
ret
. Copy and paste your struct definition.
pthread_create()
. Create a pointer to your
struct as illustrated in the last exercise and do so.
A pointer given to the pthread_create()
function in
this way will be available to your thread entry function as the
void*
parameter. However, the compiler doesn't know how to
access the data pointed to by a void*
pointer.
To access the data inside the
struct we need to cast the pointer type back to a struct
pointer, as so:
void* thread_entry( void* args ){
struct struct_name* arg_ptr = (struct struct_name*) args;
printf("thread arg1: %d\n", arg_ptr->arg1);
printf("thread arg2: %s\n", arg_ptr->arg2);
...
}
Print out the values of your struct arguments in the newly created thread. Copy and paste the program output.
main()
function. Write a value to the third member of your structure somewhere in
your thread, and then read that value in main()
function after
you have executed pthread_join()
.
pthread_t
identifiers,
and an array
of N struct struct_name
structures to pass arguments. Then,
call pthread_create()
for N iterations, and then
pthread_join()
for N iterations.
You can easily write multiple, different strings to each thread using
the snprintf()
function, which works just like the
printf()
function. For example:
unsigned bufSize = 100;
struct struct_name args[num_threads];
for( i = 0; i < num_threads; i++ ){
snprintf(args[i].arg2, bufSize, "This is thread %d", i);
}
Copy and paste your program output for five threads when you have finished.