Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // practica3test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <time.h>
- #define THREADS 2
- CRITICAL_SECTION cs;
- int marinoGlobal = 0;
- void NTAPI MyWorkCallback(PTP_CALLBACK_INSTANCE instance, void * context, PTP_WORK work)
- {
- int threadid = *((int *)context);
- int marino = 1;
- for (int i = threadid; i < 100000; i+=THREADS)
- marino++;
- EnterCriticalSection(&cs);
- marinoGlobal++;
- LeaveCriticalSection(&cs);
- }
- int main()
- {
- PTP_POOL pool = CreateThreadpool(NULL);
- TP_CALLBACK_ENVIRON CallBackEnviron;
- clock_t t_inicial, t_final;
- InitializeCriticalSection(&cs);
- InitializeThreadpoolEnvironment(&CallBackEnviron);
- SetThreadpoolThreadMaximum(pool, THREADS);
- SetThreadpoolThreadMinimum(pool, 1);
- SetThreadpoolCallbackPool(&CallBackEnviron, pool);
- TP_WORK * work;
- int pids[THREADS];
- t_inicial = clock();
- for (int i = 0; i < 2000; i++){
- for (int x = 0; x < THREADS; x++){
- pids[x] = x;
- work = CreateThreadpoolWork(MyWorkCallback, &pids[x], &CallBackEnviron);
- SubmitThreadpoolWork(work);
- }
- }
- WaitForThreadpoolWorkCallbacks(work, FALSE);
- CloseThreadpoolWork(work);
- CloseThreadpool(pool);
- t_final = clock();
- printf("En %3.6f segundos\n con %d", ((float)t_final - (float)t_inicial) / CLOCKS_PER_SEC, marinoGlobal);
- DeleteCriticalSection(&cs);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement