Detached Threads (pthreads) | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 17 ноя 2024

Комментарии • 22

  • @erendemirer1
    @erendemirer1 9 месяцев назад +3

    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!

  • @giulianobortolassi573
    @giulianobortolassi573 8 месяцев назад +2

    Well these videos are gold! I am just impressed why it has so few likes! it deserve way more!!

  • @bernhardkneer5393
    @bernhardkneer5393 10 месяцев назад +2

    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.

    • @Redditard
      @Redditard 9 месяцев назад

      I am sure mutexes should be used here

    • @tiankong90
      @tiankong90 Месяц назад

      I am also confused. I hope anyone can give some advice.

  • @vicsteiner
    @vicsteiner 4 месяца назад

    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?

  • @trocandobytes
    @trocandobytes Год назад

    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 ?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      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. :-)

    • @trocandobytes
      @trocandobytes Год назад

      @@PortfolioCourses I see, thank you for answering !

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re welcome! :-)

  • @mastermax7777
    @mastermax7777 Год назад

    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

    • @jdeleca
      @jdeleca 9 месяцев назад +1

      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*

  • @dzeno7370
    @dzeno7370 2 года назад

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

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      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
      @PortfolioCourses  2 года назад

      But to further clarify... in retrospect I shouldn't have used the & operator in this example given that it's not really necessary.

    • @dzeno7370
      @dzeno7370 2 года назад

      @@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.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      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. :-)

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +3

      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).

  • @ValliNayagamChokkalingam
    @ValliNayagamChokkalingam Год назад

    Awesome!

  • @RoockYou
    @RoockYou 2 года назад +1

    thaanks you so much man