Skip to content

Commit a1bdd63

Browse files
committed
add: reading sorter data
1 parent beb100b commit a1bdd63

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

include/SortingAlgorithmVisualizer/CommonTypes.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct RandomizeTask
2020

2121
void* data {};
2222
size_t elementCount {};
23+
24+
std::mutex* dataMutex {};
2325
};
2426

2527

include/SortingAlgorithmVisualizer/Sorters/ISorter.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <SortingAlgorithmVisualizer/CommonTypes.hpp>
44

5+
#include <mutex>
6+
57

68
class ISorter
79
{
@@ -12,9 +14,16 @@ class ISorter
1214
virtual bool step() = 0;
1315
virtual void reset() = 0;
1416

17+
bool tryReading();
18+
void startReading();
19+
void stopReading();
20+
1521
RandomizeTask& getRandomizeTask();
1622

1723

1824
protected:
1925
RandomizeTask mRandomizeTask {};
26+
27+
std::mutex mDataMutex {};
28+
std::unique_lock <std::mutex> mDataLock {mDataMutex, std::defer_lock};
2029
};

include/SortingAlgorithmVisualizer/Sorters/ISorterSpecialized.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ISorterSpecialized : public ISorter
1515
mRandomizeTask.callback = RandomizePlotData <T>;
1616
mRandomizeTask.data = mData->data();
1717
mRandomizeTask.elementCount = mData->size();
18+
mRandomizeTask.dataMutex = &mDataMutex;
1819
}
1920

2021

include/SortingAlgorithmVisualizer/Sorters/MockSorter.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <SortingAlgorithmVisualizer/Sorters/ISorterSpecialized.hpp>
44

5+
#include <thread>
6+
57

68
template <typename T>
79
class MockSorter : public ISorterSpecialized <T>
@@ -27,6 +29,17 @@ class MockSorter : public ISorterSpecialized <T>
2729
if ( this->mData == nullptr )
2830
return true;
2931

30-
return ++mCounter == this->mData->size();
32+
33+
// simulate sorting operation
34+
const bool isSorted =
35+
++mCounter == this->mData->size();
36+
37+
38+
std::lock_guard <std::mutex> lock (this->mDataMutex);
39+
40+
// simulate accumulated write operations
41+
std::this_thread::sleep_for(std::chrono::milliseconds{1});
42+
43+
return isSorted;
3144
}
3245
};

src/ISorter.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
#include <SortingAlgorithmVisualizer/Sorters/ISorter.hpp>
22

33

4+
bool
5+
ISorter::tryReading()
6+
{
7+
return mDataLock.try_lock();
8+
}
9+
10+
void
11+
ISorter::startReading()
12+
{
13+
mDataLock.lock();
14+
}
15+
16+
void
17+
ISorter::stopReading()
18+
{
19+
mDataLock.unlock();
20+
}
21+
422
RandomizeTask&
523
ISorter::getRandomizeTask()
624
{

src/SortingAlgorithmVisualizer.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <SortingAlgorithmVisualizer/CommonTypes.hpp>
33
#include <SortingAlgorithmVisualizer/Sorters/MockSorter.hpp>
44

5-
#include <vector>
65
#include <thread>
76
#include <condition_variable>
87

@@ -108,12 +107,14 @@ randomizerThreadProc(
108107

109108
if ( task != nullptr )
110109
{
111-
task->callback(task->data, task->elementCount);
110+
{
111+
std::lock_guard <std::mutex> lock (*task->dataMutex);
112+
task->callback(task->data, task->elementCount);
113+
}
112114

113115
{
114-
std::unique_lock <std::mutex> lock (task->taskFinishedMutex);
116+
std::lock_guard <std::mutex> lock (task->taskFinishedMutex);
115117
task->callback = nullptr; // mark task as done
116-
lock.unlock();
117118
}
118119

119120
task->taskFinishedSignal.notify_one();
@@ -131,9 +132,6 @@ main()
131132

132133
ThreadSharedData sharedState {};
133134

134-
std::vector <ThreadLocalData> threadData (
135-
plotCount, {sharedState} );
136-
137135
PlotData <int> testData {};
138136
testData.init(1000);
139137

@@ -146,25 +144,48 @@ main()
146144
testData1[i] = i;
147145
}
148146

149-
threadData[0].sorter = new MockSorter <int> (testData);
150-
threadData[1].sorter = new MockSorter <int> (testData1);
151147

152-
std::vector <std::thread> threads {};
153-
threads.reserve(plotCount);
148+
ThreadLocalData threadsData[plotCount]
149+
{
150+
{sharedState, new MockSorter <int> (testData)},
151+
{sharedState, new MockSorter <int> (testData1)},
152+
};
153+
154+
std::thread sorterThreads[plotCount] {};
155+
156+
for ( size_t i {}; i < plotCount; ++i )
157+
sorterThreads[i] = std::thread{sorterThreadProc, &threadsData[i]};
154158

155-
threads.emplace_back(sorterThreadProc, &threadData[0]);
156-
threads.emplace_back(sorterThreadProc, &threadData[1]);
157159

158160
auto randomizerThread = std::thread(
159161
randomizerThreadProc, &sharedState );
160162

161163

164+
for ( size_t frame {}; frame < 1000; ++frame )
165+
{
166+
for ( size_t i {}; i < plotCount; ++i )
167+
{
168+
if ( threadsData[i].sorter->tryReading() == true )
169+
{
170+
// simulate copying to back buffer
171+
std::this_thread::sleep_for(std::chrono::milliseconds{5});
172+
173+
threadsData[i].sorter->stopReading();
174+
}
175+
}
176+
177+
// write to vertex buffer
178+
// glVertexAttribDivisor + glDrawArraysInstanced
179+
// swap
180+
}
181+
162182
std::this_thread::sleep_for(std::chrono::seconds{1});
163183

184+
164185
sharedState.shutdownRequested.store(
165186
true, std::memory_order_relaxed );
166187

167-
for ( auto&& thread : threads )
188+
for ( auto&& thread : sorterThreads )
168189
thread.join();
169190

170191
sharedState.sorterThreadsAreDead.store(
@@ -180,5 +201,8 @@ main()
180201

181202
randomizerThread.join();
182203

204+
for ( auto&& threadData : threadsData )
205+
delete threadData.sorter;
206+
183207
return 0;
184208
}

0 commit comments

Comments
 (0)