Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Windows Threads and their execution


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Nov 5, 2017 7:24 AM Last Post By: Marc Van den Br... Threads: [ Previous | Next ]
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 11, 2017 1:26 PM
Hi,
I have until now been under the assumption that if you yield your threads when you have "looped through" the thread functionality by doing a Sleep(Approx 20ms), windows will give you a "good" amount of CPU time but I now find out that it is the opposite, I get horrible thread performance. If I remove the Sleep(20) I get lightning fast operation.

Admittedly, my thread Sleep(20) habit was established back in BCB4/Win95 times so what is the proper way to get "good" thread performance without hogging the CPU?

Regards
Goran
Martin van der ...

Posts: 57
Registered: 7/14/02
Re: Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 12, 2017 5:38 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
If the thread doesn't need to stay awake for polling or whatever and you can wake the thread every time it needs to do something, then you can use events for that. Look up the CreateEvent, SetEvent, WaitForSingleObject and WaitForMultipleObjects functions.

Using these, you can make a thread sleep at 0% CPU until you give it a signal to do its thing.
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 12, 2017 3:16 PM   in response to: Martin van der ... in response to: Martin van der ...
Hi Martin,
my threads are ususally multitasking, i.e. do a lot of different things outside the GUI so that is not an option. The main reason for my "surprise" is that many years ago (BCB5/Win9x) I noted that if you left just a single TThread running without yielding it hogged the CPU quite badly so I have been going with that assumption until today which in hindsight maybe wasn't that bright considering todays PC capabilities.

I have come to a conclusion that if you have functionality that really need CPU time and responsiveness it is not just as simple as yielding some milliseconds each thread run, you need some more dynamic handling suited for what your thread is actually doing.

That being said, I still would like to know if there are any "best practices" when it comes to thread handling.

Regards
Goran
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 12, 2017 6:22 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

The main reason for my "surprise" is that many years ago (BCB5/Win9x)
I noted that if you left just a single TThread running without yielding it
hogged the CPU quite badly so I have been going with that assumption
until today which in hindsight maybe wasn't that bright considering
todays PC capabilities.

Yes, the OS's thread scheduler has gotten a lot smarter since the Win9x days,
especially as dual-core, quad-core, and hyper-threaded CPUs have gotten more
common.

I have come to a conclusion that if you have functionality that really
need CPU time and responsiveness it is not just as simple as yielding
some milliseconds each thread run, you need some more dynamic
handling suited for what your thread is actually doing.

Or, you can lock down the thread to a specific core and let it max out, while
leaving other cores to service the rest of the system.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2017 9:08 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Or, you can lock down the thread to a specific core and let it max out, while
leaving other cores to service the rest of the system.

Yes, I have some studying to do!
Marc Van den Br...

Posts: 10
Registered: 6/16/07
Re: Windows Threads and their execution  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 5, 2017 7:24 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:
Hi,
I have until now been under the assumption that if you yield your threads when you have "looped through" the thread functionality by doing a Sleep(Approx 20ms), windows will give you a "good" amount of CPU time but I now find out that it is the opposite, I get horrible thread performance. If I remove the Sleep(20) I get lightning fast operation.

Admittedly, my thread Sleep(20) habit was established back in BCB4/Win95 times so what is the proper way to get "good" thread performance without hogging the CPU?

Regards
Goran

WaitForSingleObject(Route_bestel,INFINITE); this is Thread direct on win32 api. a sleep just stalls the thread as it does what ya ask sleep :) a CPU doesn't sleep. :)
#include <windows.h>

__fastcall BESTEL_THREAD::BESTEL_THREAD(int x):TThread(true)
{
FreeOnTerminate=TRUE;
Priority=3;
Resume();
};

void __fastcall BESTEL_THREAD::Execute(void)
{
char *buf=(char *)malloc(10000);
int waiting=0;
WaitForSingleObject(Route_bestel,INFINITE);

do
{
EnterCriticalSection(&Bestel_critical);
if(Form1->ListBox4->Items->Count==0)
{
WaitForSingleObject(Route_bestel,INFINITE);
waiting=1;
}

if(waiting==1)
{
waiting=0;
LeaveCriticalSection(&Bestel_critical);
continue;
};

Form1->ListBox4->ItemIndex=0;
buf=Form1->ListBox4->Items->GetText();
Form1->ListBox4->Items->Delete(0);
if(buf[0]!=0)
{
if(buf[0]=='P' && buf[1]=='T' && buf[2]=='Y')
RouteSecondServer(buf);
else
RouteBestelling(buf);
}
}
while(!Terminated);
free(buf);
};
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02