In the process of doing the project sometimes in order to improve efficiency, using a multi-threaded approach to the task of segmentation and application, and later found that the use of thread pool method can better use threads resources to calculate tasks, online There are many examples of how to run the thread pool, MSDN also gave a corresponding example: https:// msdn.microsoft.com/en-us/library/windows/desktop/ms686980 (v=vs.85). aspx
Interested people can go to see, here I give a simple demo, using the thread Pool A single call multiple times, examples are as follows:
[CPP]View PlainCopy
- #include <Windows.h>
- #include <iostream>
- #include <cstdlib>
- Using namespace std;
- #define THREAD_NUM 10
- struct TEMPSTRUCT
- {
- int A;
- int b;
- };
- VOID CALLBACK simplecallback (ptp_callback_instance INSTANCE,void* Context);
- VOID CALLBACK workcallbacktemp (ptp_callback_instance INSTANCE, void* Context, ptp_work work);
- void Main ()
- {
- Ptp_work Tpwork[thread_num];
- Tempstruct Transferstruct[thread_num];
- For ( int i = 0; i< thread_num; i++)
- {
- TRANSFERSTRUCT[I].A = i;
- transferstruct[i].b = i+1;
- }
- //One-time job submission
- Trysubmitthreadpoolcallback (Simplecallback,&transferstruct[2],null);
- For ( int i = 0; i< thread_num; i++)
- Tpwork[i] = createthreadpoolwork (workcallbacktemp,&transferstruct[i],null);
- //Submit work
- For ( int i = 0; i< thread_num; i++)
- Submitthreadpoolwork (Tpwork[i]);
- //wait for work to end
- For ( int i = 0; i< thread_num; i++)
- Waitforthreadpoolworkcallbacks (Tpwork[i],false);
- //Close Work Object
- For ( int i = 0; i< thread_num; i++)
- Closethreadpoolwork (Tpwork[i]);
- System ("pause");
- }
- VOID CALLBACK simplecallback (ptp_callback_instance INSTANCE,void* Context)
- {
- Tempstruct *pt = (tempstruct *) Context;
- int pruduct = Pt->a * pt->b;
- cout <<"Simple struct A:" <<pt->a<<"struct_temp.b:" <<pt->b<<" Pruduct: "<<pruduct<<endl;
- }
- VOID CALLBACK workcallbacktemp (ptp_callback_instance INSTANCE, void* Context, ptp_work work)
- {
- Tempstruct *pt = (tempstruct *) Context;
- int sum = pt->a + pt->b;
- cout <<"work struct A:" <<pt->a<<"struct_temp.b:" <<pt->b<<"sum:" <<sum<<endl;
- }
PS has a Web site of the tutorial written very well, the benefits of a lot of people can also go to see: http://www.cnblogs.com/kzloser/archive/2013/03/11/2909221.html
Be a mark and hope to be helpful to those who need it!
Multi-tasking assignment and operation with thread pool under windows