Multithreading slows down main thread

  c++, database, multithreading, performance

I’ll do my best to replicate this issue, since it’s quite complicated. As an overview, I have a program that displays graphs using OpenGL. This is thread specific so I know the rendering is only done on one thread. My other thread queries a database and stores the data in a copy vector. Once the thread is finished, it swaps the data with the data the OpenGL thread is using (After joining the thread with the main one). In theory there is nothing about this that should make the program run so slow?

The extremely odd part of this is how it eventually "warms up" and runs much faster after a while (it varies quite a bit, sometimes almost instantaneously, sometimes after 30s of runtime). From value’s side of thing to compare, the program begins running at about 30-60 fps whilst querying the data (as in, constantly loading it and swapping it and joining the threads), but then once it has warmed up it runs at 1000 fps.

I have tested some things out, beginning with making the query take A LONG time to run. During this, the fps is at a max of what it would be (3000+). It is only when the data is constantly being changed (swapping vectors) that is starts to run very slow. It doesn’t make sense that this alone is causing the performance hit since it runs very well after it’s "warmed up". Here is a general overview of the code. I can’t make a minimal example as it needs to take a while to query the data (I’d need to link a database of size 2gb+). However, is there anything inherently bad / obvious about this that would be the root of issue?

Microsoft Access database, sqlext.h,windows.h headers to get the data in the program, however the loading of the data i’ve found is not the issue:

Code overview (logic part)

if (thread.joinable() == true){
    thread.join();
    SwapData(); //Swaps the vectors
}

if (Database.IsQuerying() == false &&  thread.joinable() == false){
    thread = std::thread(LoadData);
}

SwapData

dates = std::move(datesCopy);
//some others

LoadData

querying = true;

//Load data into copy vectors, ie, 
datesCopy[i] = "2021-01-01";

querying = false;

Source: Windows Questions C++

LEAVE A COMMENT