How to pass arguments to and get results from threads. (pthread_create, pthread_join)

Sdílet
Vložit
  • čas přidán 9. 09. 2024
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.th...
    Website ➤ www.jacobsorbe...
    ---
    This video builds on the last one, which showed you how to create threads. ( • How to create and join... )
    This video shows you how to pass arguments to your new threads and get the results that those threads produce.
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    www.jacobsorbe...
    people.cs.clem...
    persist.cs.clem...
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/...]
    Source code is also available to Patreon supporters. --- [jsorber-youtub...]
    Want me to review your code?
    Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
    You can also find more info about code reviews here.
    • I want to review your ...

Komentáře • 84

  • @linuxinstalled
    @linuxinstalled Před 4 lety +59

    This saved me in a class project. It's so nice to be able to see and hear someone talk about how to use pthreads rather than trying to interpret a man page. Thanks again!

  • @samvelabrahamyan7230
    @samvelabrahamyan7230 Před 5 lety +34

    Thank you for the pure, concise content. Your channel deserves much more views and fame.

    • @JacobSorber
      @JacobSorber  Před 5 lety +9

      You're welcome. I won't complain if you tell everyone you know about the channel. :)

    • @bhargavpatel4781
      @bhargavpatel4781 Před 3 lety

      @@JacobSorber sir how to return pointer from thread in c++

  • @sagejpc1175
    @sagejpc1175 Před 9 měsíci

    I want to say thank you from the bottom of my heart, this video saved me on a class project.
    My professor in operating systems couldn't teach this concept in 80 minutes of lecture, you did it in less than 5.

  • @MrAitSous
    @MrAitSous Před 5 měsíci

    Perfect explanation. Saved me for class assignment.

  • @chillsway9550
    @chillsway9550 Před 3 lety +3

    Thank you so much for your videos they are so helpful and even better than some professors who just read off of PowerPoint slides

  • @dirtymint
    @dirtymint Před 4 lety +14

    Should free() be called on the int* result pointer? Not trying to be 'that guy', Im just trying to get my head around taking and giving back memory.

    • @JacobSorber
      @JacobSorber  Před 4 lety +9

      No worries. Yeah, probably. If this were a long-running program, someone needs to free that memory. I was focused mostly on getting the pointer back from the thread, not on what you should do with it once you get it. :) Also, you might want to check out another video of mine about how some common short-lived programs don't bother to free their memory. czcams.com/video/i6qgTWUMNXc/video.html

    • @angelcaru
      @angelcaru Před 3 měsíci +1

      @@JacobSorber I love using the OS as a garbage collector :P

  • @abhigyanganguly1988
    @abhigyanganguly1988 Před 2 lety

    Man, you literally saved my OS Assignment!!! Forever grateful for this!!!

  • @aliyildirim3640
    @aliyildirim3640 Před 2 lety

    This saved me from so much headache for my OS course, thank you so much!

  • @wandererstraining
    @wandererstraining Před 4 lety +3

    Now, can we get rid the the compilation warnings about incompatible pointer types? Yes we can! It took me a little bit to figure it out, but here's my solution! :)
    int *result;
    pthread_join(thread1, &result);
    printf("%d
    ", *result);
    This code issues a warning because of incompatible pointers type. The function pthread_join outputs a void type of pointer, but the pointer we store the result in is an integer type of pointer. So the solution is to accept it as a void type of pointer then cast it to the right type before printing it, just like we did for passing the argument:
    void *result
    pthread_join(thread1, &result);
    int *iresult = (int *) result;
    printf("%d
    ", *iresult);
    There! No more warnings.

    • @jamesnewton6139
      @jamesnewton6139 Před 3 lety +1

      what i did and this worked for me is change this line:
      pthread_join(newthread, &result);
      to this:
      pthread_join(newthread, (void *)&result);

  • @morauy3481
    @morauy3481 Před rokem +2

    2:23
    int *iptr = (int*) malloc(sizeof(int)) can lead to seg faults
    u need: malloc(sizeof(int*)) because int and int* are not always the same size
    x64 4 and 8 for example
    but nice video

  • @outrotipo4193
    @outrotipo4193 Před 5 lety +2

    Happy holidays to you too! Thank you for the great videos!

  • @haticeaydn3729
    @haticeaydn3729 Před 2 lety

    YOU SAVED MY SEMESTER

  • @alantao3810
    @alantao3810 Před 4 lety

    Massively underrated youtube channel. You seriously deserve more views man. Good job!

  • @poporkine
    @poporkine Před 3 lety +8

    At 3:18, shouldn't the cast be (void **) ? Because result is a pointer to int, and &result is a pointer to a pointer.

    • @blvckbytes7329
      @blvckbytes7329 Před 2 lety

      It's a pointer to a pointer, the API doesn't care about the details. A void pointer just means a pointer of unspecified type. Could be anything, including a int**.

    • @renpnal229
      @renpnal229 Před 2 lety +1

      The pthread_join function expects a void * * as it's second argument and a warning will be given if one gives it an int * *. This is what happens in the video. The way I would fix it is to cast the int * * typed value &result to void * *. However in the video it is cast to void *. void * has the property that it can be implicitly cast to and from anything so the void * typed value (void *) &result is implicitly cast to void * *. EDIT: had to add spaces between asterisks because markdown is stupid.

    • @amos660
      @amos660 Před 2 lety

      @@renpnal229 I just tried with your suggestion.. it works, thanks!

  • @nikhilbhat8443
    @nikhilbhat8443 Před 4 lety +2

    Hey, it was a great video. I liked the way you introduced the void* concept. It would be more clear if you explained the code. I mean, why the output, when we used threads, is how it is. Thanks for the video again!!

  • @noahmullane7585
    @noahmullane7585 Před 3 lety

    2021, Thank you for making this video!

  • @Partooooon
    @Partooooon Před 5 lety +3

    Thanks for the great videos! Definitely recommending them.

  • @chiragr1336
    @chiragr1336 Před 4 lety +3

    Please make thread videos to use in solving competitive programming problems.

    • @kennethdang4446
      @kennethdang4446 Před 2 lety

      Lmao, you like competitive programming? Should hop on to Art of Problem solving. I feel like they might have something there.

  • @Diogenes20111
    @Diogenes20111 Před 4 lety +1

    Thanks a lot, your video saved me a lot of time in catching a silly mistake!

  • @mehrmalik5860
    @mehrmalik5860 Před 4 lety +1

    please just slow down your speed that's all what i want,your explanation is best.

    • @JacobSorber
      @JacobSorber  Před 4 lety

      Noted. Thanks. I may redo this one at some point in the future, but for now, you can play videos back at reduced speed.

  • @vishakamohan5336
    @vishakamohan5336 Před 3 lety

    Thanks so much! Helped me a lot

  • @user-cp3tm2nx5l
    @user-cp3tm2nx5l Před 7 měsíci

    In 3:42 the second argument for pthread_join is a void pointer to a pointer to a pointer of an int : (Void*) &result.
    I don’t get how that doesn’t create an error, we only dealt in double pointer and not triple pointers so far .

  • @erbenton07
    @erbenton07 Před rokem

    Jacob, do this same video but with multiple threads (loop through main) and using pthread_attrubute.

  • @supernenechi
    @supernenechi Před 4 lety

    I am learning this for C because I tried it in Go and it was so easy, but Go is really slow with doing math related stuff apparently

  • @saranikolic2283
    @saranikolic2283 Před 3 lety

    Thanks a loooot!! :))
    Greetings from Russia.

  • @amrtcpp6203
    @amrtcpp6203 Před 4 lety

    Thank you for the great videos!

  • @nandharamya9612
    @nandharamya9612 Před 3 lety

    last bit of code is throwing compilation error .so here is my corrected code for any one who faces compilation error and segmentation fault
    void* ret;
    pthread_t newthread;
    pthread_create(&newthread,NULL,display,NULL);
    display1();
    pthread_join(newthread,&ret);
    cout

  • @ertugrulyilmaz5090
    @ertugrulyilmaz5090 Před 4 lety

    You are the best

  • @egehurturk6209
    @egehurturk6209 Před 3 lety

    Can you show an example of how to turn a thread into a daemon process in UNIX? That would be great!

  • @victorzedwings
    @victorzedwings Před 2 lety

    Such a nice katunka of blue threads!
    It's gonna be of use with good jeans!

  • @sarahross7515
    @sarahross7515 Před rokem

    why would you use a pointer to a pointer (void *)&result?
    isn't int *result pointing to a specific location?
    prolly its a silly question
    haven't touched c in a while

  • @NotMarkKnopfler
    @NotMarkKnopfler Před rokem

    Has the int that was malloc'd in the thread effectively gone out of scope by the time it's de-refenced in the 'main' thread? (I'm being pedantic - I know it's a contrived example, I'm just curious!). Thanks 👍 Would a better design pattern be to pass a pointer _in_ to the thread and effectively say "hey, put the result in here" - then it's still in scope when the thread exits. Again, just curious.

  • @terryblack3625
    @terryblack3625 Před 3 lety

    this tutorial put it in a nutshell

  • @sdgdfhdfgh
    @sdgdfhdfgh Před 2 lety

    is this good technique you are passing local variables address to the thread arguments ?

  • @stephenworrall4612
    @stephenworrall4612 Před rokem

    i really do appreciate the breaking down of the concepts into digestable chunks... but you need to talk slower so it's easier to follow! I'm having to go to 0.5 speed to follow your coding
    I also have to keep rewinding the video to be able to see the code I missed because I was coding myself. the shot scrolled out or you scrolled away from what was done (if that makes any sense? haha) maybe just record your whole screen and not a cropped in shot?

    • @JacobSorber
      @JacobSorber  Před rokem

      I get that a lot on my older videos. Is the pacing in the newer stuff working better for you?

  • @momq1434
    @momq1434 Před rokem

    why the pointer does not need to be freed?

  • @Slomeus
    @Slomeus Před 2 lety

    Is it possible to pass more than one argument to a thread?

  • @paulohenriquepatarello2163

    What is the IDE that you are using?

  • @TheChewedGaming
    @TheChewedGaming Před 3 lety +1

    is there a way to pass multiple arguments for threads?

    • @maddjhdhdhdhd6917
      @maddjhdhdhdhd6917 Před 3 lety +1

      No but you can pass them in a struct. and then make cast inside the routine function.

  • @mitultyagi3357
    @mitultyagi3357 Před 4 lety

    What about multiple threads returning the data. How to get the return value?

    • @wandererstraining
      @wandererstraining Před 4 lety

      Use multiple return values then. Perhaps using an array to keep track of the threads and an array of pointers?

  • @savarkars1403
    @savarkars1403 Před 3 lety

    Shouldnt that heap allocated memory be freed somewhere ?

    • @jorgeferreira6727
      @jorgeferreira6727 Před 2 lety

      In this specific code example, it doesn't matter. The program is existing, so all memory, heap or other, will be discarded. A full clean up. ;)

  • @ibrahimunal1206
    @ibrahimunal1206 Před 4 lety +1

    I'm celebrating coronavirus vacatıon and stuck wıth OS assignment whıich wants me to create a multıthreaded program wıth C thanks for the video btw

    • @JacobSorber
      @JacobSorber  Před 4 lety +2

      Sounds like a great use of a quarantine. I hope it all ended well.

  • @chaudharyabdulrayan5890

    Im trying to return pointer array. Can you guide me?

    • @JacobSorber
      @JacobSorber  Před 3 lety

      Allocate space on the heap for the array of pointers (using malloc, calloc, or realloc). The function will return a pointer to the allocated block of memory. Then you return the pointer from the thread function, and grab it with pthread_join.

  • @MrSardine17
    @MrSardine17 Před 3 lety

    One of those inexplicable mysteries of the universe - why doesn't the pthreads library have a producer-consumer queue, but does have join() :((

  • @soutrikmaiti4133
    @soutrikmaiti4133 Před 3 lety

    Maybe I am missing something, I am not sure why I am getting Segmentation fault.

    • @JacobSorber
      @JacobSorber  Před 3 lety +1

      Not sure. I don't have enough information. Probably a bad pointer somewhere. Maybe check out my video about tracking down segfaults in your debugger.

    • @soutrikmaiti4133
      @soutrikmaiti4133 Před 3 lety

      @@JacobSorber Thanks I will check that video out and try to debug my code.

  • @LARathbone
    @LARathbone Před 4 lety

    Did you cast the return of malloc at 2:12? Say it ain't so!

    • @JacobSorber
      @JacobSorber  Před 4 lety

      If it wasn't recorded, I wouldn't believe it myself. 🤔Old habits die...no sorry. This behavior can't be excused. 😬

    • @LARathbone
      @LARathbone Před 4 lety

      @@JacobSorber k&r 2nd Ed specifically tells you to do this which threw me off for a while :/
      I was kidding when I made fun of you for doing it though- I think you were editing your prior code that had the cast and you kept it in by mistake!

    • @JacobSorber
      @JacobSorber  Před 4 lety +1

      @@LARathbone No worries. I was joking, too. I really don't worry too much about things like that. If that's the worst thing I do on a video, then it's been a good day. :)

  • @ksk8080-d6d
    @ksk8080-d6d Před 4 lety +1

    Hi Jacob, This is awesome. But please dont use the background music

  • @singularity3555
    @singularity3555 Před 2 lety

    You move really fast from step to step. Little difficult to keep up, pause and rewind so much lol

  • @zehraortak7733
    @zehraortak7733 Před 4 lety

    SOURCE CODE:
    #include
    #include
    #include
    void * myturn(void * arg){
    int *iptr =(int *)malloc(sizeof(int));
    *iptr=5;
    for(int i=0;i

    • @zehraortak7733
      @zehraortak7733 Před 4 lety

      and also thank u jacob for this tutorial. It really helped me about operating system hw :)

  • @mgu3241
    @mgu3241 Před 2 lety

    why tf is the music so loud dawg