Multi-thread and mutex lock sorting
Multi-thread Creation
This function is a placeholder for a function defined by an application and serves as the starting address of a thread. Specify the address when calling the CreateThread function. The LPTHREAD_START_ROUTINE type defines the pointer of the callback function. ThreadProc is a placeholder for the name of an application-defined function. Dword winapi ThreadProc (LPVOID lpParameter); [1]
ParametersThe lpParameter receiving thread passes the CreateThread function lpParameter parameter data to the function.
Return ValueThe function should return a value indicating that it is successful or failed.
RemarksA process can call the GetExitCodeThread function to obtain the return value of the ThreadProc function of the thread created by CreateThread. From: Encyclopedia explanation
This article: http://write.blog.csdn.net/postedit
#ifdef WIN32static DWORD WINAPI ThreadProc( void* lpParameter){Thread *thread = (Thread *)lpParameter;thread->action();return 0;}#elsestatic void* ThreadProc( void* lpParameter){Thread *thread = (Thread *)lpParameter;return thread->action();}#endif
Multi-thread creation and destruction
Thread::Thread( int detached = 0 ){status = new_created;#ifdef WIN32#elsepthread_attr_init( &th_attr );if ( detached ){pthread_attr_setdetachstate( &th_attr, PTHREAD_CREATE_DETACHED );}#endif}Thread::~Thread(){#ifdef WIN32if ( status == running ) {TerminateThread(thread_id, 0);}#else//if ( status == running ) {//pthread_cancel(thread_id);//}#endif}
Run
int Thread::start(){#ifdef WIN32if (status == new_created ) {thread_id = CreateThread(NULL, 0, ThreadProc, this, 0, NULL);if ( thread_id ){status = running;return 0;}}#elseif ( status == new_created && !pthread_create(&thread_id, &th_attr, ThreadProc, this) ) {status = running;return 0;}#endifreturn -1;}
Stop
int Thread::stop(int nKill){#ifdef WIN32if ( status == running ) {if(nKill){TerminateThread(thread_id, 0);}status = stopped;return 0;}#elseif ( status == running ) {if(nKill){pthread_cancel(thread_id);usleep(100); // let thread process left work}status = stopped;return 0;}#endifreturn -1;}
Mutex lock type:
* PTHREAD_MUTEX_TIMED_NP, which is the default value, that is, the common lock. When a thread locks, other threads requesting the lock form a waiting queue and obtain the lock by priority after unlocking. This lock policy ensures the fairness of resource allocation. * PTHREAD_MUTEX_RECURSIVE_NP: Nested lock, which allows the same thread to successfully obtain the same lock multiple times and unlock it through multiple unlocks. For different thread requests, re-compete when the lock thread is unlocked. * PTHREAD_MUTEX_ERRORCHECK_NP: Check the error lock. If the same thread requests the same lock, EDEADLK is returned. Otherwise, the action is the same as that of the PTHREAD_MUTEX_TIMED_NP type. This ensures that no deadlock will occur in the simplest case when many locks are not allowed. * PTHREAD_MUTEX_ADAPTIVE_NP: the most simple lock type to adapt to the lock. It only waits for unlocking and then re-competing. The following is how to create a linux instance and a Windows instance.
Mutex: Mutex (int mutex_type) {# ifdef mutex = CreateMutex (NULL, false, NULL); # elsepthread_mutexattr_t attr; pthread_mutexattr_init (& attr); if (pthread_mutexattr_settype (& attr, mutex_type) {// PTHREAD_MUTEX_RECURSIVE_NP nested lock, allowing the same thread to successfully obtain the same lock multiple times and unlock it through multiple unlocks. For different thread requests, re-compete with pthread_mutexattr_settype (& attr, role) when the lock thread is unlocked; // Invalid mutex_type, use defualt type} pthread_mutex_init (& mutex, & attr) # endif}
Destroy
Mutex ::~ Mutex () {# ifdef WIN32CloseHandle (mutex); # convert (& mutex); int Mutex: lock () {# ifdef WIN32if (WAIT_FAILED = WaitForSingleObject (mutex, INFINITE )) {return-1 ;}else {return 0 ;}# elsereturn pthread_mutex_lock (& mutex) ;#endif }# endif} locked
int Mutex::lock(){#ifdef WIN32if (WAIT_FAILED == WaitForSingleObject(mutex,INFINITE)){return -1;}else{return 0;}#elsereturn pthread_mutex_lock(&mutex);#endif}
Unlockint Mutex::unlock() {#ifdef WIN32if ( ReleaseMutex(mutex) )return 0;else return -1;#elsereturn pthread_mutex_unlock(&mutex);#endif}
This article: http://write.blog.csdn.net/postedit
Not complete...