Linux基础编程 多线程中的互斥锁 pthread_mutex_lock
Linux基础编程 多线程中的互斥锁 pthread_mutex_lock
pthread_mutex.h头文件
- #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
#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源文件
- /*
- 互斥锁用来保证一段时间内只有一个线程在执行一段代码。
- 必要性显而易见:假设各个线程向同一个文件顺序写入数据,
- 最后得到的结果一定是灾难性的。
- */
- #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;
- }
/* 互斥锁用来保证一段时间内只有一个线程在执行一段代码。 必要性显而易见:假设各个线程向同一个文件顺序写入数据, 最后得到的结果一定是灾难性的。 */ #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; }
|
评论暂时关闭