Thread Operation Basics

1. Thread basic functions
    1) Thread creation
        prototype:
            int pthread_create(pthred_t *restrict tidp, const pthread_attr_t *restrict_attr, void* (*start_rtn)(void*), void *restrict arg);
    
        Function:
            Create a new thread.
    
        Parameters:
            restrict tidp: Returns a thread ID value.
            restrict_attr: incoming thread attribute settings.
            start_rtn: New thread function.
            restrict arg: incoming thread pointer
    
        Return value:
            0 is returned for success, and an error code is returned for failure.
    
    2) Thread separation
        prototype:
            int pthread_detach(pthread_t pthread);
    
        Function:
            Threads have two states of joinable or detached, which are joinable by default. A joinable thread can be reclaimed or killed by other threads, and resources are not released until reclaimed by other threads.
            The detached state, on the other hand, cannot be reclaimed or killed by other threads. It is automatically released by the system until the end of the detached thread.
            This function is used to detach threads.
        
        Parameters:
            pthread: thread ID
            
        Return value:
            0 is returned for success, and an error code is returned for failure.
    
    3) Thread termination signal
        prototype:
            int pthread_cancel(phtread_t pthread);
        
        Function:
            Send termination signal to thread.
    
        Parameters:
            pthread: thread ID
    
        Return value:
            0 if successful, otherwise non-zero. Sending successfully does not mean that the thread pthread will terminate.
        
        Related functions:
            The default state of the thread is: PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DEFERRED
            These three functions are used to set whether they can be canceled or terminated by other threads calling the pthread_cancel function.
            pthread_setcancelstate();
            Role:
                used to set the "cancelability" state of the current thread and return the previous state to the oldstate reference.
                The legal values ​​for the Cancellability status are: PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DISABLE.
                This function can also query the "cancellability" status of the current thread, that is, set the first parameter to NULL.
            pthread_setcanceltype();
            Role:
                used to set the "cancellable type" of the current thread and return the previous type to the oldtype reference.
                The legal values ​​of "cancellable type" are:
                    PTHREAD_CANCEL_DEFERRED : After the thread receives the cancellation operation, it will cancel until it reaches the "cancellable point".
                    PTHREAD_CANCEL_ASYNCHRONOUS : The thread cancels immediately after receiving the cancel operation.
            phtread_testcancel();
            Role:
                used to create a "cancellable point" in the current thread. This function has no effect if the current thread cannot be cancelled.
                If cancelable, the thread is exited at this location when a termination signal is received.
            
        Reference:        
            http://blog.csdn.net/i_am_jojo/article/details/7594174
            http://blog.csdn.net/zy799894671/article/details/18008267

    4) Waiting for thread end signal
        prototype:
            int pthread_join(pthread_t pthread, void **retval);
        Function:
            used to wait for the end of a thread, synchronization between threads Operation
    
        parameters:
            pthread: thread ID
            retval: used to store the return value of the waiting thread
    
        Return value:
            0 is returned for success, and an error code is returned for failure.
            
    5) Thread end
        prototype:
            void pthread_exit(void *retval);
            
        Function:
            End the thread.
            
        Parameters:
            retval can be obtained by calling pthread_join by other threads.
        
        Return value:
            None.
            
    6) Thread attribute setting
        thread attribute function reference:
            http://blog.csdn.net/zsf8701/article/details/7843837
    
    7) Other related functions
        pthread_t pthread_self(void);
            get the current thread ID
        int pthread_equal(pthread_t thread1, pthread_t thread2);
            compare whether the two threads are the same
    
    
2. Thread synchronization
    mutex:
    1) Initialization
        macro initialization:
            pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
        function initialization:
        prototype:
            int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
        
        function:
            initialize mutex
            
        parameters:
            restrict mutex: Mutex structure pointer
            restrict attr: Thread attribute pointer
        
        Return value:
            Returns 0 on success, and an error code on failure.
            
    2) Lock
        prototype:
            int pthread_mutex_lock(pthread_mutex_t *mutex);
        
        Role:
            lock.
            
        Parameters:
            mutex: mutex structure pointer
        
        Return value:
            0 is returned for success, and an error code is returned for failure.
        
        Related functions:
            int pthread_mutex_trylock(pthread_mutex_t *mutex);
            if the lock is not obtained, it will return immediately and return an EBUSY error.
            int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const struct timespec *restrict abs_timeout);
            will wait for abs_timeout time, and return ETIMEDOUT error if no lock is obtained.
            
    4) Unlock
        prototype:
            int pthread_mutex_unlock(pthread_mutex_t *mutex);
        
        Function:
            Unlock
            
        parameters:
            mutex: mutex structure pointer
        
        Return value:
            0 is returned for success, and an error code is returned for failure.
            
    5) Release lock
        prototype:
            int pthread_mutex_destroy(pthread_mutex_t *mutex);
            
        Function:
            Release lock resources.
            
        Parameters:
            mutex: mutex structure pointer
        
        Return value:
            0 is returned for success, and an error code is returned for failure.
    
    6) Mutex attribute setting
        related functions:
            int pthread_mutexattr_init(pthread_mutexattr_t *attr);
            int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
            used to get and set whether the mutex is shared by the process:
            if the attribute is set to PTHREAD_PROCESS_SHARED, then from multiple Mutexes allocated in memory regions shared by processes can be used to synchronize these processes.
            If the property is set to PTHREAD_PROCESS_PRIVATE, then the mutex can only be used for synchronization of multiple threads in a process. This is the default property.
            int pthread_mutexattr_getpshared(const pthread_mutexattr_t * restrict attr, int *restrict pshared);
            int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
            
        Reference:
            http://blog.csdn.net/linux_ever/article/details/51039706
    
    Reference:
        http://blog.csdn.net/linux_ever/article/details/51039706 ://www.cnblogs.com/yuuyuu/p/5140251.html
        
    Condition variable
    1) initialization
        macro initialization:
            pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
        function initialization:
            int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
    
    2) wait or notify condition variable
            int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
            int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
    
            int pthread_cond_broadcast(pthread_cond_t *cond);
            int pthread_cond_signal() ;
            
    3) Release the condition variable
            int pthread_cond_destroy(pthread_cond_t *cond);
            
    Reference:
        http://www.cnblogs.com/yuuyuu/p/5140875.html
        
    Semaphore
    1) Initialization
        prototype:
            int sem_init(sem_t *sem, int pshared , unsigned int value);  
    
    2) Reduce the semaphore value
        prototype:
            int sem_wait(sem_t *sem);  
                
    3) Increase the semaphore value
        prototype:
            int sem_post(sem_t *sem);  
    
    4) Destroy the
        prototype:
            int sem_destroy(sem_t *sem);  
    
    Other thread synchronization methods: read-
        write lock, spin lock.
    
3. Signal
    1) signal();
    2) kill();
    3) pthread_kill();
    
    Reference:
        http://blog.csdn.net/heyustudent/article/details/17506839

4. Question
1) Why is it in the child thread In the infinite loop, the main thread pthread_cancel sends a signal to terminate the child thread, and the child thread cannot be terminated?
    Reason:
        This is because the child thread has been in the while() loop and is not suspended, so it cannot be cancelled.
    Solution:
        Use the pthread_testcancel function in the child thread to set the thread cancellation point, and set the thread state to be cancelable and cancel the execution until the cancellation point.
        Or let it hang, such as: sleep

2) Implementation of message queue under Linux
    Use condition variable + mutex lock
    process:
        into the queue thread. Push data into the queue and send conditional signals with pthread_cond_signal.
        dequeue thread. phtread_cond_wait detects that the signal is woken up and pops data from the queue.
        When entering and leaving the queue, a mutex lock needs to be added.
        
3) How to debug gdb under multithreading


4) The role and significance of coroutines




        
       

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763203&siteId=291194637