My Project
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes
LibThread::SyncVar< T > Class Template Reference

#include <syncvar.h>

Public Member Functions

 SyncVar ()
 
void write (T &value)
 
void read (T &result)
 
bool try_read (T &result)
 

Private Attributes

Lock lock
 
ConditionVariable cond
 
int waiting
 
bool init
 
T value
 

Detailed Description

template<typename T>
class LibThread::SyncVar< T >

Definition at line 11 of file syncvar.h.

Constructor & Destructor Documentation

◆ SyncVar()

template<typename T >
LibThread::SyncVar< T >::SyncVar ( )
inline

Definition at line 19 of file syncvar.h.

19 : lock(), cond(&lock), waiting(0), init(false) {
20 }
ConditionVariable cond
Definition: syncvar.h:14

Member Function Documentation

◆ read()

template<typename T >
void LibThread::SyncVar< T >::read ( T result)
inline

Definition at line 31 of file syncvar.h.

31 {
32 lock.lock();
33 waiting++;
34 while (!init)
35 cond.wait();
36 waiting--;
37 if (waiting)
38 cond.signal();
39 lock.unlock();
40 }
void wait()
Definition: thread.h:88
void signal()
Definition: thread.h:97
void lock()
Definition: thread.h:46
void unlock()
Definition: thread.h:57

◆ try_read()

template<typename T >
bool LibThread::SyncVar< T >::try_read ( T result)
inline

Definition at line 41 of file syncvar.h.

41 {
42 bool success;
43 lock.lock();
44 success = init;
45 if (success) {
46 result = value;
47 }
48 lock.unlock();
49 return success;
50 }
return result
Definition: facAbsBiFact.cc:75

◆ write()

template<typename T >
void LibThread::SyncVar< T >::write ( T value)
inline

Definition at line 21 of file syncvar.h.

21 {
22 lock.lock();
23 if (!init) {
24 this->value = value;
25 this->init = true;
26 }
27 if (waiting)
28 cond.signal();
29 lock.unlock();
30 }

Field Documentation

◆ cond

template<typename T >
ConditionVariable LibThread::SyncVar< T >::cond
private

Definition at line 14 of file syncvar.h.

◆ init

template<typename T >
bool LibThread::SyncVar< T >::init
private

Definition at line 16 of file syncvar.h.

◆ lock

template<typename T >
Lock LibThread::SyncVar< T >::lock
private

Definition at line 13 of file syncvar.h.

◆ value

template<typename T >
T LibThread::SyncVar< T >::value
private

Definition at line 17 of file syncvar.h.

◆ waiting

template<typename T >
int LibThread::SyncVar< T >::waiting
private

Definition at line 15 of file syncvar.h.


The documentation for this class was generated from the following file: