How to determine whether a thread exists in Linux

WBOY
Release: 2022-04-06 17:15:01
Original
3769 people have browsed it

In mysql, you can use the "pthread_kill()" function to determine whether a thread exists. This function can send a sig signal to the thread with a specified ID to determine whether the thread is still alive. The syntax is "int pthread_kill(pthread_t thread, int sig)".

How to determine whether a thread exists in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to determine whether a thread exists in Linux

pthread_kill:

Don’t be scared by the name, pthread_kill is not kill, but sends a signal to the thread. Remember signal? The default action of most signals is to terminate the running of the process, so we have to use signal() to catch the signal and add a processing function.

int pthread_kill(pthread_t thread, int sig);
Copy after login

Send the sig signal to the thread with the specified ID. If it is not processed in the thread code, the entire process will be affected according to the default behavior of the signal. In other words, if you send SIGQUIT to a thread, but the thread does not If the signal processing function is not implemented, the entire process exits.

The same goes for pthread_kill(threadid, SIGKILL), which kills the entire process.

If you want to get the correct behavior, you need to implement signal(SIGKILL, sig_handler) in the thread.

So, if the parameter of int sig is not 0, you must know exactly what to do, and you must implement the signal processing function of the thread, otherwise, it will affect the entire process.

OK, if int sig is 0, this is a reserved signal, and its function is to determine whether the thread is still alive.

Let’s take a look at the return value of pthread_kill:

  • Success: 0

  • The thread does not exist: ESRCH

  • Illegal signal: EINVAL

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

How to determine whether a thread exists in Linux

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

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to determine whether a thread exists in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template