How to create and join threads in C (pthreads).

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody welcome back today we're going to talk about threads one of the most interesting one of the greatest one of the scariest and possibly dangerous programming tools you have at your disposal have you ever wondered how to make your program work on two things at the same time well today we're gonna learn how several months ago I made a video about fork and exec and I showed you how to use fork to create a new process to basically clone one process into two processes and using multiple processes gives you two benefits the first is concurrency both your processes can be working on separate things they can be doing two different things at the same time the second thing is isolation processes are isolated from one another their memory is separate and if one of them crashes usually the others just fine threads give you one of these threads basically give you the opportunity to get concurrency without the isolation so basically your program can be working on two different things making progress moving forward getting useful work done on two different things concurrently but without the isolation threads keep it all in the same process threads have different call stacks but they're in the same memory space that means that one thread can write into the memory of another thread which makes it easier to communicate between threads and that can be both a blessing and a curse but we'll get to that later today I just want to give you a brief introduction into threads and show you how to create one so let's make a thread there are several different api's for creating threads some are only available on some operating systems yes Windows has a function called create thread that can be used to create a new thread no I'm not going to demonstrate it today because I don't have a Windows machine handy I'm going to focus on the POSIX threads API or P threads because it's a standard API that you can use just about everywhere and yes you can use P threads on Windows you just have to download a third-party library from SourceForge and there you go you're on your way but whatever API you're using thread api's tend to be all about the same you're gonna have a function that creates a new thread with P threads that's thread create and p3 create needs to know what code we want to run in the new thread and so we're gonna pass it a pointer to a function that we want to run and thread functions can often take arguments with P threads the thread function takes one void pointer argument and it returns a void pointer and we'll get to that a little bit later and that allows us to pass any type to our thread and return any tie from a new thread and we'll get to that later you can also tell P threads created give your threads specific attributes and things you can make things more complicated but for now we're just gonna stick with the defaults and save attributes for another day because most of the time the defaults are just fine my other disclaimer today is there is a lot we can talk about with threads there's way way more than I can put in this video so in this video I'm just going to cover a simple example and then we're gonna add bells and whistles and complications in future videos okay so let's make a simple program that does something simple like stupid simple like so simple that nobody would ever really do this but since when if we let that stop us so let's make two functions one that prints my turn every second forever and one that prints out your turn every two seconds forever pretty simple pretty stupid let's say we want to do both and let's say that so like naively I could say well let's call one after the other and then well you know what's gonna happen but let's try it out anyway well maybe if I can get my include sorted out header files okay much better okay so now we run the code and of course it just says my turn forever and the second function never gets to run which doesn't seem to fit into the spirit of the example so yep you guessed it we're going to make a thread so I'm gonna declare a new P thread underscore t variable and that's gonna represent my new thread then I'm gonna call pthread create for P thread create I need two things the P thread underscore t I just declared that helps us keep track of the thread that we're gonna create and a function now thread functions like I mentioned before they have to look a certain way in P threads they have to return a void pointer and they need to take a void pointer as an argument so if we change up the my turn function to fit the mold then we can just pass it the thread create and then when the new thread runs it's going to run the my turn function okay and we compile it and we run it and it works as you can see the functions are running concurrently they're interleaving their executions you're saying my turn your turn they're taking turns and they're having a wonderful time at it there's not doing anything useful but you and I could change that really easily by sticking code that does useful stuff in both of these functions and now we would have useful stuff happening concurrently maybe later for now let's consider a different scenario what if our functions don't run forever well we'll fix that by changing our while loops to for loops and what if one operation takes longer than the other specifically what if my new thread takes longer to run than the your turn function well let's see so it runs well enough but it doesn't print out all eight my turns that's not good you see what happened is that main finished before the thread was complete and we all know what happens when main is done right the program exits but I wanted my thread to complete and so we're gonna use thread join thread join is super useful it'll wait until a specific thread finishes running and then it's gonna continue and it's called thread join because I like this ooh metaphors are so fun and in case you don't like counting lines let's print out I each time and and yes all loop iterations are accounted for and I think that's where we'll stop today like I mentioned threads are a really big topic there are more thread functions coming about returning values from threads about passing arguments to threads about sharing data between two threads and why threads are just as dangerous as they are awesome if you don't want to miss those other videos be sure to subscribe to my channel click the little bells so you get notifications whenever I post a new video but for now I leave you to ponder the mysteries of the universe and play around with with your newfound ability to create threads and add concurrency to your programs and so until next time I will see you later [Music]
Info
Channel: Jacob Sorber
Views: 123,893
Rating: 4.948936 out of 5
Keywords: pthreads c, pthreads intro, pthread_create, pthread_join, pthread_t, pthreads in c, create thread, join thread, using pthreads, how to, pthreads tutorial, c programming, software (industry), c language, posix threads, posix, c posix threads, thread, threads, c thread, c threads
Id: uA8X5zNOGw8
Channel Id: undefined
Length: 5min 59sec (359 seconds)
Published: Mon Dec 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.