Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <random>
- #include <set>
- #include <atomic>
- using t = int64_t;
- //std::atomic<t> x = 0;
- t x = 0;
- std::set<t> values;
- void fw(t a){
- while(true){
- //x.store(a);
- x = a;
- }
- }
- void fr(){
- while(true){
- //auto xr = x.load();
- auto xr = x;
- if(!( (xr==1) || (xr==-1) || (xr==33333333333) || (xr==-33333333333) || (xr==527684798377) || (xr==6))){
- //if(values.find(xr) != values.end()){
- std::cout << xr << '\n';
- std::exit(1);
- }
- }
- }
- std::vector<std::thread> w_threads;
- void run_writers(){
- for(auto v : values){
- w_threads.emplace_back(fw, v);
- }
- }
- std::vector<std::thread> r_threads;
- void run_readers(){
- for(auto it : values){
- r_threads.emplace_back(fr);
- }
- }
- int main(){
- //int n=10;
- //std::random_device rd;
- //std::mt19937 gen(rd());
- //std::uniform_int_distribution<t> dist(0, std::numeric_limits<t>::max());
- //while(n--){
- // values.insert(dist(gen));
- //}
- values = {1, -1, 33333333333, -33333333333, 527684798377, 6};
- std::cout << "Values:\n";
- for(auto v: values){
- std::cout << v <<'\n';
- }
- std::cout << std::endl;
- std::cout << "Running..." << std::endl;
- run_writers();
- run_readers();
- for(auto &th:w_threads){
- th.join();
- }
- for(auto &th:r_threads){
- th.join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement