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