pthread_join(&newthread,NULL) gives me error so i used pthread_join(newthread,NULL) and it works. Can anyone explain why it works here but does not work in my case. Thankyou.
@@JacobSorber how can it be a typo and still run on your video? do you mean that the code window and the console window are not related at all??? it's all an illusion?
It's not an illusion, just a video editing mistake. When I make a 6-minute video, I usually start with 20-25 minutes of footage that I trim down-so things don't drag, and so it doesn't take me forever to upload. In this case, I think I accidentally edited out the part where it gave me an error and I fixed this. Sadly, UA-cam doesn't do video annotations anymore, and they don't let me update the video.
I searched about 12 videos looking for a cohesive explanation on pthreads before I found this one. Now I'm watching the whole series. This is the first video I've found that logically and procedurally explains pthreads and their uses. Great job. You earned a subscriber!
This was very useful and I appreciate you making this video. You seem like a wonderful teacher and am so grateful you're sharing your knowledge with the world. I can't believe this video has thumbs down! Wishing you a great 2021 ahead!
Ok, I was looking for the definition for threads and processes. I looked soo hard but still couldn't understand. I watched 3 mins of your video and all makes sense now! Thanks:)
I remember having to write my own threading library in C with pthreads (and any other threading APIs) being totally off limits. The “metal” and I got very acquainted to say the least... It was a very valuable learning experience. I’m so thankful for the pthreads library now
Video request: pls make a video where you used parallelization to solve an algorithmic problem from say Google hash code. This will help in using this to solve real problems. Thanks.
hi Jacob, very good instructions. good teaching skills. But, in this case i can only get it to work if i use ( instead of ) and i also had to change the last line to: pthread_join(newthread, NULL); ( instead of: pthread_join(&newthread, NULL); which is whats in the video). I also had to compile with the -lpthread option like this: gcc -o thread thread.c -lpthread just thought i share this, incase somebody else has the same issue. i use ubuntu 18.04 on a mac. thanks
Most people: "I hated math in high school, why did I have to memorize the quadratic formula? What the heck is an imaginary number?" Computer science majors: "...I have seen things. Things I can't unsee. Things like void *(*start_routine)(void*) ."
I hope this will be a good stepping stone for me to transition to odersky's parallel programming course on Coursera. That course really went over my head
I suppose I am not the first one, but you remind me of Matthew Mcconaughey lmao. You're doing fantastic job with these tutorials. They've really come into use with my software-programming course in c, and really sparked the interest to learn more about c now that I start to come across the cool stuff in it through your videos (let's just say that the basics course for c wasn't the most intriguing part for me :D).
Thanks...I think. You're the first to share that with me. I don't sound like I'm from East Texas, do I? :) Yeah, with so many viewers in so many different stages of learning, some videos are going to be too basic for some and too advanced for others. I'll try to keep mixing things up to keep it interesting for everyone.
@@DevFrogora In this video, I was using the platformio-ide-terminal plugin for atom. Using ZSH, with the ohmyzsh themes (I think it's the default theme)
Hi Jacob, I'm an embedded systems engineer and would like to do more work to understand threadding in Windows environement. Any chance you could do a tutorial on MinGW / MSYS and to explain win32 vs posix threads and SEH vs SJLJ exception models, etc. that would be a guide for a newbie. Kind regards, I love the videos
If you put the pthread_create function after the your_turn function would that not allow the my_turn function inside pthread_create to terminate before main terminated ? you'd then have output like "your turn" , "my turn my turn" , etc. etc. or would you just get "your turn , your turn , your turn" followed by a string of 5 "my turn" ? btw , pthread looks like a really bad idea ! Actually you know what ? I'm going to wait till I start coding C and just try things out and isn't that always the best way anyway ? Thanks for your great videos .
This is my first time doing threads so this might be a dumb question. In the first example when you used the thread for myTurn how did the initial execution thread go to the next function why didn't both just stay on the same call and call myTurn twice . Is it just that it automatically moved to the next call. Like I would understand if the thread called yourTurn. This way the initial execution thread would still call myTurn but the second thread that was created would call yourTurn.
Simple and easy! Question for you. Using your code and compiling it with GCC, the program never terminates. myturn() and youturn() both execute until the end, but then the program just hangs until I use ctrl-C. I tried adding a return to the end, to no avail. I tried adding a printf test at the end of it all, but it doesn't print. I tried to add both functions a return. After finishing the threaded function, it just hangs there. Adding an exit(0) at the end of the myturn() function worked, but you didn't do that. Yet, your program terminates when you run it.
Found the problem! pthread_join(&newthread, NULL); That was the issue. On that one, I didn't have to pass the address of newthread, just newthread itself, like so: pthread_join(newthread, NULL); I was getting a warning at compilation, but didn't dig into it because I'm also eating chicken at the same time and am only typing with my non-greasy fingers. :| But yeah. Just the pthread_t for joining threads. Not the address.
Maybe a "cleaner" design would be to have the main thread creating 2 different threads - one running the "yourturn" function and one running "myturn" - , then joining both of them? Somehow, the asymmetry in calling one function within the main thread and one in a seperate thread, when both are conceptually doing the same (just with different strings) feels "wrong" to me. (Although I'm of course fully aware that this is just meant as a very basic example.)
Hey jacob, I was trying to combine your threads and signals videos and I've run into a question. Whenever I press + C, I noticed that the next iteration of the "my turn" loop starts without waiting the full 2 seconds. I hope that makes sense how I described it. In order to keep this from happening, I thought that making a separate thread with the signal handler would solve this, but it still looks like SIGINT triggers the "my turn" loop in main. I don't really have an idea why this would happen. It seems that the handler and main are two separate entities, so the signal should not have an effect on main. If you have a second I'd appreciate any explanation you have. Thanks!
How do I stop a single thread from terminating within its threaded function until a certain condition is met or until the parent thread calls pthreads_join?
Whats the difference between manual threading via threads.h/pthreads, and using OpenMP to do it for you? When should you use one or the other, what are the performance differences? Jacob Sorber
That depends very much on what sort of "embedded system" you're working with. If the core is a small linux single-board computer, then sure. For more constrained devices with a few KB of RAM, probably not.
I tried this, but in my case, using CYGWIN GCC, I had to flush out the stdout after printf, otherwise it did not print anything. If you can give some insight would be great.
yourturn is a regular sub executed under the main sub, and it s the only one, the myturn, become a thred, will continue to work, but this doesn't mean that the sub main in the principal thread will be waiting, code execution will continue in the sub. main .
can you help me to resolve this: write a program whose main thread creates a thread which reads an integer, given by the user, and returns it to the main thread which displays it
Are you reading the integer from the terminal? From a file? As far as returning it to the main thread, you could use a shared global variable, or the thread's result value (you can get it using pthread_join). What have you tried so far?
I accidentally crashed my computer once by running a program that I wrote which had an infitie loop which produced threads. This description of what I wrote isn't quite correct but you get the point of more or less what I did.
is it possible to do this in c programming, using dev c. i want to make a c program that can play music as the background while the program can still continue like normal, please help me
5:05 sorry I don't know how that analogy is supposed to help. Did you just wanted to have fun? Analogies are helpful for people trying to grasp abstract stuff and you could try to spend more time on them because I have no idea why you made that one
@@JacobSorber could you elaborate? because i did not understand the analogy, for me the thing u did with the thread is multithreading, i dont see the joining part
@@MMABeijing Sure. All I was doing was bringing the two threads together. When people draw threading diagrams, the join operation is where the threads come together (or are "joined" together). It's nothing more sophisticated than that.
C gifts (to the universe) : 1. Concurrency: is the same 2. Isolation: none 3.call stack: different 4.memory space : the same Thrrading is OS Dependent Win: createThreads Mac: posix Pthreads example plus bells and whistels woohoo!
you have to go over the functions properly, what is pthread_t? what is pthread_create? what are all the parameters? that's what i came to learn and i didn't get that. you just went over one example and with bare minimum information about it. it's just critique, i hope you implement in future videos.
pthread_join(&newthread,NULL) gives me error so i used pthread_join(newthread,NULL) and it works. Can anyone explain why it works here but does not work in my case. Thankyou.
It's a typo. It should be pthread_join(newthread, NULL);
@@JacobSorber Ty for the help. Maybe annotate this in the video so noobs like me dont get lost.
@@JacobSorber how can it be a typo and still run on your video? do you mean that the code window and the console window are not related at all??? it's all an illusion?
It's not an illusion, just a video editing mistake. When I make a 6-minute video, I usually start with 20-25 minutes of footage that I trim down-so things don't drag, and so it doesn't take me forever to upload. In this case, I think I accidentally edited out the part where it gave me an error and I fixed this. Sadly, UA-cam doesn't do video annotations anymore, and they don't let me update the video.
@@JacobSorber thanks
Amazing! Thanks bro. I spent 3 hours with my professor trying to explain that and he faild. You did it in 6 minutes! keep it up. Thanks
You're welcome. Glad it helped!
In 6 min I got what I I spent a full lab time to try to execute . Thank u.
I'm in the lab right now and feel the same
I searched about 12 videos looking for a cohesive explanation on pthreads before I found this one. Now I'm watching the whole series. This is the first video I've found that logically and procedurally explains pthreads and their uses. Great job. You earned a subscriber!
Thanks, and welcome to the channel.
You are a very good teacher. Thank you for this! looking forward to more videos about pthreads :)
I'm impressed with how simply you explained a topic that can be way complicated to explain. You are an excellent teacher! Thanks!
Hey
I’m at last year of my computer science bachelor .
I just want to say Thank youuuu for your videos they help me a lot ❤️
You're welcome. Glad I could help.
This was very useful and I appreciate you making this video. You seem like a wonderful teacher and am so grateful you're sharing your knowledge with the world.
I can't believe this video has thumbs down! Wishing you a great 2021 ahead!
If this channel doesnt get 1M subs till summer, I will be sad. Great videos Jacob!
Ok, I was looking for the definition for threads and processes. I looked soo hard but still couldn't understand.
I watched 3 mins of your video and all makes sense now!
Thanks:)
Love how people like you make programming easier to learn for the rest of us. Thanks!
Thanks a lot! Refreshing C, learn Zig and Rust and learning every single day a lot and increase the understanding of all. You are a great teacher!
JacobSorber is a great C language teacher. I’m learning lots of useful programming techniques. Thanks
You're welcome.
I remember having to write my own threading library in C with pthreads (and any other threading APIs) being totally off limits. The “metal” and I got very acquainted to say the least... It was a very valuable learning experience. I’m so thankful for the pthreads library now
Thank you for making this video. I will continue my journey by making a new search algorithm using this knowledge.
You really know the subject and concepts and I sincerely follow your tips and guidance. Many thanks for these useful videos.
Thank you. This made my understanding about threads so much clear.
Love your channel for quick interview prep.
I don't think you know how much I love you
Brilliant video! Can't wait for more!
Woah!you are awesome. Learnt the basics in just five mins!
Nice! Thank you for this. Hope you can continue for a long time to come!
Video request: pls make a video where you used parallelization to solve an algorithmic problem from say Google hash code. This will help in using this to solve real problems. Thanks.
what a amazing tutorial. You are the best. thank you for quality content
Great video! The second example is more intuitive.
No one has a Windows machine “handy”.
I only ever have a Windows machine lurking or skulking about.
That's definitely true for me.
They plot against us at night
Thanks, this was clear and straight to the point
Extremely powerful video
Amazing video, simple and easy.
You just help to do my C project. Thanks you!
hi Jacob, very good instructions. good teaching skills.
But, in this case i can only get it to work if i use ( instead of )
and i also had to change the last line to: pthread_join(newthread, NULL); ( instead of: pthread_join(&newthread, NULL); which is whats in the video).
I also had to compile with the -lpthread option like this: gcc -o thread thread.c -lpthread
just thought i share this, incase somebody else has the same issue. i use ubuntu 18.04 on a mac.
thanks
Thank you so much for making these videos.
My pleasure. I'm glad to think they're making a difference.
Most people: "I hated math in high school, why did I have to memorize the quadratic formula? What the heck is an imaginary number?"
Computer science majors: "...I have seen things. Things I can't unsee. Things like void *(*start_routine)(void*) ."
Loved the way you teaching 👍👌superb
thanks, you save me!
my professor make that a lot more difficult to understand at class :(
Same here, Sometimes Cool Profs like Jacob save our asses!
I hope this will be a good stepping stone for me to transition to odersky's parallel programming course on Coursera. That course really went over my head
Perfect, Perfect, Perfect, Perfect,Perfect, Perfect,Perfect,Perfect,Perfect,Perfect !!!
Better than any of my teachers
Awesome class, thanks a lot!
I suppose I am not the first one, but you remind me of Matthew Mcconaughey lmao. You're doing fantastic job with these tutorials. They've really come into use with my software-programming course in c, and really sparked the interest to learn more about c now that I start to come across the cool stuff in it through your videos (let's just say that the basics course for c wasn't the most intriguing part for me :D).
Thanks...I think. You're the first to share that with me. I don't sound like I'm from East Texas, do I? :)
Yeah, with so many viewers in so many different stages of learning, some videos are going to be too basic for some and too advanced for others. I'll try to keep mixing things up to keep it interesting for everyone.
Excellent work
Thank you so much for this useful video.
very helpful, thankyou!
Great Video, Well Understood👍.
Thank you, man😊
Amazing intuitive intro.
Glad you enjoyed it!
@@JacobSorber sorry for ping can i get to know which terminal are u using, i like the theme
@@DevFrogora In this video, I was using the platformio-ide-terminal plugin for atom. Using ZSH, with the ohmyzsh themes (I think it's the default theme)
good and sample explanation, Thanks!
GOLD! ty so much
More on threads and OS please and thank you
Excellent again
Hi Jacob,
I'm an embedded systems engineer and would like to do more work to understand threadding in Windows environement.
Any chance you could do a tutorial on MinGW / MSYS and to explain win32 vs posix threads and SEH vs SJLJ exception models, etc. that would be a guide for a newbie.
Kind regards, I love the videos
What is your programming environment setup? Do you have a video that goes over this? I my self use mobaxterm with vim or just visual studio code.
Instant subscribe!
Execellent introduction.
you earned a subscriber thank you!
Thanks! Welcome to the channel.
Really appreciate it!
you are the best !
Why do some examples I see pass in &start_routine and some pass in start_routine? Both seem to work.
Can we make a thread wait for other thread to complete it's execution by
pthread_join
Or it is specifically used in main function only
So is this all happening in parallel? The processor doesn't switch from thread1 to thread2 because of the sleep?
If you put the pthread_create function after the your_turn function would that not allow the my_turn function inside pthread_create to terminate before main terminated ? you'd then have output like "your turn" , "my turn my turn" , etc. etc. or would you just get "your turn , your turn , your turn" followed by a string of 5 "my turn" ? btw , pthread looks like a really bad idea ! Actually you know what ? I'm going to wait till I start coding C and just try things out and isn't that always the best way anyway ? Thanks for your great videos .
This is my first time doing threads so this might be a dumb question. In the first example when you used the thread for myTurn how did the initial execution thread go to the next function why didn't both just stay on the same call and call myTurn twice . Is it just that it automatically moved to the next call. Like I would understand if the thread called yourTurn. This way the initial execution thread would still call myTurn but the second thread that was created would call yourTurn.
Can you make a video on threadpools? Thanks for these!
You're welcome. I do talk about thread pools a bit in my multithreaded server series. ua-cam.com/video/FMNnusHqjpw/v-deo.html
@4:30 main done before thread
Is this relevant for c++ or is there a different more popular tool for it?
After creating a thread how long does it last? Until we call pthread_kill, pthread_cancel or pthread_join?
Yes, or until the thread function returns or calls pthread_exit
absolutely perfect
Thank you soo much...thanks alot
Simple and easy! Question for you. Using your code and compiling it with GCC, the program never terminates. myturn() and youturn() both execute until the end, but then the program just hangs until I use ctrl-C. I tried adding a return to the end, to no avail. I tried adding a printf test at the end of it all, but it doesn't print. I tried to add both functions a return. After finishing the threaded function, it just hangs there. Adding an exit(0) at the end of the myturn() function worked, but you didn't do that. Yet, your program terminates when you run it.
Found the problem!
pthread_join(&newthread, NULL);
That was the issue. On that one, I didn't have to pass the address of newthread, just newthread itself, like so:
pthread_join(newthread, NULL);
I was getting a warning at compilation, but didn't dig into it because I'm also eating chicken at the same time and am only typing with my non-greasy fingers. :|
But yeah. Just the pthread_t for joining threads. Not the address.
Maybe a "cleaner" design would be to have the main thread creating 2 different threads - one running the "yourturn" function and one running "myturn" - , then joining both of them?
Somehow, the asymmetry in calling one function within the main thread and one in a seperate thread, when both are conceptually doing the same (just with different strings) feels "wrong" to me.
(Although I'm of course fully aware that this is just meant as a very basic example.)
Sir why do you use clang.iam using gcc .does clang has better libraries i think both have same number of libries and advantages
Thread is one the scary topic to me. Thank you for explaining it such an easy way 😊
Hey jacob, I was trying to combine your threads and signals videos and I've run into a question.
Whenever I press + C, I noticed that the next iteration of the "my turn" loop starts without waiting the full 2 seconds. I hope that makes sense how I described it. In order to keep this from happening, I thought that making a separate thread with the signal handler would solve this, but it still looks like SIGINT triggers the "my turn" loop in main. I don't really have an idea why this would happen. It seems that the handler and main are two separate entities, so the signal should not have an effect on main. If you have a second I'd appreciate any explanation you have. Thanks!
How do I stop a single thread from terminating within its threaded function until a certain condition is met or until the parent thread calls pthreads_join?
Thank you so much for this amazing video. It saved me a lot of stress!
Whats the difference between manual threading via threads.h/pthreads, and using OpenMP to do it for you?
When should you use one or the other, what are the performance differences?
Jacob Sorber
Hi Jacob, will pthread run in embedded systems?
That depends very much on what sort of "embedded system" you're working with. If the core is a small linux single-board computer, then sure. For more constrained devices with a few KB of RAM, probably not.
I tried this, but in my case, using CYGWIN GCC, I had to flush out the stdout after printf, otherwise it did not print anything. If you can give some insight would be great.
Thank you
I'm using your video to learning english😂😂, and programming skills
you are awesome, thanks.
You're welcome. Thanks for watching.
awesome thanks!
@5:04 Couldn't understand the metaphor !
what kinda font do you use? and vs code theme?
Thanks, now I understand:)
thanks a lot
Can someone explain to me how the "yourturn" function was also consistently being repeated while the thread function only included *myturn ?
yourturn is a regular sub executed under the main sub, and it s the only one, the myturn, become a thred, will continue to work, but this doesn't mean that the sub main in the principal thread will be waiting, code execution will continue in the sub. main .
you're the best
Thanks. 😀
awesome vid
can you help me to resolve this:
write a program whose main thread creates a thread which reads an integer, given by the user, and returns it to the main thread which displays it
Are you reading the integer from the terminal? From a file? As far as returning it to the main thread, you could use a shared global variable, or the thread's result value (you can get it using pthread_join).
What have you tried so far?
Ooh Metaphors are so Fun
I accidentally crashed my computer once by running a program that I wrote which had an infitie loop which produced threads. This description of what I wrote isn't quite correct but you get the point of more or less what I did.
how do I install 3rd party libraries for codeblocks? is there any tutorial?
I don't use codeblocks. So, hopefully, someone else can chime in.
thread.c:15:22: warning: unused parameter 'arg' [-Wunused-parameter]
void* myTurn (void * arg) {
Paramater void *myTurn (void * arg) not use, Why?
Thank you, Could you give real world examples of threads ? Learning how they work is good, but even better if you apply them to real world projects.
Check out my multithreaded server videos.
is it possible to do this in c programming, using dev c.
i want to make a c program that can play music as the background while the program can still continue like normal, please help me
5:05 sorry I don't know how that analogy is supposed to help. Did you just wanted to have fun? Analogies are helpful for people trying to grasp abstract stuff and you could try to spend more time on them because I have no idea why you made that one
It's simply my attempt to guess at why they call the function "join", instead of something like wait_for_thread_to_finish_and_get_its_result.
@@JacobSorber could you elaborate? because i did not understand the analogy, for me the thing u did with the thread is multithreading, i dont see the joining part
@@MMABeijing Sure. All I was doing was bringing the two threads together. When people draw threading diagrams, the join operation is where the threads come together (or are "joined" together). It's nothing more sophisticated than that.
the intro music 00:10 ?
C gifts (to the universe) :
1. Concurrency: is the same
2. Isolation: none
3.call stack: different
4.memory space : the same
Thrrading is OS Dependent
Win: createThreads
Mac: posix
Pthreads example plus bells and whistels woohoo!
you have to go over the functions properly, what is pthread_t? what is pthread_create? what are all the parameters? that's what i came to learn and i didn't get that. you just went over one example and with bare minimum information about it. it's just critique, i hope you implement in future videos.