Jonathan Woollett-light Jonathan Woollett-light - 25 days ago 9
C++ Question

'_Thr = _Other._Thr;' break point

With most errors I often have some idea in what direction I need to head to it, but here I have no idea.

So let's start with the breakpoint itself:

enter image description here

There are relatively few times any interactions are mad using the thread library, here are all of them:
The inclusion:

#include <thread>

The usage:

void evolve(double* bestN, double* bestP, double* bestM, double* bestQ, double* bestWinConstant) {

std::thread threadArray[threadAmount];

for (int i = 0; i < 100000; i++) {
for (int t = 0; t < threadAmount; t++) {
if (gameArrayInUse[t] == 0) {
copyArray(gameArray[t], startArray);
gameArrayInUse[t] = 1;
threadArray[t] = std::thread(playGames, i, t);
std::cout << "------------New Thread Spawned, No: " << t << std::endl;
if (t == threadAmount - 1) {
t = -1;

for (int i = 0; i < threadAmount; i++)
std::cout << "JOIN THREADS--------------------------------------------------" << std::endl;

The error as seen in the console is (should be noted this changes every time):

Start of evolution on magnitude:1
------------New Thread Spawned, No: 0Completed:
------------New Thread Spawned, No: 1

Due to my lacking knowledge about this I apologise if you find the content I provide about my code lacking in certain areas, I would implore you to inform me via a comment so I can amend it.

Answer Source

The "breakpoint" is actually triggered by _STD terminate(), one line above. (Especially when debugging native code, I find that it's a healthy habit to look at the line above the one that seems to have caused an error when it looks inexplicable.) Per the condition that wraps it, it's triggered when you move a thread on top of an existing thread that is running.

C++ does not allow you to overwrite running thread objects. Before running each iteration of your for(i) loop, you should ensure that every thread created in your for(t) loop has terminated. You can do that by calling thread::join. Alternatively, you can call thread::detach to let the thread run on its own without an object representing it, but you won't have any way to check for its completion.

You must also ensure that every thread has completed before the function returns, or else you'll get a similar error when the running thread objects are destroyed.