This is just a quick overview on what you need to do to use the thread pool.
Note: In this tutorial, I’ve shown you functions that you need to create the thread pool. There are, however, smaller functions (accessors, mutators, etc.) that I haven’t shown you, but are easy to understand.
Note 2: The example project also was compiled using Visual Leak Detector. If you try to run the sample without VLD, you’ll get a crash. Just comment out the
#includein “main.cpp” or go and download the handy-dandy tool.
Adding the Thread Pool To Your Project
This is also expressed in the README that comes with the sample project in on the download page for this project. Really it’s quite simple, and there are 2 ways to go about it.
Variant 1: Add the library to your project.
This method is a bit hard to get down, but when you do it enough times, you’ll get it. And it’s always right here to reference back to.
- Cut a hole in a box
- Put your j–
WHOA! Wrong instructions!
OK, here they are…
- Put the libKMThreadPool folder from the sample project somewhere on your computer that’s easy to reference to (for example: “C:/C++/libKMThreadPool/”
- Go to Visual Studios and open the solution that contains your project (lets call it “MyProj”)
- Include the project file for libKMThreadPool into your solution
- Right-click on MyProj and open Properties
- In “C++>>General” under “Additional Include Directories,” add in the path to the thread pools’ directory (in this example it would be “C:/C++/libKMThreadPool/”)
- Now, still in Properties, go in “Linker>>Input” under “Additional Dependencies” and add libKMThreadPool.lib
- Now exit Properties, click on MyProj, and click on Project Dependencies.
- Check the checkbox that reads “libKMThreadPool”
That’s all there is to it! If you compile the project now, the output .lib file from libKMThreadPool will appear in the same directory as your solution for MyProj is in. Cool, huh?
Variant 2: Add the source directly into your project.
This is real easy. If for some reason the above method doesn’t work, then just copy all the source files from libKMThreadPool and chuck them directly into your project. Quick and dirty.
Initializing and Shutting Down the Thread Pool
Including the files
The thread pool I have set up are all in namespaces. The primary namespace is
kmp. The threading stuff (such as KMLock, KMThread, etc.) are all stored in
kmp::threading, any Utilities are under
kmp::threading::utility, and any algorithms are in
Since you included the directories, you won’t need some long, horrible-looking #include. The only files you’ll really be interacting with a lot are KMThreadPool_Win32.h” and “KMUtility.h” (and “KMLock.h” maybe).
First thing’s first – when you want to use the thread pool from anywhere, you’ve got to grab the singleton instance of it.
KMThreadPool* pthreadpool = KMThreadPool::getInstance();
That’s easy. If you’re doing this from inside a class definition, you may want to store the instance so you’re not constantly calling getInstance(). Once you have that, now it’s time to call
But wait! I know you’re ready to call
pthreadpool->Initialize(1000, 1000000); but that would be foolish! You processor doesn’t have that much power, but it will try to create at least 1000 threads, but maybe only 4 of them will be actual threads! All the rest will be virtual threads.
Most processors are designed only to really allow 2 threads per core (some have been known to go as far up as 8), so reasonably, if you have a dual-core processor, your max threads should be 4, and min should be 2. If you try to increase that number, you’ll notice your program will run very sluggish.
Now that our thread pool is ready, we can call
BeginProcessing() and start adding tasks!
Once your program is finished, it’s a good idea to clean up the memory this thread pool has allocated. Now, you can call
StopProcessing() and then call
Shutdown() on the thread pool, or you can just call
Shutdown(). Either way, call
Shutdown() once your program is finished. Done.
Adding tasks require a bit of set up. The first thing we want to do is set up a child class of
IKMTaskData. This class is going to contain data needed for your task, like, say,
GameObject* curObj, etc, etc. See the examples in the sample project if you need more explanation, but there really isn’t much to explain here.
Now for the task function itself. Remember: the task function must be static, as explained in Step 2. This task must be set up just like
KMTaskFunc – same return value, same parameter list. If you want to access your data you made in the child of
IKMTaskData from the
IKMTaskData*, you have to cast the
IKMTaskData* to your child class, also as explained in Step 2.
So lets say we have
void MyTask(IKMTaskData*) and
MyTaskData which derives from
IKMTaskData and contains 2 ints and a constructor. How do I get this to the pool?
pthreadpool->AddTask(MyTask, new MyTaskData(10, 67));