-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMutex.cpp
More file actions
43 lines (38 loc) · 844 Bytes
/
Mutex.cpp
File metadata and controls
43 lines (38 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Mutex.cpp
// MessageLoop
//
// Created by Lymons on 14-3-24.
// Copyright (c) 2014年 Lymons. All rights reserved.
//
#include "Mutex.h"
#ifndef WIN32
static pthread_mutexattr_t *sMutexAttr=NULL;
static void MutexAttrInit();
static pthread_once_t sMutexAttrInit = PTHREAD_ONCE_INIT;
#endif
MMutex::MMutex()
{
#ifdef WIN32
::InitializeCriticalSection(&fMutex);
#else
(void)pthread_once(&sMutexAttrInit, MutexAttrInit);
(void)pthread_mutex_init(&fMutex, sMutexAttr);
#endif
}
#ifndef WIN32
void MutexAttrInit()
{
sMutexAttr = (pthread_mutexattr_t*)malloc(sizeof(pthread_mutexattr_t));
::memset(sMutexAttr, 0, sizeof(pthread_mutexattr_t));
pthread_mutexattr_init(sMutexAttr);
}
#endif
MMutex::~MMutex()
{
#ifdef WIN32
::DeleteCriticalSection(&fMutex);
#else
pthread_mutex_destroy(&fMutex);
#endif
}