I see a number of things that may help you improve your code.
Make sure to #include
all required headers
This program refers to std::vector
and std::thread
but does not include the corresponding headers. Fix that by adding these lines:
#include <thread>
#include <vector>
Check return values
The function rdx.connect()
can fail. Your program should check the return value and handle the case in which it does.
Understand your data structures
The std::vector
is used appropriately except for the peculiar call to pool.empty()
at the end of the code. The purpose for empty()
is to check to see if the vector is empty, not to empty the vector. In any case, it's not necessary to empty it since it will automatically be destroyed when it goes out of scope at the end of main()
.
Understand the thread pool pattern
The question describes this as a "thread pool" but that's not what I see here. It's not a finite set of threads doing some other number of tasks; rather it's one function being called once for each thread. While this is multithreading, it's not a thread pool. To keep from confusing yourself or others, it's useful to use common terms in the usual way.
Avoid concurrency problems
Any time multiple tasks run in parallel, we have to take extra care to assure that things work correctly. In particular, my_cool_function()
claims to "do stuff with rdx", but rdx
is a single global variable. The code for that function isn't shown, but it's highly likely that some form of locking will be required to avoid problems with concurrent access to the single global object.
Omit return 0
at the end of main
The compiler will automatically generate a return 0;
at the end of main
so it is not necessary to supply your own.