Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <time.h>
  4. #include <pthread.h>
  5. #include <atomic>
  6. #include <string>
  7. #include <chrono>
  8.  
  9. class WriteDescriptor {
  10. public:
  11. bool op;
  12. int value;
  13.  
  14. WriteDescriptor(bool o) {
  15. //pop
  16. op = o;
  17. }
  18. WriteDescriptor(bool o) {
  19. //constroctor for push
  20. }
  21.  
  22. };
  23.  
  24. class Desc {
  25. public:
  26. int size;
  27. WriteDescriptor<int>* dop;
  28.  
  29. Desc(bool op) {
  30. //pop
  31. dop = new WriteDescriptor<int>(op);
  32. }
  33.  
  34. Desc(int x, bool op) {
  35. // constructor for push
  36. }
  37.  
  38. // initialize size
  39.  
  40. };
  41.  
  42. class Node {
  43. public:
  44. int value;
  45. bool fnode;
  46. Node *next;
  47.  
  48. //constructors
  49.  
  50. };
  51.  
  52.  
  53. class Stack {
  54.  
  55. public:
  56. atomic<Node<int>*> head;
  57. atomic<Node<int>*> garbage;
  58. atomic<Desc<int>*> descriptor;
  59. atomic<int> numOps;
  60.  
  61. Stack() {
  62. //constructor
  63. }
  64.  
  65. bool complete() {
  66. Desc<int>* d = descriptor.load();
  67. if(d->dop->op == false) {
  68. //push
  69. //code for completing push
  70. return true;
  71. }
  72. else {
  73. //pop
  74. //code for completing push
  75.  
  76. return true;
  77. }
  78. }
  79.  
  80. bool push(int x) {
  81.  
  82. Desc<int> *d = new Desc<int>(x, false);
  83. Desc<int> *old_d = descriptor.load();
  84.  
  85. do {
  86. d->size = old_d->size + 1;
  87. complete();
  88. }
  89. while(!descriptor.compare_exchange_strong(old_d, d));
  90. complete();
  91. atomic_fetch_add(&numOps, 1);
  92.  
  93. return true;
  94. }
  95.  
  96. bool pop() {
  97. Desc<int> *d = new Desc<int>(true);
  98. Desc<int> *old_d = descriptor.load();
  99.  
  100.  
  101. do {
  102. if(old_d->size > 0)
  103. d->size = old_d->size - 1;
  104.  
  105. complete();
  106. }
  107. while(!descriptor.compare_exchange_strong(old_d, d));
  108.  
  109.  
  110. complete();
  111. atomic_fetch_add(&numOps, 1);
  112.  
  113. return true;
  114.  
  115. }
  116.  
  117. bool collect_garbage(Node<int> *n) {
  118.  
  119. Node<int> *old_garbage_head = garbage.load();
  120. do {
  121. n->next = old_garbage_head;
  122. } while(!garbage.compare_exchange_weak(old_garbage_head, n));
  123.  
  124. return true;
  125. }
  126. };
  127.  
  128.  
  129.  
  130. int main() {
  131.  
  132. return 0;
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement