first commit
This commit is contained in:
72
BreakMemory/Cpp/breakmemory.cpp
Normal file
72
BreakMemory/Cpp/breakmemory.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <random>
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
|
||||
class Incorrect
|
||||
{
|
||||
public:
|
||||
Incorrect(size_t iterations = 1000)
|
||||
: mIterations(iterations)
|
||||
{
|
||||
isReady = false;
|
||||
done = false;
|
||||
newValue = 0xdeadbeef;
|
||||
}
|
||||
|
||||
void RunAndWait()
|
||||
{
|
||||
std::thread consumer(&Incorrect::Consumer, this);
|
||||
std::thread writer(&Incorrect::Writer, this);
|
||||
|
||||
writer.join();
|
||||
consumer.join();
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
void Consumer()
|
||||
{
|
||||
while (!done)
|
||||
{
|
||||
while (isReady != true);
|
||||
auto print = newValue;
|
||||
isReady = false;
|
||||
printf("New Value: %d\n", print);
|
||||
}
|
||||
}
|
||||
|
||||
void Writer()
|
||||
{
|
||||
for (auto i = int(mIterations); i > 0; i--)
|
||||
{
|
||||
while (isReady != false);
|
||||
|
||||
newValue = i;
|
||||
isReady = true;
|
||||
}
|
||||
|
||||
done = true;
|
||||
}
|
||||
|
||||
const size_t mIterations;
|
||||
volatile bool isReady;
|
||||
volatile int newValue;
|
||||
volatile bool done;
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
Incorrect incorrect;
|
||||
incorrect.RunAndWait();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user