pop-up text- #define N1 3
- #define N2 1000000
- static int
- do_rand(unsigned long ctx)
- {
- /*
- * Compute x = (7^5 * x) mod (2^31 - 1)
- * wihout overflowing 31 bits:
- * (2^31 - 1) = 127773 * (7^5) + 2836
- * From "Random number generators: good ones are hard to find",
- * Park and Miller, Communications of the ACM, vol. 31, no. 10,
- * October 1988, p. 1195.
- */
- long hi, lo, x;
- /* Can't be initialized with 0, so use another value. */
- if (ctx == 0)
- ctx = 123459876;
- hi = ctx / 127773;
- lo = ctx % 127773;
- x = 16807 * lo - 2836 * hi;
- if (x < 0)
- x += 0x7fffffff;
- return x;
- }
- long commonContext;
- char fill[54];
- struct Tls
- {
- long individualContext;
- long myRandomContext;
- int outCounter;
- char fill[54];
- } tls[30];
- HANDLE manual = CreateEvent(0, TRUE, FALSE, 0);
- int moda;
- DWORD WINAPI threadFunc1(LPVOID lpThreadParameter)
- {
- Tls& me = tls[(int) lpThreadParameter];
- me.myRandomContext = (int)&lpThreadParameter;
- long& C = commonContext;
- const int myModa = moda;
- WaitForSingleObject(manual, INFINITE);
- for(int i=0;i
- for(int j=0;j
- {
- long oldVal = C;
- for(;;)
- {
- long newVal = InterlockedCompareExchange(&C, do_rand(oldVal), oldVal);
- if (newVal == oldVal)
- break;
- else
- {
- oldVal = newVal;
- }
- }
- me.myRandomContext = do_rand(me.myRandomContext);
- int limit=(me.myRandomContext %myModa);
- for(int k=0;k
- me.outCounter+=limit+10;
- }
- return 0;
- }
- CRITICAL_SECTION cs;
- DWORD WINAPI threadFunc2(LPVOID lpThreadParameter)
- {
- Tls& me = tls[(int) lpThreadParameter];
- me.myRandomContext = (int)&lpThreadParameter;
- const int myModa = moda;
- long& C = commonContext;
- WaitForSingleObject(manual, INFINITE);
- for(int i=0;i
- for(int j=0;j
- {
- EnterCriticalSection(&cs);
- C = do_rand(C);
- LeaveCriticalSection(&cs);
- me.myRandomContext = do_rand(me.myRandomContext);
- int limit =(me.myRandomContext %myModa);
- for(int k=0;k
- me.outCounter+=limit+10;
- }
- return 0;
- }
- int step(int n)
- {
- int retval = n / 5;
- if (retval==0)
- retval = 1;
- return retval;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- InitializeCriticalSection(&cs);
- HANDLE threads[100];
- unsigned start;
- int tnum=8;
- moda=1000;
- // for(moda=1; moda<20000;moda+=step(moda))
- for (tnum=1;tnum<=18;tnum++)
- {
- for(int i=0;i
- {
- DWORD tid;
- threads[i]=CreateThread(NULL, 0, threadFunc1, (void*)i, 0, &tid );
- }
- Sleep(1000); // naive but enough for dirty test
- start = GetTickCount();
- SetEvent(manual);
- for(int i=0;i
- {
- WaitForSingleObject(threads[i], INFINITE);
- }
- int v1 = GetTickCount()-start;
- ResetEvent(manual);
- for(int i=0;i
- {
- DWORD tid;
- threads[i]=CreateThread(NULL, 0, threadFunc2, (void*)i, 0, &tid );
- }
- Sleep(1000);
- start = GetTickCount();
- SetEvent(manual);
- for(int i=0;i
- {
- WaitForSingleObject(threads[i], INFINITE);
- }
- int v2 = GetTickCount()-start;
- ResetEvent(manual);
- printf("%d %d %f %f\n", tnum, moda,(double(tnum)*1000.)/v1,(double(tnum)*1000.)/v2);
- }
- return 0;
- }
#define N1 3
#define N2 1000000
static int
do_rand(unsigned long ctx)
{
/*
* Compute x = (7^5 * x) mod (2^31 - 1)
* wihout overflowing 31 bits:
* (2^31 - 1) = 127773 * (7^5) + 2836
* From "Random number generators: good ones are hard to find",
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
* October 1988, p. 1195.
*/
long hi, lo, x;
/* Can't be initialized with 0, so use another value. */
if (ctx == 0)
ctx = 123459876;
hi = ctx / 127773;
lo = ctx % 127773;
x = 16807 * lo - 2836 * hi;
if (x < 0)
x += 0x7fffffff;
return x;
}
long commonContext;
char fill[54];
struct Tls
{
long individualContext;
long myRandomContext;
int outCounter;
char fill[54];
} tls[30];
HANDLE manual = CreateEvent(0, TRUE, FALSE, 0);
int moda;
DWORD WINAPI threadFunc1(LPVOID lpThreadParameter)
{
Tls& me = tls[(int) lpThreadParameter];
me.myRandomContext = (int)&lpThreadParameter;
long& C = commonContext;
const int myModa = moda;
WaitForSingleObject(manual, INFINITE);
for(int i=0;i