Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // practica3test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <time.h>
  9.  
  10. #define THREADS 2
  11.  
  12. CRITICAL_SECTION cs;
  13. int marinoGlobal = 0;
  14.  
  15.  
  16. void NTAPI MyWorkCallback(PTP_CALLBACK_INSTANCE instance, void * context, PTP_WORK work)
  17. {
  18. int threadid = *((int *)context);
  19. int marino = 1;
  20. for (int i = threadid; i < 100000; i+=THREADS)
  21. marino++;
  22. EnterCriticalSection(&cs);
  23. marinoGlobal++;
  24. LeaveCriticalSection(&cs);
  25. }
  26.  
  27.  
  28.  
  29. int main()
  30. {
  31. PTP_POOL pool = CreateThreadpool(NULL);
  32. TP_CALLBACK_ENVIRON CallBackEnviron;
  33. clock_t t_inicial, t_final;
  34. InitializeCriticalSection(&cs);
  35. InitializeThreadpoolEnvironment(&CallBackEnviron);
  36. SetThreadpoolThreadMaximum(pool, THREADS);
  37. SetThreadpoolThreadMinimum(pool, 1);
  38. SetThreadpoolCallbackPool(&CallBackEnviron, pool);
  39. TP_WORK * work;
  40. int pids[THREADS];
  41. t_inicial = clock();
  42.  
  43. for (int i = 0; i < 2000; i++){
  44. for (int x = 0; x < THREADS; x++){
  45. pids[x] = x;
  46. work = CreateThreadpoolWork(MyWorkCallback, &pids[x], &CallBackEnviron);
  47. SubmitThreadpoolWork(work);
  48. }
  49. }
  50. WaitForThreadpoolWorkCallbacks(work, FALSE);
  51. CloseThreadpoolWork(work);
  52. CloseThreadpool(pool);
  53. t_final = clock();
  54. printf("En %3.6f segundos\n con %d", ((float)t_final - (float)t_inicial) / CLOCKS_PER_SEC, marinoGlobal);
  55. DeleteCriticalSection(&cs);
  56. getchar();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement