Advertisement
Guest User

Untitled

a guest
May 26th, 2025
23
0
313 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <random>
  4. #include <set>
  5.  
  6. #include <atomic>
  7.  
  8. using t = int64_t;
  9.  
  10. //std::atomic<t> x = 0;
  11. t x = 0;
  12.  
  13. std::set<t> values;
  14.  
  15. void fw(t a){
  16.     while(true){
  17.         //x.store(a);
  18.         x = a;
  19.     }
  20. }
  21.  
  22.  
  23. void fr(){
  24.     while(true){
  25.         //auto xr = x.load();
  26.         auto xr = x;
  27.         if(!( (xr==1) || (xr==-1) || (xr==33333333333) || (xr==-33333333333) || (xr==527684798377) || (xr==6))){
  28.         //if(values.find(xr) != values.end()){
  29.             std::cout << xr << '\n';
  30.             std::exit(1);
  31.         }
  32.     }
  33. }
  34.  
  35. std::vector<std::thread> w_threads;
  36.  
  37. void run_writers(){
  38.     for(auto v : values){
  39.         w_threads.emplace_back(fw, v);
  40.     }
  41. }
  42.  
  43. std::vector<std::thread> r_threads;
  44.  
  45. void run_readers(){
  46.     for(auto it : values){
  47.         r_threads.emplace_back(fr);
  48.     }
  49. }
  50.  
  51.  
  52. int main(){
  53.     //int n=10;
  54.     //std::random_device rd;
  55.     //std::mt19937 gen(rd());
  56.     //std::uniform_int_distribution<t> dist(0, std::numeric_limits<t>::max());
  57.     //while(n--){
  58.     //    values.insert(dist(gen));
  59.     //}
  60.  
  61.     values = {1, -1, 33333333333, -33333333333, 527684798377, 6};
  62.  
  63.     std::cout << "Values:\n";
  64.     for(auto v: values){
  65.         std::cout << v <<'\n';
  66.     }
  67.     std::cout << std::endl;
  68.  
  69.  
  70.     std::cout << "Running..." << std::endl;
  71.  
  72.     run_writers();
  73.     run_readers();
  74.  
  75.     for(auto &th:w_threads){
  76.         th.join();
  77.     }
  78.  
  79.     for(auto &th:r_threads){
  80.         th.join();
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement