first commit
This commit is contained in:
3
BreakMemory/CMakeLists.txt
Normal file
3
BreakMemory/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/Cpp/CMakeLists.txt)
|
||||
37
BreakMemory/Cpp/CMakeLists.txt
Normal file
37
BreakMemory/Cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(CppBreakMemory)
|
||||
set(CURRENT_PROJECT_CODE_NAME breakmemory)
|
||||
set(FILE_EXT cpp)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
list(APPEND EXTRA_LIBS "Threads::Threads")
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
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