Pthread User Guide (2) joinable and detached

Source: Internet
Author: User

The thread from pthread_craete (), joinable or detached.

If it is a jionale thread, you must use pthread_join () to wait for the end of the thread. Otherwise, the resources occupied by the thread will not be released, causing resource leakage. If you want to create a thread but do not want to use pthread_join () to wait for the end of the thread, you can create a detached thread. The resources occupied by the detached thread are automatically released at the end of the thread.
Detached is not required, and pthread_join () cannot be used to wait for the thread to end.
In addition, a jionale thread can only have one pthread_jion () to wait for the end. If there are multiple threads, only the first thread is valid, and other threads will return directly, the error message is returned by the return value of the pthread_join () function.

The above code mainly involves the creation of joinable and detached state threads, multiple pthread_join () and one detached thread in the same thread:


[Cpp]
# Include <pthread. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>
 
// Thread ID
Pthread_t ntid_joinable;
Pthread_t ntid_detached;
// Mutex object
Pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
Int count;
 
Void printids (const char * s)
{
Pid_t pid;
Pthread_t tid;
 
Pid = getpid ();
Tid = pthread_self ();
 
Printf ("% s pid % u tid % u (0x % x) \ n", s, (unsigned int) pid,
(Unsigned int) tid, (unsigned int) tid );
}
 
 
// Thread Functions
Void * thr_joinablefn (void * arg)
{
Printids ("new thread thr_joinablefn begin \ n ");
 
// Lock
Pthread_mutex_lock (& mutex );
 
Printids ("new thread thr_joinablefn: \ n ");
 
Int I = 0;
For (; I <5; ++ I)
{
Printf ("thr_joinablefn runing % d \ n", count ++ );
Sleep (1 );
}
 
// Release the mutex lock
Pthread_mutex_unlock (& mutex );
 
Return (void *) 555 );
}
 
// Thread Functions
Void * thr_detachedfn (void * arg)
{
Printids ("new thread thr_detachedfn begin \ n ");
 
// Lock
// Pthread_mutex_lock (& mutex );
 
Int err;
Int ** ret;
Err = pthread_join (ntid_joinable, (void **) ret );
If (err = 0)
{
Printf ("thr_joinablefn return in thr_detachedfn % d \ n", * ret );
}
Else
{
Printf ("can't pthread_join ntid_joinable thread in thr_detachedfn: % s \ n", strerror (err ));
}
 
Printids ("new thread thr_detachedfn: \ n ");
 
Int I = 0;
For (; I <10; ++ I)
{
Printf ("thr_detachedfn runing % d \ n", count ++ );
Sleep (1 );
}
 
// Release the mutex lock
// Pthread_mutex_unlock (& mutex );
 
Return (void *) 666 );
}
 
Int main (void)
{
Int err;
 
Count = 0;
// Initialize the mutex object
Pthread_mutex_init (& mutex, NULL );
 
// Create a joinable thread
// If the second parameter of pthread_craete is NULL, a thread with the default attribute is created, which is PTHREAD_CREATE_JOINABLE.
Err = pthread_create (& ntid_joinable, NULL, thr_joinablefn, NULL );
If (0! = Err)
{
Printf ("can't create ntid_joinable thread: % s \ n", strerror (err ));
}
 
// Create a detached thread
Pthread_attr_t attr;
Pthread_attr_init (& attr );
// If PTHREAD_CREATE_DETACHED in the following sentence is changed to PTHREAD_CREATE_JOINABLE
// The thread is the same as the thread created above
Pthread_attr_setdetachstate (& attr, PTHREAD_CREATE_DETACHED );
Err = pthread_create (& ntid_detached, & attr, thr_detachedfn, NULL );
If (0! = Err)
{
Printf ("can't create thr_detachedfn thread: % s \ n", strerror (err ));
}
Pthread_attr_destroy (& attr );
 
Int ** ret;
Err = pthread_join (ntid_joinable, (void **) ret );
If (err = 0)
{
Printf ("thr_joinablefn return % d \ n", * ret );
}
Else
{
Printf ("can't pthread_join ntid_joinable thread: % s \ n", strerror (err ));
}
 
Err = pthread_join (ntid_detached, (void **) ret );
If (err = 0)
{
Printf ("ntid_detached return % d \ n", * ret );
}
Else
{
Printf ("can't pthread_join ntid_detached thread: % s \ n", strerror (err ));
}
 
Pthread_mutex_destroy (& mutex );
 
Return 0;
}
[Cpp]
Running result:
New thread thr_joinablefn begin
Pid 2352 tid 4981312 (0x4c0240)
New thread thr_detachedfn begin
Pid 2352 tid 5047072 (0x4d0320)
New thread thr_joinablefn:
Pid 2352 tid 4981312 (0x4c0240)
Can't pthread_join ntid_joinable thread in thr_detachedfn: Invalid argument
Thr_joinablefn runing 0
New thread thr_detachedfn:
Pid 2352 tid 5047072 (0x4d0320)
Thr_detachedfn runing 1
Thr_joinablefn runing 2
Thr_detachedfn runing 3
Thr_joinablefn runing 4
Thr_detachedfn runing 5
Thr_joinablefn runing 6
Thr_detachedfn runing 7
Thr_joinablefn runing 8
Thr_detachedfn runing 9
Thr_joinablefn return 555
Thr_detachedfn runing 10
Can't pthread_join ntid_detached thread: Invalid argument

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.