Threads come with overhead for creation - thus it is usually a bad idea to start and stop threads too frequently.
I would recommend you to work with two shared queues (Thread::Queue). One queue would be the queue to the threads which try to ssh, the other queue would send data from the worker threads to the database thread.
The main issue is now how to synchronize the main thread with the with these threads. Either the main threads sleeps x seconds after the queue to the worker thread is cleaned or all threads stop and the main thread polls from time to time the number of threads.
As you can see it is rather complex. Let me know if you need some example code.