Multithreading Using pthreads in C language (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody my name is moss norman and in today's video i'm going to walk you through how to write a multi-threaded program in c i thought it was time to do a follow-up video from my previous video on multi-threading introducing you to the concept it's now time to see a concrete example of multi-threading and in order to write our multi-threaded c program we're going to make use of a library called p threads p threads is short for posix threads the pthreads library offers an interface through which you can create update and destroy threads in a program and as we write our code i'll explain the basic functions that you call from the pthreads library so with that if you haven't already grab a coffee and let's get coding okay so i have my editor opened i'm using adam to make this program but you can use whatever editor you prefer i've created a new file and i've called it mt hyphen example.c and this is going to be our our multi-threaded program and the first thing that we need to add in the program is we need to link the necessary libraries so the first one that we're going to add is standard input output then we're going to link the standard library we're also going to link the uh library to interface with the uh the posix api and then we'll we'll also link the pthread library okay so the pthread library is is the library that gives us provides an interface for interacting creating and destroying new uh new p threads so the next thing that we're going to do is we're going to create a main function and inside of that main function we're going to declare a new variable called tid 0 and it's of data type p thread underscore t and that data type is uh is something that's provided by the p threads interface and it's to uniquely identify a a thread so this is actually just going to be a large integer basically and it's going to uniquely identify the thread that we're about to create after we've declared this uh variable we can then call the p thread create function and the p thread create function is what's actually going to initialize a new thread and it takes several arguments and the first argument is the address to the variable that we just declared tid 0 and it's going to come up with a new id and it's going to assign that new id to this tid 0 variable so we pass in the address of that declared variable and then in the the second parameter to the p thread create function the second parameter is a data structure of attributes that you can assign to the new thread that you're creating and these attributes describe the the behavior uh of that thread and how it's going to behave and what it can do and what it can't do for instance a thread can be can be made not joinable so basically that means that its life cycle is not going to be dependent on the on the life cycle of the main uh program of the thread running this main function so it will have an independent life cycle uh in this case we're going to use null so we're not going to pass in any any attributes we're just gonna keep the default uh the default behavior uh when we create this thread and then the third uh the third argument to the pthread create function is the uh a pointer to the function that you want this new thread to execute so when you create a new thread you have to give it a piece of code that it can run and execute and we do that by passing in a function so we refer to a function that that thread will then execute the code inside of that function we haven't created this function yet but i'm going to just name it here it's going to be called worker thread funk and then in the fourth argument and the last argument that we're going to pass in is the uh actually the input to this worker thread function so if we have any arguments for this uh for this thread we can actually pass them in in this fourth uh in this fourth parameter to the pthread create function and it has to be passed in as a void pointer okay so this is going to be in our case what we're going to do is we're going to pass in the thread id uh into our worker thread function so what we want this worker thread function to do is basically just print out to the console hello world this is thread and then print the id of that specific thread so it should print this value so now that we have this pthread create function set up let's go ahead and call the p thread exit function and we're going to pass in null as a parameter to p thread exit so the p thread exit function basically forces this uh main thread to wait until the thread that we create in pthread create is finished with its work so it will execute all of the code inside of worker thread function and then this main thread will exit but if we don't include this what will happen is the main thread will exit too fast it will exit before this worker thread function can complete its work and so nothing will be printed out to the console and i'll show what that actually looks like later on but for now uh we'll just keep this uh this invocation in the program and the last thing that we want to do is we want to return and that will actually exit the the program now that we have our main function set up let's go ahead and implement our worker thread function it's going to uh have a void return type and it will take as input a void pointer which will be the thread id and we have to cast that thread id to a long integer so we're going to create a new variable a long pointer and we're going to set that to the thread id argument and now that we've done that let's go ahead and print out to the console hello world this is thread and then we're going to use uh a format a string formatter to insert the thread id alrighty so now this program when it runs and let me just add a new line here so it's a little bit more readable when this program runs it will create a new thread and then that thread is going to take his input that thread's unique identifier and it will print that out to the console so let's go ahead and compile it and see if it runs i'm just going to confirm that i'm in the right directory and i am so to compile the program i'm just going to use gcc and then mt example.c and when we compile a program that's using the pthreads library we actually have to link it using the the gcc option dash lp thread okay so it won't work if we don't link p threads uh here okay so it worked but it did give me a warning uh let me fix that warning it's just complaining about what kind of uh oh uh let me just forgot to de-reference the uh the id so let's recompile and now that generated an executable file a.out so i'm going to call a dot out here and then it prints out to the console hello world this is thread and then this is the unique identifier of the thread that we created so that was simple enough to do but let's make it a little bit more complex let's actually create several several threads in our program so we're going to modify this and convert this into a for loop and we'll create three threads and they'll all print to the console their unique identifiers so the first thing that i have to do here is i have to declare two more thread id variables and then i'm going to write a for loop here we'll do int i is equal to zero i is less than three and then increment i we'll place the pthread create function inside of here now in order to provide each thread their respective thread id variable what we'll need to do is put the addresses of these variables into an array so what i'll do is i'll create a p thread type pointer array and it will be equal to the addresses of the thread ids all right and then when we iterate over this for loop what i'll do is i'll just simply refer to index i as the uh as the correct variable now when we run this program uh recompile it and run it what we should see is three print statements printed out of the console and then three unique thread ids so let's go ahead and recompile and and see what we get and so you can see here now each of these numbers is unique so we created three threads in each one of those threads printed out to the console they're unique identifiers so i mentioned that i would also show you what happens if we were to not call p thread exit so let's go ahead and remove pthread exit and just see what happens and recompile here so you can see the the program immediately returns and the reason for that is that the main thread that's running this main function here is immediately exiting after it completes this for loop so as soon as it completes this for loop it's go it's returning and then the program exits and so it doesn't wait for the these three pro these three p threads to complete their work and print to the console so that's why we need the the call to the p thread exit to ensure that the main program is gonna wait for these threads to complete their work before it actually exits so let's go ahead and just recompile and run so this is a pretty basic introduction to multi-threading and i hope you enjoyed it if you did please consider throwing a like on the video and subscribing to the channel for upcoming multi-threaded videos uh in the next video we're going to kind of uh expand this program out a little bit more and we'll get to see the behavior of threads when they're accessing shared data so stay tuned and thanks for watching
Info
Channel: Tech and Beyond With Moss
Views: 16,880
Rating: 4.968689 out of 5
Keywords: multithreading, programming, C programming, coding, multithreaded, tutorial, c language, posix threads, threads, mutex, programming in c, c programming language, c programming projects, multithreading in c, pthread tutorial c, c multithreading, multithreading c, pthread in c, pthreads in c, threads in c, multithreading in c programming, pthread tutorial in c
Id: qPhP86HIXgg
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Fri Sep 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.