Reputation: 652
I'm currently trying to implement a multithreaded database connection interface for personal use. This is implemented using 3 classes
DatabaseManager
A database connexion instancier instantiating a new thread for each connectionDatabase
an object inheriting from QThread, meant to be run in the main thread and exposing an interface to do things on a worker objectDatabaseWorker
the worker object that runs an event loop in a worker thread managing a single database connection, awaits queries and deliver results through Qt:QueuedConnections (signal/slots)close()
method), it's related event loop is stopped and the thread is deleted. The purpose of that is to keep thread creation and deletion at a minimum.this architecture have a lot of success so far with a limited number of threads (less than 10 connections usually). My main problem is that I don't know how it will behave if I have 100+ connections which mean 100+ threads for one application which bring my main question
Is having a lot of thread in an application counter productive? what happens if I open 50/100/500/1000 connections at the same time? if I do that, I/O database operations are going to take a lot of time to complete (it has not been designed for that in a first place) but how much of that lag is caused by the number of threads?
Upvotes: 1
Views: 352
Reputation: 41208
Number of threads in of itself is not a huge problem. Each one uses some memory but for modern machines that overhead is not huge.
The main problem comes when all those threads start trying to do work at once, they start fighting for resources and you can end up with a slower result than you get from a smaller number of threads working smarter.
A thread pool is a common solution for this sort of situation, where tasks get queued up to be performed and you have a thread pool with a suitable number of threads for your machine (for example one thread per core) that processes items off the queue. If your threads spend a lot of time idle waiting for responses then more threads might be warranted, if they are constantly doing calculations though then adding more might well actually slow things down.
Measure the performance and adjust accordingly :) "Gut feel" is very unreliable when working on performance.
Upvotes: 2
Reputation: 5336
I'd say that yes, it's counter productive. Each thread uses up a significant chunk of memory for its stack space and if you only have 6 cores you cannot run 100s of threads in parallel anyways. Use a thread pool instead, e.g. via KF5 ThreadWeaver or Qt Concurrent and pay attention to the ideal thread count.
Upvotes: 0