How to pass arguments to and get results from threads. (pthread_create, pthread_join)
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 ...
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!
Thank you for the pure, concise content. Your channel deserves much more views and fame.
You're welcome. I won't complain if you tell everyone you know about the channel. :)
@@JacobSorber sir how to return pointer from thread in c++
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.
Perfect explanation. Saved me for class assignment.
Thank you so much for your videos they are so helpful and even better than some professors who just read off of PowerPoint slides
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.
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
@@JacobSorber I love using the OS as a garbage collector :P
Man, you literally saved my OS Assignment!!! Forever grateful for this!!!
This saved me from so much headache for my OS course, thank you so much!
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.
what i did and this worked for me is change this line:
pthread_join(newthread, &result);
to this:
pthread_join(newthread, (void *)&result);
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
Happy holidays to you too! Thank you for the great videos!
YOU SAVED MY SEMESTER
Wonderful. Glad I could help.
Massively underrated youtube channel. You seriously deserve more views man. Good job!
Thanks, Alan.
At 3:18, shouldn't the cast be (void **) ? Because result is a pointer to int, and &result is a pointer to a pointer.
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**.
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.
@@renpnal229 I just tried with your suggestion.. it works, thanks!
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!!
2021, Thank you for making this video!
Thanks for the great videos! Definitely recommending them.
Thanks, Eric.
Please make thread videos to use in solving competitive programming problems.
Lmao, you like competitive programming? Should hop on to Art of Problem solving. I feel like they might have something there.
Thanks a lot, your video saved me a lot of time in catching a silly mistake!
You're welcome. Glad I could help.
please just slow down your speed that's all what i want,your explanation is best.
Noted. Thanks. I may redo this one at some point in the future, but for now, you can play videos back at reduced speed.
Thanks so much! Helped me a lot
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 .
Jacob, do this same video but with multiple threads (loop through main) and using pthread_attrubute.
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
Thanks a loooot!! :))
Greetings from Russia.
Thank you for the great videos!
Welcome. Glad you liked it.
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
You are the best
Can you show an example of how to turn a thread into a daemon process in UNIX? That would be great!
Such a nice katunka of blue threads!
It's gonna be of use with good jeans!
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
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.
this tutorial put it in a nutshell
is this good technique you are passing local variables address to the thread arguments ?
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?
I get that a lot on my older videos. Is the pacing in the newer stuff working better for you?
why the pointer does not need to be freed?
Is it possible to pass more than one argument to a thread?
What is the IDE that you are using?
is there a way to pass multiple arguments for threads?
No but you can pass them in a struct. and then make cast inside the routine function.
What about multiple threads returning the data. How to get the return value?
Use multiple return values then. Perhaps using an array to keep track of the threads and an array of pointers?
Shouldnt that heap allocated memory be freed somewhere ?
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. ;)
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
Sounds like a great use of a quarantine. I hope it all ended well.
Im trying to return pointer array. Can you guide me?
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.
One of those inexplicable mysteries of the universe - why doesn't the pthreads library have a producer-consumer queue, but does have join() :((
Maybe I am missing something, I am not sure why I am getting Segmentation fault.
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.
@@JacobSorber Thanks I will check that video out and try to debug my code.
Did you cast the return of malloc at 2:12? Say it ain't so!
If it wasn't recorded, I wouldn't believe it myself. 🤔Old habits die...no sorry. This behavior can't be excused. 😬
@@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!
@@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. :)
Hi Jacob, This is awesome. But please dont use the background music
You move really fast from step to step. Little difficult to keep up, pause and rewind so much lol
SOURCE CODE:
#include
#include
#include
void * myturn(void * arg){
int *iptr =(int *)malloc(sizeof(int));
*iptr=5;
for(int i=0;i
and also thank u jacob for this tutorial. It really helped me about operating system hw :)
why tf is the music so loud dawg