Skip to content

Instantly share code, notes, and snippets.

@wallstop
Created February 28, 2014 03:51
Show Gist options
  • Save wallstop/9264885 to your computer and use it in GitHub Desktop.
Save wallstop/9264885 to your computer and use it in GitHub Desktop.
Test of DX/ConcurrentQueue.h
#include <ConcurrentDX/ConcurrentDX.h>
#include <thread>
#include <atomic>
#include <iostream>
#include <vector>
using namespace DX;
ConcurrentQueue<size_t> buffer;
std::atomic<size_t> count;
SpinMutex coutMutex;
#define NUM_LOOPS 1000000000UL
void writeToBuffer()
{
for(size_t i = 0; i < NUM_LOOPS; ++i)
{
buffer.push(i);
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void readFromBuffer2()
{
std::vector<int> output;
while(count < NUM_LOOPS)
{
size_t temp;
if(buffer.pop(temp))
{
output.push_back(temp);
++count;
}
}
bool sorted = true;
for(size_t i = 0; i < output.size() - 1; ++i)
{
if(output[i] > output[i + 1])
{
{
SpinLock _lock(coutMutex);
std::cout << i << " " << i + 1 << std::endl;
}
sorted = false;
break;
}
}
{
SpinLock _lock(coutMutex);
if(sorted)
std::cout << "It worked!" << std::endl;
else
std::cout << "It didn't work :(" << std::endl;
}
}
void readFromBuffer1()
{
std::vector<int> output;
while(count < NUM_LOOPS)
{
size_t temp;
if(buffer.pop(temp))
{
output.push_back(temp);
++count;
}
}
bool sorted = true;
for(size_t i = 0; i < output.size() - 1; ++i)
{
if(output[i] > output[i + 1])
{
{
SpinLock _lock(coutMutex);
std::cout << i << " " << i + 1 << std::endl;
}
sorted = false;
break;
}
}
{
SpinLock _lock(coutMutex);
if(sorted)
std::cout << "It worked!" << std::endl;
else
std::cout << "It didn't work :(" << std::endl;
}
}
void readFromBuffer()
{
std::vector<int> output;
while(count < NUM_LOOPS)
{
size_t temp;
if(buffer.pop(temp))
{
output.push_back(temp);
++count;
}
}
bool sorted = true;
for(size_t i = 0; i < output.size() - 1; ++i)
{
if(output[i] > output[i + 1])
{
{
SpinLock _lock(coutMutex);
std::cout << i << " " << i + 1 << std::endl;
}
sorted = false;
break;
}
}
{
SpinLock _lock(coutMutex);
if(sorted)
std::cout << "It worked!" << std::endl;
else
std::cout << "It didn't work :(" << std::endl;
}
}
int main(int argc, char* argv[])
{
count = 0;
//std::cout << "atomic: " << sizeof(std::atomic<int*>) << " int: " << sizeof(int*) << " char: " << sizeof(char*) << std::endl;
//SpinLock _lock(spinMutex);
// DX::SpinRWLock myLock(spinRWMutex, true);
std::thread writerThread1(writeToBuffer);
//std::thread writerThread2(writeToBuffer);
// std::thread writerThread3(writeToBuffer);
std::thread readerThread(readFromBuffer);
std::thread readerThread1(readFromBuffer1);
std::thread readerThread2(readFromBuffer2);
writerThread1.join();
//writerThread2.join();
//writerThread3.join();
readerThread.join();
readerThread1.join();
readerThread2.join();
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment