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

preview_player
Показать описание
---

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:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

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!

linuxinstalled
Автор

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.

dirtymint
Автор

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

samvelabrahamyan
Автор

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

poporkine
Автор

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\n", *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\n", *iresult);

There! No more warnings.

wandererstraining
Автор

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

morauy
Автор

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

chillsway
Автор

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

nikhilbhat
Автор

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

chiragr
Автор

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

ibrahimunal
Автор

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

sarahross
Автор

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 .

JacemHagui
Автор

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

outrotipo
Автор

Thanks for the great videos! Definitely recommending them.

Partooooon
Автор

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<<"data ="<<*((int*)ret)<<endl;

nandharamya
Автор

SOURCE CODE:


#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

void * myturn(void * arg){
int *iptr =(int *)malloc(sizeof(int));
*iptr=5;
for(int i=0;i<8;i++)
{
sleep(1);
printf("My turn %d\n", i);
(*iptr)++;
}

return iptr;

}

void yourturn()
{
for(int i=0;i<3;i++){
sleep(2);
printf("Your Turn %d\n", i);
}

}

int main(){

pthread_t newthread;
int *result;

pthread_create(&newthread, NULL, myturn, NULL);
yourturn();

pthread_join(newthread, &result);
printf("threads done: *result= %d\n", *result);

}

zehraortak
Автор

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

Slomeus
Автор

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.

NotMarkKnopfler
Автор

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?

stephenworrall
Автор

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

mehrmalik