Reputation: 11
I have C++ program with HMI. Everytime user clicks/presses button on HMI, some calculations are performed. If it is a single click then I do the calculation once and if it is a long press I keep calculating till user releases the button.
In either case I use boost::thread to do the calculation(s).
I create the thread per each calculation as follows:
additional_trajectory_calc_thread = boost::thread(boost::bind(&MyClass::populate, this, arg1, arg2,arg3)); additional_trajectory_calc_thread.detach()
(additional_trajectory_calc_thread is a private member of my class)
Every time user clicks once on button this thread is created once. And every time user presses on the button, one thread is created every 1 second till he releases the button.
When I first run the program, my program is very responsive, hence no performance issues. But when total number of threads created reaches around 300 (like 50 single clicks + 250 seconds of press time In total) system gets slower.
Why might this be happening? Is it allocating additional memory each time the thread is created? Might that be the reason? Or something else? How can I check and find the root cause?
Thanks in advance.
Upvotes: 1
Views: 121
Reputation: 249153
You should not be creating threads every time the user clicks. Just create one thread or a small "thread pool" and use this on every click.
The performance degradation from having too many threads varies by platform. On Linux it is not too bad, but there is an upper limit to the number of threads you can create. Most programs create only a very small number of threads, and reuse them for multiple activities.
Upvotes: 1