Use the pthread_kill function in Linux to test whether the thread is alive.

Source: Internet
Author: User

Pthread_kill:

Don't be scared by the name. pthread_kill is not a kill, but a signal sent to the thread. Do you still remember signal? Most of the default actions of signal are to terminate the running of processes. Therefore, we only need to use signal () to capture signals and add processing functions.

Int pthread_kill (pthread_t thread, int sig );

Sends a Sig signal to the thread with the specified ID. If no processing is performed in the thread code, the whole process is affected according to the default behavior of the signal. That is to say, if you send sigquit to a thread, however, if the thread does not implement the signal processing function, the entire process exits.

Pthread_kill (threadid, sigkill) is the same, killing the entire process.
If you want to obtain the correct behavior, you need to implement signal (sigkill, sig_handler) in the thread.

Therefore, if the int sig parameter is not 0, you must know what to do and implement the thread signal processing function. Otherwise, the entire process will be affected.

OK. If int SIG is 0, this is a reserved signal. It is used to determine whether the thread is still alive.

Let's take a look at the returned values of pthread_kill:
Success: 0
Thread does not exist: esrch
Invalid signal: einval

Therefore, pthread_kill (threadid, 0) is very useful.

Int kill_rc = pthread_kill (thread_id, 0 );

If (kill_rc = esrch)
Printf ("the specified thread did not exists or already quit/N ");
Else if (kill_rc = einval)
Printf ("signal is invalid/N ");
Else
Printf ("the specified thread is alive/N ");

The above code can determine whether the thread is still alive.

 

 

Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://xufish.blogbus.com/logs/40545082.html

Use the pthread_kill function to check whether a thread is still alive. In the Linux environment, the GCC program is compiled and the code is pasted below:

/*******************************Pthread_kill.c*******************************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <pthread. h>
# Include <errno. h>

Void * func1 ()/* exit after 1 second */
{
Sleep (1 );
Printf ("thread 1 (ID: 0x % x) exits. /N ", (unsigned INT) pthread_self ());
Pthread_exit (void *) 0 );
}

Void * func2 ()/* exit after 5 seconds */
{
Sleep (5 );
Printf ("thread 2 (ID: 0x % x) exits. /N ", (unsigned INT) pthread_self ());
Pthread_exit (void *) 0 );
}

Void test_pthread (pthread_t tid)/* return value of pthread_kill: Success (0) the thread does not exist (esrch) The signal is invalid (einval )*/
{
Int pthread_kill_err;
Pthread_kill_err = pthread_kill (TID, 0 );

If (pthread_kill_err = esrch)
Printf ("the thread whose ID is 0x % x does not exist or has exited. /N ", (unsigned INT) tid );
Else if (pthread_kill_err = einval)
Printf ("invalid sending signal. /N ");
Else
Printf ("the thread whose ID is 0x % x is still alive. /N ", (unsigned INT) tid );
}

Int main ()
{
Int ret;
Pthread_t tid1, tid2;

Pthread_create (& tid1, null, func1, null );
Pthread_create (& tid2, null, func2, null );

Sleep (3);/* after creating two processes for three seconds, test whether they are still active */

Test_pthread (tid1);/* test whether a thread with the ID of tid1 exists */
Test_pthread (tid2);/* test whether a thread with the ID of tid2 exists */

Exit (0 );
}

Compile: gcc-O pthread_kill-lpthread pthread_kill.c
Run:./pthread_kill

///////////// ////////////////
Thread 1 (ID: 0xb7e95B90) Exit.
ID: 0xb7e95The B90 thread does not exist or has exited.
ID: 0xb7694The B90 thread is still alive.


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.