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

Поділитися
Вставка
  • Опубліковано 31 січ 2025

КОМЕНТАРІ • 170

  • @biswas809
    @biswas809 5 років тому +99

    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
      @JacobSorber  5 років тому +84

      It's a typo. It should be pthread_join(newthread, NULL);

    • @navyjosh1
      @navyjosh1 4 роки тому +11

      @@JacobSorber Ty for the help. Maybe annotate this in the video so noobs like me dont get lost.

    • @GustavoMontanha
      @GustavoMontanha 4 роки тому +7

      @@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?

    • @JacobSorber
      @JacobSorber  4 роки тому +64

      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.

    • @GustavoMontanha
      @GustavoMontanha 4 роки тому

      @@JacobSorber thanks

  • @youssefgerges9078
    @youssefgerges9078 3 роки тому +6

    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

    • @JacobSorber
      @JacobSorber  3 роки тому +1

      You're welcome. Glad it helped!

  • @joe2jamaly
    @joe2jamaly 5 років тому +29

    In 6 min I got what I I spent a full lab time to try to execute . Thank u.

  • @carolinaSolid19
    @carolinaSolid19 5 років тому +2

    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!

    • @JacobSorber
      @JacobSorber  5 років тому +1

      Thanks, and welcome to the channel.

  • @intelignet
    @intelignet 6 років тому +61

    You are a very good teacher. Thank you for this! looking forward to more videos about pthreads :)

  • @martinpichardo3985
    @martinpichardo3985 10 місяців тому

    I'm impressed with how simply you explained a topic that can be way complicated to explain. You are an excellent teacher! Thanks!

  • @nilou-bt
    @nilou-bt 2 роки тому +2

    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 ❤️

    • @JacobSorber
      @JacobSorber  2 роки тому +1

      You're welcome. Glad I could help.

  • @user-ry1ku2zr4l
    @user-ry1ku2zr4l 4 роки тому +6

    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!

  • @danielskala7956
    @danielskala7956 3 роки тому

    If this channel doesnt get 1M subs till summer, I will be sad. Great videos Jacob!

  • @applenews9249
    @applenews9249 4 роки тому +4

    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:)

  • @denebvegaaltair1146
    @denebvegaaltair1146 3 роки тому +1

    Love how people like you make programming easier to learn for the rest of us. Thanks!

  • @alexanderzikal7244
    @alexanderzikal7244 5 місяців тому

    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!

  • @frankgomez9556
    @frankgomez9556 3 роки тому +1

    JacobSorber is a great C language teacher. I’m learning lots of useful programming techniques. Thanks

  • @TMerica199
    @TMerica199 4 роки тому +6

    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

  • @TsubasaKato
    @TsubasaKato 4 роки тому +1

    Thank you for making this video. I will continue my journey by making a new search algorithm using this knowledge.

  • @saveplanet3977
    @saveplanet3977 3 роки тому

    You really know the subject and concepts and I sincerely follow your tips and guidance. Many thanks for these useful videos.

  • @thiwankapathirana9532
    @thiwankapathirana9532 Рік тому

    Thank you. This made my understanding about threads so much clear.

  • @santoshsco
    @santoshsco 4 роки тому

    Love your channel for quick interview prep.

  • @juanperret7044
    @juanperret7044 4 роки тому

    I don't think you know how much I love you

  • @jondabomb2184
    @jondabomb2184 6 років тому +2

    Brilliant video! Can't wait for more!

  • @smh_18
    @smh_18 4 роки тому

    Woah!you are awesome. Learnt the basics in just five mins!

  • @outrotipo4193
    @outrotipo4193 6 років тому

    Nice! Thank you for this. Hope you can continue for a long time to come!

  • @chiragr1336
    @chiragr1336 4 роки тому +9

    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.

  • @kavindudananjaya3290
    @kavindudananjaya3290 5 років тому +1

    what a amazing tutorial. You are the best. thank you for quality content

  • @luizfernandorabelo5570
    @luizfernandorabelo5570 3 роки тому +1

    Great video! The second example is more intuitive.

  • @askhowiknow5527
    @askhowiknow5527 5 років тому +56

    No one has a Windows machine “handy”.
    I only ever have a Windows machine lurking or skulking about.

  • @domste
    @domste 3 роки тому

    Thanks, this was clear and straight to the point

  • @globaltechmastery2393
    @globaltechmastery2393 2 роки тому

    Extremely powerful video

  • @shantanu83
    @shantanu83 2 роки тому

    Amazing video, simple and easy.

  • @longtranbao4177
    @longtranbao4177 4 роки тому

    You just help to do my C project. Thanks you!

  • @jamesnewton6139
    @jamesnewton6139 4 роки тому +9

    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

  • @sakurira
    @sakurira 4 роки тому

    Thank you so much for making these videos.

    • @JacobSorber
      @JacobSorber  4 роки тому

      My pleasure. I'm glad to think they're making a difference.

  • @stapler942
    @stapler942 3 роки тому +1

    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*) ."

  • @sagarkhairnar568
    @sagarkhairnar568 3 роки тому

    Loved the way you teaching 👍👌superb

  • @rodrigodisselli4532
    @rodrigodisselli4532 5 років тому +3

    thanks, you save me!
    my professor make that a lot more difficult to understand at class :(

    • @mockingbird3809
      @mockingbird3809 4 роки тому

      Same here, Sometimes Cool Profs like Jacob save our asses!

  • @evenaicantfigurethisout
    @evenaicantfigurethisout 2 роки тому

    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

  • @mohamadfawaz4146
    @mohamadfawaz4146 2 роки тому

    Perfect, Perfect, Perfect, Perfect,Perfect, Perfect,Perfect,Perfect,Perfect,Perfect !!!

  • @thepickicool97
    @thepickicool97 4 роки тому

    Better than any of my teachers

  • @lt_erwin
    @lt_erwin 2 роки тому

    Awesome class, thanks a lot!

  • @juusokorhonen1628
    @juusokorhonen1628 5 років тому

    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).

    • @JacobSorber
      @JacobSorber  5 років тому +1

      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.

  • @Asiaashor
    @Asiaashor Рік тому

    Excellent work

  • @amrtcpp6203
    @amrtcpp6203 4 роки тому

    Thank you so much for this useful video.

  • @Webcomentarista_Aditivo
    @Webcomentarista_Aditivo 2 роки тому

    very helpful, thankyou!

  • @muhammadzakiahmad8069
    @muhammadzakiahmad8069 Рік тому

    Great Video, Well Understood👍.

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 6 місяців тому

    Thank you, man😊

  • @Sahilbc-wj8qk
    @Sahilbc-wj8qk 6 років тому +1

    Amazing intuitive intro.

    • @JacobSorber
      @JacobSorber  6 років тому

      Glad you enjoyed it!

    • @DevFrogora
      @DevFrogora 5 років тому

      @@JacobSorber sorry for ping can i get to know which terminal are u using, i like the theme

    • @JacobSorber
      @JacobSorber  5 років тому

      @@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)

  • @cheexiong8759
    @cheexiong8759 3 роки тому

    good and sample explanation, Thanks!

  • @danielepadrini6731
    @danielepadrini6731 2 роки тому

    GOLD! ty so much

  • @eddydecolombia
    @eddydecolombia 3 роки тому

    More on threads and OS please and thank you

  • @pathayes2224
    @pathayes2224 3 роки тому

    Excellent again

  • @danielwait8555
    @danielwait8555 Рік тому

    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

  • @rudyku-cejin288
    @rudyku-cejin288 Рік тому

    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.

  • @touseef_akhtar
    @touseef_akhtar Рік тому

    Instant subscribe!

  • @kushagrakumar9576
    @kushagrakumar9576 6 років тому

    Execellent introduction.

  • @sandeeparathnayake9427
    @sandeeparathnayake9427 4 роки тому

    you earned a subscriber thank you!

    • @JacobSorber
      @JacobSorber  4 роки тому +1

      Thanks! Welcome to the channel.

  • @rhinoshop2169
    @rhinoshop2169 4 роки тому

    Really appreciate it!

  • @yjc149
    @yjc149 2 роки тому

    you are the best !

  • @yospa5074
    @yospa5074 5 місяців тому

    Why do some examples I see pass in &start_routine and some pass in start_routine? Both seem to work.

  • @randomsoul00
    @randomsoul00 Рік тому

    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

  • @WaldoTheWombat
    @WaldoTheWombat Рік тому

    So is this all happening in parallel? The processor doesn't switch from thread1 to thread2 because of the sleep?

  • @edwardmacnab354
    @edwardmacnab354 2 роки тому

    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 .

  • @bossman4112
    @bossman4112 3 роки тому

    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.

  • @laibamustafa108
    @laibamustafa108 4 роки тому

    Can you make a video on threadpools? Thanks for these!

    • @JacobSorber
      @JacobSorber  4 роки тому

      You're welcome. I do talk about thread pools a bit in my multithreaded server series. ua-cam.com/video/FMNnusHqjpw/v-deo.html

  • @logicboard7746
    @logicboard7746 2 роки тому +1

    @4:30 main done before thread

  • @WaldoTheWombat
    @WaldoTheWombat 2 роки тому

    Is this relevant for c++ or is there a different more popular tool for it?

  • @pranavrao6370
    @pranavrao6370 3 роки тому

    After creating a thread how long does it last? Until we call pthread_kill, pthread_cancel or pthread_join?

    • @JacobSorber
      @JacobSorber  3 роки тому

      Yes, or until the thread function returns or calls pthread_exit

  • @loranceluo2497
    @loranceluo2497 5 років тому

    absolutely perfect

  • @srivanikothavadla4606
    @srivanikothavadla4606 4 роки тому

    Thank you soo much...thanks alot

  • @wandererstraining
    @wandererstraining 4 роки тому

    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.

    • @wandererstraining
      @wandererstraining 4 роки тому

      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.

  • @Uerdue
    @Uerdue 2 роки тому

    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.)

  • @chintusharma1
    @chintusharma1 3 роки тому

    Sir why do you use clang.iam using gcc .does clang has better libraries i think both have same number of libries and advantages

  • @khalidsaifullahfuad
    @khalidsaifullahfuad 3 роки тому

    Thread is one the scary topic to me. Thank you for explaining it such an easy way 😊

  • @brooksburris8341
    @brooksburris8341 3 роки тому

    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!

  • @pranavrao6370
    @pranavrao6370 3 роки тому

    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?

  • @mockingbird3809
    @mockingbird3809 4 роки тому

    Thank you so much for this amazing video. It saved me a lot of stress!

  • @shekharmaela2308
    @shekharmaela2308 4 роки тому

    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

  • @jonathangainsford6811
    @jonathangainsford6811 3 роки тому

    Hi Jacob, will pthread run in embedded systems?

    • @JacobSorber
      @JacobSorber  3 роки тому

      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.

  • @jonaskyman
    @jonaskyman 3 роки тому

    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.

  • @manualchnn
    @manualchnn 5 років тому

    Thank you

  • @meluobote7664
    @meluobote7664 Рік тому

    I'm using your video to learning english😂😂, and programming skills

  • @rshrewd8809
    @rshrewd8809 5 років тому

    you are awesome, thanks.

    • @JacobSorber
      @JacobSorber  5 років тому

      You're welcome. Thanks for watching.

  • @MasterRazz3
    @MasterRazz3 5 років тому

    awesome thanks!

  • @muaz1236
    @muaz1236 4 роки тому +1

    @5:04 Couldn't understand the metaphor !

  • @SIGMA_BLYAT
    @SIGMA_BLYAT 2 роки тому

    what kinda font do you use? and vs code theme?

  • @jireff
    @jireff 5 років тому

    Thanks, now I understand:)

  •  4 роки тому

    thanks a lot

  • @fevering8120
    @fevering8120 4 роки тому

    Can someone explain to me how the "yourturn" function was also consistently being repeated while the thread function only included *myturn ?

    • @phoenixhistory5433
      @phoenixhistory5433 4 роки тому

      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 .

  • @JYSERroshanseeam
    @JYSERroshanseeam 4 роки тому

    you're the best

  • @videowatcher4852
    @videowatcher4852 5 років тому

    awesome vid

  • @saadileila9331
    @saadileila9331 5 років тому

    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

    • @JacobSorber
      @JacobSorber  5 років тому

      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?

  • @sadowizzy1105
    @sadowizzy1105 5 років тому +1

    Ooh Metaphors are so Fun

  • @smrtfasizmu6161
    @smrtfasizmu6161 2 роки тому

    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.

  • @manumonkey2712
    @manumonkey2712 3 роки тому

    how do I install 3rd party libraries for codeblocks? is there any tutorial?

    • @JacobSorber
      @JacobSorber  3 роки тому

      I don't use codeblocks. So, hopefully, someone else can chime in.

  • @dwozk147
    @dwozk147 3 роки тому

    thread.c:15:22: warning: unused parameter 'arg' [-Wunused-parameter]
    void* myTurn (void * arg) {
    Paramater void *myTurn (void * arg) not use, Why?

  • @mathssoso4261
    @mathssoso4261 3 роки тому

    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.

    • @JacobSorber
      @JacobSorber  3 роки тому

      Check out my multithreaded server videos.

  • @jos3008
    @jos3008 4 роки тому

    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

  • @MMABeijing
    @MMABeijing 5 років тому

    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
      @JacobSorber  5 років тому

      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.

    • @MMABeijing
      @MMABeijing 5 років тому

      @@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

    • @JacobSorber
      @JacobSorber  5 років тому

      @@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.

  • @ricksanchez4736
    @ricksanchez4736 Рік тому

    the intro music 00:10 ?

  • @alute5532
    @alute5532 Рік тому

    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!

  • @aniruddharora4004
    @aniruddharora4004 3 роки тому

    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.