How to pass arguments to threads in C

preview_player
Показать описание
Source code can be found here:

===== Support us through our store =====

===== Check out our website =====

===== Check out our Discord server =====
Рекомендации по теме
Комментарии
Автор

We can pass address of position in array instead index "i", ie: "primes+i". I guess...

mariuszk
Автор

I am so happy to watch all these videos on this channel. He is genuinely one of the best teachers. I am really new to threads and c programming in general and he is making it so much easier on new programmers to learn. He starts with giving examples on how we would first think to approach an issue then tells us what is wrong with it and how we can improve it. I really like the way he teaches. thank you

alihelbah
Автор

I just really appreciate all of your videos, very informative and easy to follow

firedemon
Автор

Thank you so much for your couses! I learn a lot with your lessons. Here is another solution.

void *routine(void *arg)
{
int index = *(int*)arg;
pthread_mutex_lock(&mutex);
printf("%d ", primes[index]);

free(arg);
return NULL;
}

sakwon
Автор

Great video, thanks hugely. I was struggling with arguments in the pthread routine for a while

Dudeitsbrian
Автор

Thank you for the video I definitely learn a lot from you. I think one important thing to mention too is that declaring the malloced int* also needs to happen inside the loop or else you you would pass the same pointer and free would free the value sometimes before other threads can use it. I had the same setup but assigned into an int of a struct and had double freeing issues because of this. Still trying how to figure out a way around this though while passing struct if there are any ideas I am all ears. :)

michaelnguyen
Автор

apparently putting the sleep function after the pthread_create function works too lol thanx for your videos !!

fabv
Автор

Man you are no less than God to me! You explain such complex concepts and debug stuff so easily, you are amazing.

ayushanand
Автор

Very Nicely Explained.
Thanks a lot.
Wish you a happy new year ahead.

mohammadahsan
Автор

Utterly hilarious video. I just ran into this exact situation six hours ago as I began to develop a test multi-threading program. After staring at the screen for two hours trying to figure out what was wrong, I solved it using a small const static array of values and used the loop index to index into the array and pass the address of the array element.

KABOOM, problem solved.

I did not need malloc() or free() because the small array was static in memory, trading off a little space for a little memory management processing.

Great videos, absolutely tremendous. I'm so glad to have stumbled on your channel and have enjoyed several of your very informative videos.

Thank you, thank you, thank you.

innovationsurvival
Автор

fk, you are too good, I literally encounter the exact same problem of having duplicated value of i after passed to the thread function. You saved me

ivanlo
Автор

thanks for the lesson! better than my univeristy...

marco
Автор

we can also pass value of a variable i by casting it as (void *) then in function we can again cast to int.. no need to allocate and deallocate for that.
PS: you have the best OS coding videos, thanks! :)

gauravghati
Автор

You are really great!!! Can you make IPC shared memory and message passing concept video?

prathapbillgates
Автор

First of all, THANK YOU very much for your recent playlists especially the UNIX ones. These lectures are very helpful and organized!

I have a question which is unrelated to thread but arose from this example you showed. Inside the loop, instead of dynamically allocating new integers in each iteration, I just used ``int idx = i;'' hoping in each iteration a new int variable (idx) would be created from stack memory. Printing the address of this idx variable, I notice the addresses in each interactions are same, which indicates the idx variable is only created once and reused in all iterations. Is my understanding correct? It looks unintuitive though. I thought int idx = i; would declare and initiate new variables in each iteration.

MdAbdullahAlFahim
Автор

Excellent example and explanation. In pthread_create(), could you just pass in &primes[i] as the argument?

ssbitar
Автор

When 'i' was declared inside the 'for' loop, why wasn't that giving an error on accessing its location after 1 sec as it would definitely have been out of scope(and deleted) in due course of time? It is instead giving a value of 10, does that mean it didn't get deleted on exit from the 'for' loop?

kshitijsingh
Автор

I think that a remark on 12:16 "every single pointer has the same size" is a bit of a gaffe / misleading in the context of the free() call, because what we are actually freeing is a chunk of memory allocated by malloc. It's probably more relevant (and I guess valid in most cases) concerning the implicit conversion as we provide a (int *) as an argument to the pthread_create() which expects (void *).

drmabusedesporte
Автор

Is there any way that we can see them in the order that they were in array?

onursimsek
Автор

what if I create the routine function as void* routine(int arg) and pass i value? It works also. And you dont have to allocate dynamic memory. There is a downside?

chnsnyz