OpendTect  6.6
queue.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H.Lammertink
8  Date: March 2004
9  RCS: $Id$
10 ________________*_______________________________________________________
11 
12 -*/
13 
14 #include "threadlock.h"
15 
16 
21 template <class T>
23 {
24 public:
25  QueueEntry( T item )
26  : value( item ), next( 0 ) {}
27 
28  T value;
30 };
31 
32 
37 template <class T>
39 {
40 public:
41  ObjQueue() : head(0), tail(0), lock_(true) {}
42 
44  void add( T* item )
45  {
46  Threads::Locker lckr( lock_ );
47  if ( !tail )
48  head = tail = new QueueEntry<T*>( item );
49  else
50  {
51  tail->next = new QueueEntry<T*>( item );
52  tail = tail->next;
53  }
54  }
55 
57  T* next()
58  {
59  if ( !head ) return 0;
60  Threads::Locker lckr( lock_ );
61 
62  T* value = head->value;
63 
64  QueueEntry<T*>* old = head;
65  head = head->next;
66  delete old;
67 
68  if ( !head ) tail = 0;
69  return value;
70  }
71 protected:
72 
74 
77 
78 };
79 
80 
85 template <class T>
87 {
88 public:
89  TypeQueue() : head(0), tail(0), lock_(true) {}
90 
91  void add( T item )
92  {
93  Threads::Locker lckr( lock_ );
94  if ( !tail )
95  head = tail = new QueueEntry<T>( item );
96  else
97  {
98  tail->next = new QueueEntry<T>( item );
99  tail = tail->next;
100  }
101  }
102 
103  bool empty() { return !head; }
104 
105  T next()
106  {
107  if ( empty() ) return 0;
108  Threads::Locker lckr( lock_ );
109 
110  T value = head->value;
111 
112  QueueEntry<T>* old = head;
113  head = head->next;
114  delete old;
115 
116  if ( !head ) tail = 0;
117 
118  return value;
119  }
120 protected:
121 
123 
126 
127 };
128 
129 
QueueEntry::next
QueueEntry< T > * next
Definition: queue.h:29
Threads::Locker
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:85
TypeQueue::add
void add(T item)
Definition: queue.h:91
ObjQueue::lock_
Threads::Lock lock_
Definition: queue.h:73
TypeQueue::next
T next()
Definition: queue.h:105
QueueEntry::value
T value
Definition: queue.h:28
TypeQueue::lock_
Threads::Lock lock_
Definition: queue.h:122
ObjQueue::head
QueueEntry< T * > * head
Definition: queue.h:75
mClass
#define mClass(module)
Definition: commondefs.h:181
TypeQueue::tail
QueueEntry< T > * tail
Definition: queue.h:125
TypeQueue::head
QueueEntry< T > * head
Definition: queue.h:124
TypeQueue::TypeQueue
TypeQueue()
Definition: queue.h:89
QueueEntry::QueueEntry
QueueEntry(T item)
Definition: queue.h:25
TypeQueue::empty
bool empty()
Definition: queue.h:103
ObjQueue::next
T * next()
becomes YOURS!
Definition: queue.h:57
threadlock.h
ObjQueue::add
void add(T *item)
item becomes MINE!
Definition: queue.h:44
QueueEntry
One single queue entry.
Definition: queue.h:23
ObjQueue
Queue of objects.
Definition: queue.h:39
ObjQueue::ObjQueue
ObjQueue()
Definition: queue.h:41
TypeQueue
Queue of basic data types.
Definition: queue.h:87
Threads::Lock
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class.
Definition: threadlock.h:53
ObjQueue::tail
QueueEntry< T * > * tail
Definition: queue.h:76

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021