DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
cptr< T, M, N > Class Template Reference

#include <cptr.hpp>

Public Member Functions

 cptr (T *p)
 
cptr_proxy< T, M > operator-> ()
 
cptr_proxy< T, M > operator-> () const
 
T * get ()
 
T * get () const
 

Protected Attributes

std::shared_ptr< M > llock
 
T * base
 

Friends

class cptr_proxy< T, M >
 

Detailed Description

template<typename T, typename M = std::recursive_mutex, int N = 100>
class cptr< T, M, N >

cptr models a concurrent pointer with locks.

In its current implementation takes advantage of the drill down behaviour of the arrow operator and returns a proxy object. That object is returned only once the associated lock has been obtained. The proxy can be dereferenced and returns the pointer to the actual holded object. The lock is released when the proxy object is destroyed.

Usage:

Myclass * p = new Myclass;

In any thread :

cptr<Myclass> cp(p);

cp->dosomething();

Definition at line 49 of file cptr.hpp.

Constructor & Destructor Documentation

◆ cptr()

template<typename T , typename M = std::recursive_mutex, int N = 100>
cptr< T, M, N >::cptr ( T * p)
inlineexplicit

Definition at line 57 of file cptr.hpp.

58 : llock ( new M() ),
59 base ( p )
60 {
61 }
std::shared_ptr< M > llock
Definition cptr.hpp:52
T * base
Definition cptr.hpp:54

Member Function Documentation

◆ get() [1/2]

template<typename T , typename M = std::recursive_mutex, int N = 100>
T * cptr< T, M, N >::get ( )
inline

Definition at line 83 of file cptr.hpp.

84 {
85 return base;
86 }

◆ get() [2/2]

template<typename T , typename M = std::recursive_mutex, int N = 100>
T * cptr< T, M, N >::get ( ) const
inline

Definition at line 88 of file cptr.hpp.

89 {
90 return base;
91 }

◆ operator->() [1/2]

template<typename T , typename M = std::recursive_mutex, int N = 100>
cptr_proxy< T, M > cptr< T, M, N >::operator-> ( )
inline

Definition at line 63 of file cptr.hpp.

64 {
65 while ( !llock->try_lock() )
66 {
67 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
68 }
69
70 return *this;
71 }

◆ operator->() [2/2]

template<typename T , typename M = std::recursive_mutex, int N = 100>
cptr_proxy< T, M > cptr< T, M, N >::operator-> ( ) const
inline

Definition at line 73 of file cptr.hpp.

74 {
75 while ( !llock->try_lock() )
76 {
77 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
78 }
79
80 return *this;
81 }

Friends And Related Symbol Documentation

◆ cptr_proxy< T, M >

template<typename T , typename M = std::recursive_mutex, int N = 100>
friend class cptr_proxy< T, M >
friend

Definition at line 88 of file cptr.hpp.

Member Data Documentation

◆ base

template<typename T , typename M = std::recursive_mutex, int N = 100>
T* cptr< T, M, N >::base
protected

Definition at line 54 of file cptr.hpp.

◆ llock

template<typename T , typename M = std::recursive_mutex, int N = 100>
std::shared_ptr<M> cptr< T, M, N >::llock
protected

Definition at line 52 of file cptr.hpp.


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