I. general thread concepts
A thread can be viewed as a lightweight process. All programs have a main thread, which is the control flow or execution thread of the process. In a multi-threaded program, the main thread can create one or more peer threads. From this point in time, these threads start to run concurrently. The difference between the main thread and the peer thread is that the main thread is always the first running thread in the process. Threads have two advantages: low resource consumption and convenient communication mechanism.
Ii. Common Operations on threads
(1) create a thread: pthread_create
Int pthread_create (pthread_t * thread, pthread_attr_t * attr, void * (* start_routine) (void *), void * arg );
The first parameter is the thread structure pthread_t to be created, the second parameter is the relevant attributes of the thread, and the third parameter is the pointer of the function to be executed by the thread, the fourth parameter is the parameter passed to * start_routine. You can select its value and usage.
Pthread_create returns 0 after successful execution, and stores the tid thread identifier in the pthread_t structure; otherwise, a non-zero value is returned and errno is set.
(2) terminate the current thread: pthread_exit
Void pthread_exit (void * retval );
Pthread_exit to exit the current thread. Before exiting, pthread_cleanup_push will be called. It is implicitly called at the end of the upper-level function of the thread. You can add a retval parameter to call it explicitly for pthread_join reference.
(3) suspend the current thread: pthread_join
Int pthread_join (pthread_t th, void ** thread_return );
Pthread_join suspends the current thread until the specified Thread th is terminated. thread_return is the parameter at the end of th. If it is not explicitly specified, it is NULL.
(4) cancel a thread: pthread_cancel
Int pthread_cancel (pthread_t thread );
(5) wait for the end of the thread: pthread_join
Int pthread_join (pthread_t th, void ** thread_return );
The thread that calls this function suspends until the waiting thread ends. The main purpose of this function is to wait for the specified thread to end and reclaim its memory resources. Otherwise, a large amount of memory resources will be wasted.
Iii. Thread resource mutual lock
Since threads share resources, mutual exclusion is very important. Mutex provides a lock method for the mutex object to obtain the exclusive resource. Other threads attempting to lock the mutex will be blocked and suspended until the thread that locks the resource is unlocked.
When using thread programming, you do not need to use mutex everywhere. Otherwise, the meaning of concurrent threads will be lost. For example, you can use mutex under resources that can only be accessed in serial mode.
The mutex object is defined as pthread_mutex_t in pthead. h. Its system calls mainly include:
Int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutex_attr_t * mutexattr );
Int pthread_mutex_lock (pthread_mutex_t * mutex );
Int pthread_mutex_trylock (pthread_mutex_t * mutex );
Int pthread_mutex_unlock (pthread_mutex_t * mutex );
Int pthread_mutex_destroy (pthread_mutex_t * mutex );
Pthread_mutex_init creates a mutex object and initializes the mutex object with the specified attribute. The initialization attribute includes PTHREAD_MUTEX_INITIALIZER to create a quick mutex. This mutex performs simple locking and unlocking, after locking, block another thread to lock it. PTHREAD_RECURSIVE_MUTEX_INITIALIZER creates recursive mutex. This mutex counts the lock and calls the same number of pthread_mutex_unlocks when unlocking;
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER creates a check error mutex. After being locked, this mutex will return an EDEADLK error code to the thread trying to lock it, without blocking it.
Pthread_mutex_lock and pthread_mutex_unlock are the mutex pairs specified for locking and unlocking. The difference between pthread_mutex_trylock and pthread_mutex_lock is that if the mutex object is locked, an EBUSY error code is returned, pthread_mutex_destory destructor mutex and release related resources. If the call succeeds, 0 is returned. If the call fails, a non-zero error code is returned.
4. Thread Maintenance
After a thread is created, the thread starts to execute the content in the thread according to the CPU time allocation. In order to ensure stable running of the thread, the thread cannot be obtained due to other causes such as CPU congestion, the thread cannot be executed. If the thread cannot be executed within a certain period of time, the previous thread is revoked and re-established. The general operation is as follows:
(1) set a global variable g_SocketErr;
(2) In addition to running the functions you want in the thread, set g_SocketErr to 0;
(3) In the while (1) of the main function,
If (g_SocketErr> = 10) // if this thread is abnormal for 10 times, close this thread and re-open this thread {nErr = pthread_cancel (sock_tid); if (nErr! = 0) {printf ("cancel pthread_ipc error! \ N ") ;}else {pthread_join (sock_tid, NULL); nErr = pthread_create (& sock_tid, NULL, ThreadSocket, NULL); // open the loop socket thread if (nErr! = 0) {printf ("creat socket err");} else {g_SocketErr = 0; printf ("Thread pthread_ipc create OK! \ N ") ;}} else {g_SocketErr ++ ;}
Now, we will summarize it here.
Reference: Basic POSIX thread concepts
Pthread_cancel
Http://blog.chinaunix.net/uid-14846899-id-2782352.html this student sort of multi-threaded Reading Notes sort well. You can focus on it.
Http://fanqiang.chinaunix.net/a4/b8/20010811/0905001105.html