歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux基礎編程 多線程中的互斥鎖 pthread_mutex_lock

Linux基礎編程 多線程中的互斥鎖 pthread_mutex_lock

日期:2017/3/1 10:17:13   编辑:Linux編程

pthread_mutex.h頭文件

  1. #ifndef __SEM_UTIL_H__
  2. #define __SEM_UTIL_H__
  3. typedef void* SemHandl_t;
  4. SemHandl_t MakeSem(); ///< Initialize the semaphore.
  5. int SemRelease(SemHandl_t hndlSem); ///< Unlock the semaphore.
  6. int SemWait(SemHandl_t hndlSem); ///< Lock the semaphore.
  7. int DestroySem(SemHandl_t hndlSem); ///< Destory the semaphore.
  8. #endif
#ifndef __SEM_UTIL_H__
#define __SEM_UTIL_H__

typedef void* SemHandl_t;

SemHandl_t MakeSem(); ///< Initialize the semaphore.
int SemRelease(SemHandl_t hndlSem); ///< Unlock the semaphore.
int SemWait(SemHandl_t hndlSem); ///< Lock the semaphore.
int DestroySem(SemHandl_t hndlSem); ///< Destory the semaphore.


#endif


pthread_mutex.c源文件

  1. /*
  2. 互斥鎖用來保證一段時間內只有一個線程在執行一段代碼。
  3. 必要性顯而易見:假設各個線程向同一個文件順序寫入數據,
  4. 最後得到的結果一定是災難性的。
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <pthread.h>
  10. #include "pthread_mutex.h"
  11. #define __DEBUG
  12. #ifdef __DEBUG
  13. #define DBG(fmt,args...) fprintf(stdout, fmt, ##args)
  14. #else
  15. #define DBG(fmt,args...)
  16. #endif
  17. #define ERR(fmt,args...) fprintf(stderr, fmt, ##args)
  18. /*線程互斥鎖初始化*/
  19. SemHandl_t MakeSem()
  20. {
  21. SemHandl_t hndlSem = malloc(sizeof(pthread_mutex_t));
  22. if(hndlSem == NULL){
  23. ERR("Not enough memory!!\n");
  24. return NULL;
  25. }
  26. /* Initialize the mutex which protects the global data */
  27. if(pthread_mutex_init(hndlSem, NULL) != 0){
  28. ERR("Sem init faill!!\n");
  29. free(hndlSem);
  30. return NULL;
  31. }
  32. return hndlSem;
  33. }
  34. /*線程互斥鎖釋放*/
  35. int SemRelease(SemHandl_t hndlSem)
  36. {
  37. if(hndlSem == NULL){
  38. ERR("SemRelease: Invalid Semaphore handler\n");
  39. return -1;
  40. }
  41. return pthread_mutex_unlock(hndlSem);
  42. }
  43. /*等待*/
  44. int SemWait(SemHandl_t hndlSem)
  45. {
  46. if(hndlSem == NULL){
  47. ERR("SemWait: Invalid Semaphore handler\n");
  48. return -1;
  49. }
  50. return pthread_mutex_lock(hndlSem);
  51. }
  52. /*刪除*/
  53. int DestroySem(SemHandl_t hndlSem)
  54. {
  55. if(hndlSem == NULL){
  56. ERR("DestroySem: Invalid Semaphore handler\n");
  57. return -1;
  58. }
  59. pthread_mutex_lock(hndlSem);
  60. pthread_mutex_unlock(hndlSem);
  61. if(pthread_mutex_destroy(hndlSem) !=0){
  62. ERR("Sem_kill faill!!\n");
  63. }
  64. free(hndlSem);
  65. return 0;
  66. }
/*
互斥鎖用來保證一段時間內只有一個線程在執行一段代碼。
必要性顯而易見:假設各個線程向同一個文件順序寫入數據,
最後得到的結果一定是災難性的。
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#include "pthread_mutex.h"

#define __DEBUG
#ifdef __DEBUG
#define DBG(fmt,args...) fprintf(stdout,  fmt,  ##args)
#else
#define DBG(fmt,args...)
#endif
#define ERR(fmt,args...) fprintf(stderr,  fmt,  ##args)

/*線程互斥鎖初始化*/
SemHandl_t MakeSem()
{
	SemHandl_t hndlSem = malloc(sizeof(pthread_mutex_t));
	if(hndlSem == NULL){
		ERR("Not enough memory!!\n");
		return NULL;
	}
	/* Initialize the mutex which protects the global data */
	if(pthread_mutex_init(hndlSem, NULL) != 0){
		ERR("Sem init faill!!\n");
		free(hndlSem);
		return NULL;
	}
	return hndlSem;
}

/*線程互斥鎖釋放*/
int SemRelease(SemHandl_t hndlSem)
{
	if(hndlSem == NULL){
		ERR("SemRelease: Invalid Semaphore handler\n");
		return -1;
	}
	return pthread_mutex_unlock(hndlSem);
}

/*等待*/
int SemWait(SemHandl_t hndlSem)
{
	if(hndlSem == NULL){
		ERR("SemWait: Invalid Semaphore handler\n");
		return -1;
	}
	return pthread_mutex_lock(hndlSem);
}

/*刪除*/
int DestroySem(SemHandl_t hndlSem)
{
	if(hndlSem == NULL){
		ERR("DestroySem: Invalid Semaphore handler\n");
		return -1;
	}
	pthread_mutex_lock(hndlSem);
	pthread_mutex_unlock(hndlSem);
	if(pthread_mutex_destroy(hndlSem) !=0){
		ERR("Sem_kill faill!!\n");
	}
	free(hndlSem);
	return 0;
}
Copyright © Linux教程網 All Rights Reserved