summaryrefslogtreecommitdiffhomepage
path: root/faceapi/mutex.h
blob: a4f84705ee2e3a5fea02567f3a80869af6457410 (plain)
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
44
45
46
#ifndef SM_API_TESTAPPCONSOLE_MUTEX_H
#define SM_API_TESTAPPCONSOLE_MUTEX_H

#include <exception>

namespace sm
{
    namespace faceapi
    {
        namespace samplecode
        {
            // A very simple mutex class for sample code purposes. 
            // It is recommended that you use the boost threads library.
            class Mutex
            {
            public:
                Mutex()
                {
                    if (!InitializeCriticalSectionAndSpinCount(&_cs,0x80000400)) 
                    {
                        throw std::exception();
                    }
                }
                ~Mutex()
                {
                    DeleteCriticalSection(&_cs);
                }
                void lock() const
                {
                    EnterCriticalSection(&_cs); 
                }
                void unlock() const
                {
                    LeaveCriticalSection(&_cs); 
                }
            private:
                // Noncopyable
                Mutex(const Mutex &);
                Mutex &operator=(const Mutex &);
            private:
                mutable CRITICAL_SECTION _cs;
            };
        }
    }
}
#endif