Multi-tasking assignment and operation with thread pool under windows

Source: Internet
Author: User

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
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. Using namespace std;
  5. #define THREAD_NUM 10
  6. struct TEMPSTRUCT
  7. {
  8. int A;
  9. int b;
  10. };
  11. VOID CALLBACK simplecallback (ptp_callback_instance INSTANCE,void* Context);
  12. VOID CALLBACK workcallbacktemp (ptp_callback_instance INSTANCE, void* Context, ptp_work work);
  13. void Main ()
  14. {
  15. Ptp_work Tpwork[thread_num];
  16. Tempstruct Transferstruct[thread_num];
  17. For ( int i = 0; i< thread_num; i++)
  18. {
  19. TRANSFERSTRUCT[I].A = i;
  20. transferstruct[i].b = i+1;
  21. }
  22. //One-time job submission
  23. Trysubmitthreadpoolcallback (Simplecallback,&transferstruct[2],null);
  24. For ( int i = 0; i< thread_num; i++)
  25. Tpwork[i] = createthreadpoolwork (workcallbacktemp,&transferstruct[i],null);
  26. //Submit work
  27. For ( int i = 0; i< thread_num; i++)
  28. Submitthreadpoolwork (Tpwork[i]);
  29. //wait for work to end
  30. For ( int i = 0; i< thread_num; i++)
  31. Waitforthreadpoolworkcallbacks (Tpwork[i],false);
  32. //Close Work Object
  33. For ( int i = 0; i< thread_num; i++)
  34. Closethreadpoolwork (Tpwork[i]);
  35. System ("pause");
  36. }
  37. VOID CALLBACK simplecallback (ptp_callback_instance INSTANCE,void* Context)
  38. {
  39. Tempstruct *pt = (tempstruct *) Context;
  40. int pruduct = Pt->a * pt->b;
  41. cout <<"Simple struct A:" <<pt->a<<"struct_temp.b:" <<pt->b<<"  Pruduct: "<<pruduct<<endl;
  42. }
  43. VOID CALLBACK workcallbacktemp (ptp_callback_instance INSTANCE, void* Context, ptp_work work)
  44. {
  45. Tempstruct *pt = (tempstruct *) Context;
  46. int sum = pt->a + pt->b;
  47. cout <<"work struct A:" <<pt->a<<"struct_temp.b:" <<pt->b<<"sum:"  <<sum<<endl;
  48. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.