Continued from Part 2:
In Linux, normally (when we use default pthread_attribute, aka sending null to pthread_create()), we're creating the joinable thread. There is another type of thread named detached thread, which We'll see later.
A joinable thread is like a process, isn't automatically cleaned up by GNU/Linux when it terminates. Instead, its exit states hang around the system (like a zombie process) until the thread called pthread_join() obtains its return value.
Let's see what happens in the Windows world:
When the thread terminates (the normal way, aka the thread function returns), it ensures the following :
1. All C++ objects created in the thread function will be destroyed properly via their destructor.
2. The OS will clean up memory owned by the thread's stack.
3. The system will decrement threads kernel objects usage count.
The following code snippet (GNU/Linux) shows a thread created using pthread_create() with default pthread_attr_t (aka null) as the joinable thread.
void *Print_Details(void *param)
{
/*Cast the pointer to the right type */
int status;
int state;
struct mystruct* p = (struct mystruct*) param;
printf("Name: %s\n", p->name);
printf("Thread: %s\n", p->thread);
printf("+++++++++++++++++++\n");
pthread_attr_t pta;
status = pthread_attr_init(&pta);
// Get the thread state, Joinable or Detached
status = pthread_attr_getdetachstate(&pta, &state);
switch (state)
{
case PTHREAD_CREATE_DETACHED:
printf("The thread is Detached\n");
break;
case PTHREAD_CREATE_JOINABLE:
printf("The thread is Joinable\n");
break;
}
// Destroys thread attributes object and allows the system to reclaim resources
// associated (if any) with the attribute object.
status = pthread_attr_destroy(&pta);
return NULL;
}
In Linux, normally (when we use default pthread_attribute, aka sending null to pthread_create()), we're creating the joinable thread. There is another type of thread named detached thread, which We'll see later.
A joinable thread is like a process, isn't automatically cleaned up by GNU/Linux when it terminates. Instead, its exit states hang around the system (like a zombie process) until the thread called pthread_join() obtains its return value.
Let's see what happens in the Windows world:
When the thread terminates (the normal way, aka the thread function returns), it ensures the following :
1. All C++ objects created in the thread function will be destroyed properly via their destructor.
2. The OS will clean up memory owned by the thread's stack.
3. The system will decrement threads kernel objects usage count.
The following code snippet (GNU/Linux) shows a thread created using pthread_create() with default pthread_attr_t (aka null) as the joinable thread.
void *Print_Details(void *param)
{
/*Cast the pointer to the right type */
int status;
int state;
struct mystruct* p = (struct mystruct*) param;
printf("Name: %s\n", p->name);
printf("Thread: %s\n", p->thread);
printf("+++++++++++++++++++\n");
pthread_attr_t pta;
status = pthread_attr_init(&pta);
// Get the thread state, Joinable or Detached
status = pthread_attr_getdetachstate(&pta, &state);
switch (state)
{
case PTHREAD_CREATE_DETACHED:
printf("The thread is Detached\n");
break;
case PTHREAD_CREATE_JOINABLE:
printf("The thread is Joinable\n");
break;
}
// Destroys thread attributes object and allows the system to reclaim resources
// associated (if any) with the attribute object.
status = pthread_attr_destroy(&pta);
return NULL;
}
Comments