.


:




:

































 

 

 

 


CRITICAL_SECTION




, , CRITICAL_SECTION . statsMX.c ( 9.1) . statsCS.c, , Web- , , CRITICAL_SECTION, stats IN. (interlocked functions). , statsNS., , ; , , . . , 9.1 64 .

, :

.

, , , .

CS.

, , , .

9.1. statsMX:

/* 9. statsMX.c */

/* "/", */

/* .*/

/* , . */

#include "EvryThng.h"

#define DELAY_COUNT 20

/* : statsMX nthread ntasks */

/* "nthread" , */

/* "ntasks" . */

/* */

/* , . */

DWORD WINAPI worker(void *);

typedef struct _THARG {

int thread_number;

HANDLE *phMutex;

unsigned int tasks_to_complete;

unsigned int *tasks_complete;

} THARG;

 

int _tmain(DWORD argc, LPTSTR argv[]) {

INT tstatus, nthread, ithread;

HANDLE *worker_t, hMutex;

unsigned int* task_count, tasks_per_thread;

THARG* thread_arg;

/* . */

hMutex = CreateMutex(NULL, FALSE, NULL);

nthread = _ttoi(argv[1]);

tasks_per_thread = _ttoi(argv[2]);

worker_t = malloc(nthread * sizeof(HANDLE));

task_count = calloc(nthread, sizeof(unsigned int));

thread_arg = calloc(nthread, sizeof(THARG));

for(ithread = 0; ithread < nthread; ithread++) {

/* . */

thread_arg[ithread].thread_number = ithread;

thread_arg[ithread].tasks_to_complete = tasks_per_thread;

thread_arg[ithread].tasks_complete = &task_count[ithread];

thread_arg[ithread].phMutex = &hMutex;

worker_t[ithread] = (HANDLE)_beginthreadex (NULL, 0, worker, &thread_arg[ithread], 0, &ThId);

}

/* . */

WaitForMultipleObjects(nthread, worker_t, TRUE, INFINITE);

free(worker_t);

printf(" \n");

for (ithread = 0; ithread < nthread; ithread++) {

_tprintf(_T(" , %5d: %6d\n"), ithread, task_count[ithread]);

}

return 0;

free(task_count);

free(thread_arg);

}

 

DWORD WINAPI worker(void *arg) {

THARG * thread_arg;

int ithread;

thread_arg = (THARG*)arg;

ithread = thread_arg->thread_number;

while (*thread_arg->tasks_complete < thread_arg->tasks_to_complete) {

delay_cpu(DELAY_COUNT);

WaitForSingleObject(*(thread_arg->phMutex), INFINITE);

(*thread_arg->tasks_complete)++;

ReleaseMutex(*(thread_arg->phMutex));

}

return 0;

}

timep 6 ( 6.2). , , , 250 000 1,2,4, 8, 16, 32, 64 128 , :

(4 ) NS ( ), IN ( ) CS ( CRITICAL_SECTION) . CS (10-20 ), , . , MX () - .

CS 5 . NT5- , , , . 1, 2, 4 , , (Windows 2000, Pentium 1 , ) ( ) 0.5, 1.0, 2.0, 4.0, 14.9, 16.0, 32.1 363.4, (Windows 2000, Pentium 500 , ) 1.2, 2.3, 4.7, 9.3, 42.7, 101.3, 207.8 1212.5 . , , 4 8, , 128.

MX CS, 2:1 10:1 .

SMP- . , , , , MX CS . CS -, .

. .

task_count 32- , " " ("word tearing") " " ("cache line conflict") SMP-. , , (32- Intel x86). , , . , . , 32- . 9.6.





:


: 2015-09-20; !; : 456 |


:

:

. .
==> ...

750 - | 708 -


© 2015-2024 lektsii.org - -

: 0.015 .