Created
February 26, 2018 09:21
-
-
Save martende/d17ccf744f6b3db9f10101a5935251bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <cstring> | |
#include <map> | |
#include <memory> | |
#include <vector> | |
#include <set> | |
#include <ctime> | |
#include <chrono> | |
#include <bitset> | |
#include <thread> | |
#include <cstdarg> | |
#include <fstream> | |
#include <mutex> | |
class Bucket2 { | |
public: | |
bool ok ; | |
int id ; | |
Bucket2(int); | |
//void dump(std::ostream &stream); | |
~Bucket2() { | |
printf("%d %p %p\n",this->id,this); | |
ok = false; | |
} | |
}; | |
Bucket2::Bucket2(int _id) { | |
ok = true; | |
id = _id; | |
} | |
std::shared_ptr<Bucket2> oneBucket; | |
std::mutex mtx; | |
double x; | |
void reader() { | |
while ( 1) { | |
mtx.lock(); | |
auto b = oneBucket; | |
mtx.unlock(); | |
if ((bool)b) { | |
x = b->id; | |
} | |
} | |
} | |
void writer() { | |
int i=0; | |
while ( 1) { | |
//Bucket2 *bkt = new Bucket2(++i); | |
//auto tb = std::shared_ptr<Bucket2>(bkt); | |
mtx.lock(); | |
oneBucket = std::make_shared<Bucket2>(++i); | |
mtx.unlock(); | |
} | |
} | |
int main(int argc,char** argv) { | |
std::thread _th1(reader); | |
std::thread _th2(writer); | |
_th1.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment