Article View: borland.public.cppbuilder.nativeapi
Article #17531Re: Tweaking threads
From: Micha Nelissen
Date: Thu, 13 Mar 2003 21:04
Date: Thu, 13 Mar 2003 21:04
105 lines
3898 bytes
3898 bytes
"Dennis Jones" wrote:
> performance hit because the OS will efficiently avoid switching into the
> context of a thread that is waiting for an OS event. On the other hand,
if
> every one of those threads is busy doing something (or worse, is in a
tight
> loop watching the state of a flag), then the amount of context switching
> would probably be significantly measureable, and seriously affecting
> performance.
I have tried to write a program to measure this, but it is quite tricky. I
will post the code at the bottom. Use commandline bcc32 to compile it.
Warning: very not userfriendly, use at own risk, etc.
Program takes 2 parameters:
threads #numthreads #delaycounter
#numthreads = the number of threads the program creates before beginning
#delaycounter = a number that is used to slow things down. 1000 is approx 1
sec on my computer, 750MHz.
I don't know if a conclusion can be drawn, but 2024 is max threads for my
machine and the program runs about 40 percent slower. 1 thread takes 1040
msec, 2024 threads take 1412 msec to complete.
I have tried to measure only the difference context switching, but I don't
know how succesful I was.
HTH,
Micha.
---code threads.cpp---
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
int num_iterations;
bool active;
unsigned long __stdcall thread_func(void* data)
{
unsigned __int64 i;
while (!active) {}
for (i=0; i < (*reinterpret_cast<unsigned __int64*>(data)); i++);
return NULL;
}
int main(int argc, char** argv)
{
HANDLE* tid;
int i, j, numthreads;
unsigned __int64 count;
DWORD start;
if (argc < 3)
return 1;
// init threads
printf("Creating threads...");
active = false;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
numthreads = atoi(argv[1]);
count = atoi(argv[2]);
count *= 100000/numthreads;
tid = new HANDLE[numthreads];
printf("%d, count to %Lu\n", numthreads, count);
for (i=0; i<numthreads; i++)
{
tid[i] = CreateThread(NULL, 0, thread_func, &count, CREATE_SUSPENDED,
NULL);
if (tid[i] == NULL)
{
printf ("error create thread\n");
}
SetThreadPriority(tid[i], THREAD_PRIORITY_IDLE);
}
// start!
printf("Resuming...\n", MAXIMUM_WAIT_OBJECTS);
for (i=0; i<numthreads; i++)
ResumeThread(tid[i]);
active = true;
// wait for threads to terminate
start = GetTickCount();
for (i=0; i<numthreads; i+=j)
{
j = numthreads-i;
if (j > MAXIMUM_WAIT_OBJECTS)
j = MAXIMUM_WAIT_OBJECTS;
if (WaitForMultipleObjects(j, tid+i, TRUE, INFINITE) == WAIT_FAILED)
printf("wait failed: error=%u\n", GetLastError());
}
printf("Time=%dms\n", GetTickCount()-start);
delete[] tid;
}
Technical Details
Message-ID:
<3e70f277$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3e6deb3f@newsgroups.borland.com> <3e6e2fb9@newsgroups.borland.com> <3e6f8ce5$1@newsgroups.borland.com> <hq9v6vkjgup69jju5tdj5ltp9s72su9vlq@4ax.com> <3e6fb851$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,47731b28ebfc1a3 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-03-13 13:08:47 PST From: "Micha Nelissen" <M.Nelissen@an.ti.sp.am.student.tue.nl> Newsgroups: borland.public.cppbuilder.nativeapi References: <3e6deb3f@newsgroups.borland.com> <3e6e2fb9@newsgroups.borland.com> <3e6f8ce5$1@newsgroups.borland.com> <hq9v6vkjgup69jju5tdj5ltp9s72su9vlq@4ax.com> <3e6fb851$1@newsgroups.borland.com> Subject: Re: Tweaking threads Date: Thu, 13 Mar 2003 22:04:56 +0100 Lines: 107 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 62.21.237.84 Message-ID: <3e70f277$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1047589495 62.21.237.84 (13 Mar 2003 13:04:55 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail