linux程式多執行緒互斥鎖的簡單使用

readyao發表於2015-12-07

思路:

首先定義一個互斥鎖變數;然後初始化該互斥鎖pthread_mutex_init;

使用臨界區資源的時候首先獲取該鎖pthread_mutex_lock(如果無法獲取資源則執行緒會阻塞,直到該資源可以使用),對臨界區資源使用完時候釋放該互斥鎖pthread_mutex_unlock;

       #include <pthread.h>
       pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
       pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
       pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;


       int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_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);

例項1:不使用互斥鎖,多個執行緒輸出,此時的效果是多個執行緒是並行輸出的,沒有對資源的訪問進行控制

/*************************************************************************
    > File Name: threadbase.c
    > Author: 
    > Mail:  
    > Created Time: 2015騫?2鏈?4鏃?鏄熸湡浜?17鏃?4鍒?8縐?
 ************************************************************************/

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <pthread.h>

typedef struct Threadinfo{
	char name[64];
	int age;
	int numId;
}ThreadInfo;

int nNum, nLoop;
/*
       #include <pthread.h>

       pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;

       pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

       pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;

       int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_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_t mutex = PTHREAD_MUTEX_INITIALIZER;
int g_num = 999;

 
void *thread_routine(void *arg)
{
	int i = 0;
//	pthread_detach(pthread_self());
	ThreadInfo * p = (ThreadInfo *)arg;
//	pthread_mutex_lock(&mutex);
	for (i = 0; i < nLoop; ++i){
		printf("thread id is :%ld, i = %d, numid is %d, g_num is %d\n", pthread_self(), i, p->numId, g_num);
		sleep(1);
	}
	printf("child exit: %ld\n", pthread_self());
//	pthread_mutex_unlock(&mutex);
	pthread_exit(arg);
}

int main()
{
	int i = 0;
	pthread_t tidArray[200];
	ThreadInfo tmp;

	printf("input the number of thread:");
	scanf("%d", &nNum);
	printf("input the number of nLoop:");
	scanf("%d", &nLoop);

	for (i = 0; i < nNum; ++i){
		memset(&tmp, 0, sizeof(ThreadInfo));
		tmp.numId= 100;
		pthread_create(&tidArray[i], NULL, thread_routine, &tmp);
	}

	for (i = 0; i < nNum; ++i){
		pthread_join(tidArray[i], NULL);
	}
	printf("parent exit\n");
	exit(0);
}



例項2:對多個執行緒進行控制,用互斥鎖對資源的訪問進行控制,每次只能有一個執行緒訪問該資源;

/*************************************************************************
    > File Name: threadbase.c
    > Author: 
    > Mail:  
    > Created Time: 2015騫?2鏈?4鏃?鏄熸湡浜?17鏃?4鍒?8縐?
 ************************************************************************/

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <pthread.h>

typedef struct Threadinfo{
	char name[64];
	int age;
	int numId;
}ThreadInfo;

int nNum, nLoop;
/*
       #include <pthread.h>

       pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;

       pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

       pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;

       int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_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_t mutex = PTHREAD_MUTEX_INITIALIZER;
int g_num = 999;

 
void *thread_routine(void *arg)
{
	int i = 0;
//	pthread_detach(pthread_self());
	ThreadInfo * p = (ThreadInfo *)arg;
	pthread_mutex_lock(&mutex);
	for (i = 0; i < nLoop; ++i){
		printf("thread id is :%ld, i = %d, numid is %d, g_num is %d\n", pthread_self(), i, p->numId, g_num);
		sleep(1);
	}
	printf("child exit: %ld\n", pthread_self());
	pthread_mutex_unlock(&mutex);
	pthread_exit(arg);
}

int main()
{
	int i = 0;
	pthread_t tidArray[200];
	ThreadInfo tmp;

	printf("input the number of thread:");
	scanf("%d", &nNum);
	printf("input the number of nLoop:");
	scanf("%d", &nLoop);

	for (i = 0; i < nNum; ++i){
		memset(&tmp, 0, sizeof(ThreadInfo));
		tmp.numId= 100;
		pthread_create(&tidArray[i], NULL, thread_routine, &tmp);
	}

	for (i = 0; i < nNum; ++i){
		pthread_join(tidArray[i], NULL);
	}
	printf("parent exit\n");
	exit(0);
}



此例中的互斥鎖是靜態初始化的,也可以使用函式pthread_mutex_init(&mutex, NULL)來進行初始化;

不過通過例項測試,即使不初始化也是可以的,只不過是沒有自定義的屬性;


相關文章