Hi, nice video but I would have a question to the detached thread. My understanding is that when the thread is detached from the main thread it runs independently. But what happens when main ends. When will the thread end as it is running in an endless loop? Will it get notified by the main thread. In addition the thread is using the file pointer to write the log file. As this file is close by the main thread before it ends I would assume that the thread would crash when it tries to write the next timestamp. Or did I miss something? Thanks a lot.
Another nice video! Interesting that the log is not a log of the event of a new incident but just the status at a specific timestamp. So I can enter more than one incident in between the logger executions. I was curious if things like garbage collection could work using detached threads too?
Hi! Thank you for share your knowledge ! So, do you put the char timestamp[256] declaration inside the while loop redeclaring the array again and again... This can cause a bug by running for a long time ?
Great question! :-) Just to be clear with how it's working, it's *not* the case that if the loop runs 100 times we have 100 char arrays timestamp that exist simultaneously as a result. At any one time we will only have one char array timestamp that exists. In theory every time the loop runs we get a "new" char array timestamp but the "old" one from the last loop iteration is no longer there. The compiler may do some optimizations though and re-use the same memory, I'm not sure we would have to print out the memory address of the char array with each loop iteration to check this. But anyways, because only one array will ever exist at a time, it shouldn't cause a bug. :-)
Those are not two variables names, he is naming the variable exactly as the struct, because the struct is called: tm, so he first writes the type of the variable: struct tm and then the variable: tm, together will be: *struct tm tm*
How is that you put "&logger" inside pthread_create function for detached threads, but on your previous videos there is no ampersand in front of callback functions inside pthread_create (i.e. function "deposit" inside "Race Conditions Explained With An Example" video, and function "computation" inside "Introduction To Threads (pthreads)" video ??
This is a really great question! 🙂 In my experience both function_name and &function_name will give you the memory address (pointer) of the function. So I actually think the answer is that "it doesn't matter", and that in the one video I just did it differently for no real reason. I am trying to find some official documentation to verify this, but I'm 99% sure this is the case. For example in the code below we will get the same memory address output: #include void *func() { printf("func! "); } int main() { func(); printf("%p ", func); printf("%p ", &func); return 0; }
@@PortfolioCourses I was thinking the same, but was not sure because that "&" usually plays a big role when dealing with pointers and adresses. Nevertheless, considering that you have more expirience than I do, I will take you asswer as a correct one. Thank you for for a quick response and also great videos.
It’s interesting I’ve found very official examples online for reputable sources that do it both ways! The compiler will likely just do the same thing no matter what, so it doesn’t matter too much either way. :-)
One more update... I found an official answer, they are in fact the same: stackoverflow.com/a/6293570. Though this question actually gave me two new ideas for videos, as I would like to cover this, and the difference between array and &array (where it turns out there actually is a difference).
You're a great teacher. When I need to learn about subjects I don't know, I find myself here. Greets from Ecole 42, thank you!
42 Lisbon here! :)
Well these videos are gold! I am just impressed why it has so few likes! it deserve way more!!
Hi, nice video but I would have a question to the detached thread. My understanding is that when the thread is detached from the main thread it runs independently. But what happens when main ends. When will the thread end as it is running in an endless loop? Will it get notified by the main thread. In addition the thread is using the file pointer to write the log file. As this file is close by the main thread before it ends I would assume that the thread would crash when it tries to write the next timestamp. Or did I miss something? Thanks a lot.
I am sure mutexes should be used here
I am also confused. I hope anyone can give some advice.
Another nice video! Interesting that the log is not a log of the event of a new incident but just the status at a specific timestamp. So I can enter more than one incident in between the logger executions. I was curious if things like garbage collection could work using detached threads too?
Hi! Thank you for share your knowledge !
So, do you put the char timestamp[256] declaration inside the while loop redeclaring the array again and again... This can cause a bug by running for a long time ?
Great question! :-) Just to be clear with how it's working, it's *not* the case that if the loop runs 100 times we have 100 char arrays timestamp that exist simultaneously as a result. At any one time we will only have one char array timestamp that exists. In theory every time the loop runs we get a "new" char array timestamp but the "old" one from the last loop iteration is no longer there. The compiler may do some optimizations though and re-use the same memory, I'm not sure we would have to print out the memory address of the char array with each loop iteration to check this. But anyways, because only one array will ever exist at a time, it shouldn't cause a bug. :-)
@@PortfolioCourses I see, thank you for answering !
You’re welcome! :-)
Hi Teacher, what is the meaning of "struct tm *tm" ? how can a struct have two variable names. 9:00 . I am confused, thank you
Those are not two variables names, he is naming the variable exactly as the struct, because the struct is called: tm, so he first writes the type of the variable: struct tm and then the variable: tm, together will be: *struct tm tm*
How is that you put "&logger" inside pthread_create function for detached threads, but on your previous videos there is no ampersand in front of callback functions inside pthread_create (i.e. function "deposit" inside "Race Conditions Explained With An Example" video, and function "computation" inside "Introduction To Threads (pthreads)" video ??
This is a really great question! 🙂 In my experience both function_name and &function_name will give you the memory address (pointer) of the function. So I actually think the answer is that "it doesn't matter", and that in the one video I just did it differently for no real reason. I am trying to find some official documentation to verify this, but I'm 99% sure this is the case. For example in the code below we will get the same memory address output:
#include
void *func()
{
printf("func!
");
}
int main()
{
func();
printf("%p
", func);
printf("%p
", &func);
return 0;
}
But to further clarify... in retrospect I shouldn't have used the & operator in this example given that it's not really necessary.
@@PortfolioCourses I was thinking the same, but was not sure because that "&" usually plays a big role when dealing with pointers and adresses. Nevertheless, considering that you have more expirience than I do, I will take you asswer as a correct one. Thank you for for a quick response and also great videos.
It’s interesting I’ve found very official examples online for reputable sources that do it both ways! The compiler will likely just do the same thing no matter what, so it doesn’t matter too much either way. :-)
One more update... I found an official answer, they are in fact the same: stackoverflow.com/a/6293570. Though this question actually gave me two new ideas for videos, as I would like to cover this, and the difference between array and &array (where it turns out there actually is a difference).
Awesome!
thaanks you so much man
You're welcome! :-D