8000 GitHub - surinkim/WinThreadPoolWrapper: A simple wrapper class for using windows thread pool apis
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

surinkim/WinThreadPoolWrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

Introduction

WinThreadPoolWrapper is a simple wrapper class for windows thread pool apis, such as CreateThreadpool().
Using by this, we don't need to know detaily those windows thread pool apis.

How to use

This project is written in VS2010.

First, you should write your own work thread. For example, below code is work thread which prints odd numbers between min and max number. Let's say this function's name is MyWorkCallback().

    VOID CALLBACK MyWorkCallback(PTP_CALLBACK_INSTANCE instance, PVOID parameter, PTP_WORK work)
    {	
        if(parameter == nullptr) { return; }

	    ParamInfo* param_info = static_cast<ParamInfo*>(parameter);
    	for(int i = param_info->min_number_; i < param_info->max_number_; i++)
    	{
    		if((i % 2) == 1)
    		{
    			AcquireSRWLockExclusive(&g_lock);
    			cout << "[thread id = " << GetCurrentThreadId() << "] find number = " << i << endl;
    			g_found_count++;
    			cout << "[found count = " << g_found_count << "]" << endl;
    			ReleaseSRWLockExclusive(&g_lock);
    		}
    		Sleep(100);
    	}

    	return;
    }

And then, we simply use above function using by ThreadPoolWrapper. Like this, in your main,

    int main(int argc, char* argv[])
    {
    	//Init SRWLOCK object
    	InitializeSRWLock(&g_lock);
    
    	//Init ThreadPoolWrapper
    	ThreadPoolWrapper wrapper;
    	wrapper.Init();
    	
    	//Set Min/Max Thread Count
    	wrapper.SetThreadCount(1, 3);
    
    	//put callback with pram into the Threadpool
    	ParamInfo infos[] = {ParamInfo(1, 50), ParamInfo(51, 100)};
    	for_each(std::begin(infos), std::end(infos), [&](ParamInfo& info)
    	{
    		wrapper.SetCallback(MyWorkCallback, static_cast<PVOID>(&info));
    	});
    
    	//Wait for Callback finish
    	wrapper.WaitCallbackFinish();
    
    	getchar();
    
    	return 0;
    }

References

Author

About

A simple wrapper class for using windows thread pool apis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0