Advertisement
LupusOssorum

queue

Nov 1st, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. /**
  2. An implemention of a Queue.
  3.  
  4. The queue is designed to be able to work with multiple threads.
  5. You can have multiple thread put to thead the Queue will automaticly block if another thread is putting to the queue.
  6. If you want to have mutiple threads read data from queue the reading threads will need to be sure to block themself.
  7.  
  8. To Create a queue
  9. ---
  10. Queue!string queue; // Replace string with data type that values should be.
  11. ---
  12.  
  13. To add to queue
  14. ---
  15. queue.put("This is a value that is added to queue!"); // Argument should be of oppropriate type.
  16. ---
  17.  
  18. To read data
  19. ---
  20. foreach (string value; queue) {
  21. // Deal with value here!
  22. }
  23. ---
  24.  
  25. To read data safe to have multiple threads reading
  26. ---
  27. synchronized {
  28. foreach (string value; queue) {
  29. // Deal with value here!
  30. }
  31. }
  32. ---
  33. or
  34. ---
  35. while (true) {
  36. string value;
  37. synchronized {
  38. if (queue.empty) {
  39. break;
  40. }
  41. value = queue.front;
  42. queue.popFront;
  43. }
  44. // Deal with value here!
  45. }
  46. ---
  47.  
  48. Examples:
  49. ---
  50. Queue!string queue;
  51.  
  52. queue.add("A!");
  53. queue.add("BBB!");
  54. queue.add("CC!");
  55.  
  56. foreach (string value; queue) {
  57. writeln(value);
  58. }
  59.  
  60. queue.add("DDDDD!");
  61. queue.add("EEEE!");
  62.  
  63. foreach (string value; queue) {
  64. writeln(value);
  65. }
  66. ---
  67. */
  68.  
  69. module queue;
  70.  
  71. import std.stdio;
  72. ////import core.sync.semaphore : Semaphore;
  73.  
  74. class Queue(T) {
  75.  
  76. //private struct HeadNode {
  77. // Node* _first;
  78. //}
  79. private struct Node {
  80. T payload;
  81. Node* next;
  82. }
  83.  
  84. private Node* _first;
  85. private Node* _last = new Node(null,null);
  86. private int count = 0;
  87. private Object countLock = new Object;
  88.  
  89. this() {
  90. this._first = this._last;
  91. }
  92.  
  93. /**
  94. Add to the Queue (to the end).
  95. */
  96. void put(T value) {
  97. synchronized {
  98. Node* newLast = new Node(null,null);
  99. this._last.payload = value;
  100. this._last.next = newLast;
  101. this._last = newLast;
  102.  
  103. synchronized(countLock) {
  104. count++;
  105. }
  106. }
  107. }
  108.  
  109.  
  110. /**
  111. To be iterable with `foreach` loop.
  112. */
  113. bool empty() const {
  114. return this.count == 0;
  115. ////return _head._first == null;
  116. }
  117.  
  118. ///ditto
  119. void popFront() {
  120. assert (!this.empty);
  121. this._first = this._first.next;
  122. synchronized(countLock) {
  123. count--;
  124. }
  125. }
  126.  
  127. ///ditto
  128. T front() const {
  129. assert (!this.empty);
  130. return this._first.payload;
  131. ////}
  132. ////else {
  133. //// return null;
  134. ////}
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement