|
Threading Library 1.02
A simple to use task based, work stealing Threading Library.
|
First, Threading Library is Free to use for anyone for any purpose with the exception that NO ONE MAY redistribute the code, in part or in total, representing it as their work. Credit to Scott Lee, at NoLimitsDesigns.com MUST be given as well as notice of this license. This License is good for the current version of this Library which is currently 1.01. This does not preclude newer versions from holding a differnet license agreement; however, previous versions shall maintain their original license agreement.
Download the files.
This is my first release. Currently, I have only had enough time to test a 32 bit and 64 bit build on Windows 7, compiled using Microsoft Visual Studio 2010. Support for Unix and Linux will be soon, but until then, if you are determined, the change in code should take about 15 minutes. I Currently do not develope in Linux, so it is not a priority for me, but I will get around to it.
I AM NO LONGER UPDATING THIS LIBRARY. The code as well as this web page will remain so that others can learn from it.#include "stdafx.h" #include <iostream> #include "tchar.h" #include "Threading\Threading.h" using namespace std; float global; class thrdworking: public Threading::cParallel_For_Work { public: void operator()(unsigned int beg, unsigned int end) const{ for(unsigned int be(beg); be!= end; be++){ global += sqrt(static_cast<float>(be)); } } }; class singletask: public Threading::cTask_Work { public: virtual void execute() const{ cout<<"Task completed"<<endl; } }; int _tmain(int argc, _TCHAR* argv[]){ cout << "Prallel for! First number is the beginning, second is the end, the last is the number is splits."<<endl; unsigned int beg, end, splits; cin>>beg>>end>>splits; thrdworking trd; cout<<"Starting Parallel For Work"<<endl; Threading::Parallel_For(beg, end, &trd, splits); cout<<"Finished Parallel For Work"<<endl; // this shows how to start a task that the threading library handles and frees the memory upon completion of the task. In this case, you cannot wait directly on the task since it will be destroyed upon completion. // instead, you must wait on the thread to complete its work to tell if the task is done. Or, add code to the task that sets a variable when the task completes that you can check on afterwards size_t threadid = Threading::RunTask(new singletask, true);// the threading library will free the memory when completed cout<<"Starting task"<<endl; Threading::Wait(threadid);// this waits on a specific thread to finish with its work. This is faster than WaitForAll since that waits on all threads to complete their work cout<<"Finished waiting on the thread to complete its work "<<endl; // this is how to create a task and wait directly on the task itself to finish running. You must delete it yourself upon completion singletask *task1=new singletask; threadid = Threading::RunTask(task1, false);// DO NOT TAKE OWNERSHIP OF THE task. I will delete it myself and I can now check when the task is done directly on the task itself cout<<"Starting task 1"<<endl; Threading::Wait(task1);// wait for the task to complete delete task1;// this MUST BE DONE if the threading library does not have ownership, otherwise a memory leak will uccur cout<<"Finished waiting task 1. "<<endl; }
1.7.4