Yangshazhou November 2001
This is a column for POSIX thread programming. The author will tell you the POSIX thread library API on the basis of clarifying the concept. This article is the fifth article to tell you pThread_self (), pthread_equal () and pThread_once () and other miscellaneous functions.
In the POSIX thread specification, there are several auxiliary functions that are difficult to classify, temporarily called a miscellaneous function, mainly including pthread_self (), pthread_equal () and pthread_once () three, and a LinuxThread non-portability extension function pthread_kill_other_threads_np (). This article introduces the definition and use of these functions.
1. Get this thread ID
PTHREAD_T PTHREAD_SELF (VOID)
This function returns the identifier of this thread.
In LinuxThreads, each thread is described in a pthread_descr structure, which contains all the required data structures such as thread state, thread ID, which is the implementation of this function to find the pthread_descr structure of this thread in the thread stack frame, and then return it P_TID item.
The PTHREAD_T type is defined in LinuxThreads as unsigned long.
2. Judging whether the two threads are the same thread int pthread_equal (pthread_t thread1, pthread_t thread2)
It is judge whether two thread descriptors points to the same thread. In LinuxThreads, the same threads of the thread ID must be the same thread, so the implementation of this function simply determines whether THREAD1 and Thread2 are equal.
3. Perform only action INT pthread_once (pthread_once_t * overce_control, void (* init_routine) (void))
This function uses the initial_control variable of pthread_once_init to ensure that the init_routine () function is executed only once in the execution sequence of this process.
#include
#include
Pthread_once_t overce = pthread_once_init;
Void OnCE_Run (Void)
{
Printf ("OnCE_Run in Thread% D / N", pthread_self ());
}
Void * child1 (void * arg)
{
INT TID = pthread_self ();
Printf ("Thread% D ENTER / N", TID);
Pthread_once (& overce, overcE_run);
Printf ("Thread% D Returns / N", TID);
}
Void * child2 (void * arg)
{
INT TID = pthread_self ();
Printf ("Thread% D ENTER / N", TID);
Pthread_once (& overce, overcE_run);
Printf ("Thread% D Returns / N", TID);
}
Int main (void)
{
Int Tid1, TID2;
Printf ("Hello / N");
Pthread_create (& TID1, NULL, Child1, NULL);
Pthread_create (& TID2, NULL, Child2, NULL);
Sleep (10);
Printf ("Main Thread EXIT / N");
Return 0;
}
The onCE_Run () function is only performed once, and it is not executed in which thread, although pthread_once (& overce, onCE_RUN) appears in two threads. LinuxThreads uses a mutex and conditional variable to perform the function specified by the pthread_once () and perform only once, while the onCE_Control is characterized whether it is performed. If the initial value of OnCE_Control is not pthread_once_init (LinuXThreads defined as 0), the behavior of pthread_once () will be abnormal. In LinuxThreads, there are three actual "primary functions": Never (0), in_Progress (1), DONE (2), if the initial value of the ONCE is set to 1, then all pThread_once () must wait one of them Excorplence "has executed" signal, so all pthread_once () will fall into a permanent waiting; if set to 2, it means that the function has been executed once, so all pthread_once () will return 0 immediately.
4. Pthread_kill_other_threads_np () void pthread_kill_other_threads_np (void)
This function is the extension of LinuxThreads to the POSIX convention that cannot be implemented. POSIX requires that all threads of the current process should be terminated when the process of thread executing an Exec * system call is loaded in the process space. Due to the limitations of LinuxThreads, the mechanism cannot be implemented in Exec, so the thread is required to perform all threads before executing EXEC. This is this by pthread_kill_other_threads_np ().
It should be noted that pthread_kill_other_threads_np () does not terminate the thread by pthread_cancel (), but directly to the management line "process exit" signal, so that all other threads are running, and without the CANCEL action, of course, will not be executed Exit the callback function. Although LinuxThreads' experimental results are the same as the document description, the code implementation is used in the __pthread_sig_cancel signal to the Kill thread, which should be the same as executing pthread_cancel (), where the reason is not clear.