I'm training on C++ and threads. I found the following code from this page and compiled it on my Ubuntu 20.04 machine:
// C program to show thread functions
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void* func(void* arg)
{
// detach the current thread
// from the calling thread
pthread_detach(pthread_self());
usleep(3*1000000);
printf("Inside the thread\n");
// exit the current thread
pthread_exit(NULL);
}
void fun()
{
pthread_t ptid;
// Creating a new thread
pthread_create(&ptid, NULL, &func, NULL);
printf("This line may be printed"
" before thread terminates\n");
// The following line terminates
// the thread manually
// pthread_cancel(ptid);
// Compare the two threads created
if(pthread_equal(ptid, pthread_self()))
printf("Threads are equal\n");
else
printf("Threads are not equal\n");
// Waiting for the created thread to terminate
pthread_join(ptid, NULL);
printf("This line will be printed"
" after thread ends\n");
pthread_exit(NULL);
}
// Driver code
int main()
{
fun();
return 0;
}
I just added a usleep
in the thread function but the behavior doesn't change.
If I understand everything correctly the message "This line will be printed after thread ends"
shall be always printed at the very end of the program, when thread ptid is ended.
But in reality, it often happens that this ending message is printed and then after 3 seconds (due to usleep
call) it is printed the message "Inside the thread", seeming that thread ptid is still alive and running.
Without the usleep
(as per original code) happened the same just without the 3s wait in the middle.
What's going wrong?