File: AtomicCounter.h

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (40 lines) | stat: -rw-r--r-- 748 bytes parent folder | download | duplicates (9)
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
#pragma once

#include <QMutex>
#include <QWaitCondition>

namespace SyntopiaCore {
	namespace GLEngine {	

		class AtomicCounter {
		public:
			AtomicCounter() : current(0) { };
			
			int increase() {
				mutex.lock(); 
				int i = ++current; 
				mutex.unlock(); 
				wc.wakeAll(); 
				return i; 
			}

			void setValue(int value) {
				mutex.lock(); 
				current = value; 
				mutex.unlock(); 
				wc.wakeAll(); 
			}

			bool wait(unsigned long time = ULONG_MAX) { wcm.lock(); bool w = wc.wait(&wcm,time); wcm.unlock(); return w; }
			int value() { mutex.lock(); int i = current; mutex.unlock(); return i; }
		private:
			int current;
			QMutex mutex;
			QMutex wcm;
			QWaitCondition wc;
		};


	}
}