summaryrefslogtreecommitdiffhomepage
path: root/faceapi/mutex.h
blob: 11aabafc5ebbdc1036085e54263a83c92c0ac0fe (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
#ifndef SM_API_TESTAPPCONSOLE_MUTEX_H
#define SM_API_TESTAPPCONSOLE_MUTEX_H

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::runtime_error("Failed to initialize Mutex");
                    }
                }
                ~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