linux程序多线程互斥锁的简单使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Linux_ever/article/details/50209665

思路:

首先定义一个互斥锁变量;然后初始化该互斥锁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)来进行初始化;

不过通过实例测试,即使不初始化也是可以的,只不过是没有自定义的属性;


展开阅读全文

没有更多推荐了,返回首页