943,901 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2658
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 6th, 2009
0

Re: How to get a thread's execution time

When you spawn the thread you pass a void user value!
Why not pass a boolean pointer! Loop while it is set and when false fall out of the thread loop. Don't forget your thread exit function for proper cleanup.
C++ Syntax (Toggle Plain Text)
  1. void MyWorker( void *foo )
  2. {
  3. bool *pbSignal = (bool *)foo;
  4.  
  5. while (foo)
  6. {
  7.  
  8.  
  9. }
  10.  
  11. }

Or pass in a local index for that worker thread.

C++ Syntax (Toggle Plain Text)
  1. void MyWorker( void *foo )
  2. {
  3. uint idx = (uint)foo;
  4.  
  5. while ( gbAppActive == true )
  6. {
  7.  
  8.  
  9. }
  10.  
  11. gThreadActive[ idx ] = false; // Tell root that this thread is shut down
  12.  
  13. }


In application root cleanup.
Loop for up until all gThreadActive[] become clear or the clock runs out, whichever comes first.

If you use semaphores, use a single gThreadActive parameter and merely clear the bit!
C++ Syntax (Toggle Plain Text)
  1. LOCK();
  2. gThreadActive ^= 1 << idx;
  3. UNLOCK();
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 7th, 2009
0

Re: How to get a thread's execution time

Quote ...
When you spawn the thread you pass a void user value!
Why not pass a boolean pointer!
When you spawn the thread you pass a void pointer, which means you pass a pointer to something (or nothing). I am already using this to pass some arguments to the threads.
Wildgoose, I don't get what this last post has to offer to the discussion.
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jul 7th, 2009
0

Re: How to get a thread's execution time

I don't understand what's so difficult about thread timing. If you want the thread to quit after a specified amount of time, then just compare current time with original thread entry time and exit when that difference is greater than some pre-determined amount of time.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 7th, 2009
0

Re: How to get a thread's execution time

I don't understand what's so difficult about thread timing. If you want the thread to quit after a specified amount of time, then just compare current time with original thread entry time and exit when that difference is greater than some pre-determined amount of time.
I don't understand what's so difficult about multi-threading for you. Threads yield execution to other threads and gain it back and yield again... etc. When they yeld they are not running anymore so any "raw" difference between starting time and current time is *not* what you want. Stop insisting please... I would flag your last post as a bad post...

NOTE: as mentioned in a previous post this issue can be solved in UNIX by using clock_gettime with the flag CLOCK_THREAD_CPUTIME_ID. I don't even need to save the start point because each thread's clock is set to 0 when it starts. I made the tests, 10 threads executing for 50 seconds in a core-duo processor, giving total time ~= 250 seconds, perfect for me!
I am still looking for a solution which can be applied cross-platform, or at least a similar solution for windows.
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jul 7th, 2009
0

Re: How to get a thread's execution time

GetThreadTimes()

I have not used it and don't know if it will meet your needs. Did you overlook this thread?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 7th, 2009
0

Re: How to get a thread's execution time

Thanks, this looks like what I want for windows. As soon as I have tried it, I will post comments on it.
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jan 10th, 2011
1

Function to get thread CPU usage

BOOL WINAPI GetThreadTimes(
__in HANDLE hThread,
__out LPFILETIME lpCreationTime,
__out LPFILETIME lpExitTime,
__out LPFILETIME lpKernelTime,
__out LPFILETIME lpUserTime
);

http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

It is only updated on context switch so a Sleep(1) is needed before calling it. THERE ARE OTHER CAVEATS so that it may not be suitable for measuring very small increments.

IMPORTANT CAVEATS here!

http://blog.kalmbachnet.de/?postid=28

Vista provides

BOOL WINAPI QueryThreadCycleTime(
__in HANDLE ThreadHandle,
__out PULONG64 CycleTime
);

http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

I don't know what caveats apply to this call.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JohnDiego is offline Offline
2 posts
since Jan 2011
Jan 11th, 2011
0
Re: How to get a thread's execution time
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void thread_function( void* arg )
  6. {
  7. time_t start, finish;
  8. long loop;
  9. double result, elapsed_time;
  10.  
  11. time( &start );
  12.  
  13. // Your thread code
  14.  
  15. time( &finish );
  16.  
  17. elapsed_time = difftime( finish, start );
  18. printf( "\nThread takes %6.0f seconds.\n", elapsed_time );
  19. }

The code counts for the execution time of one thread, it doesn't matter if the thread is active or not.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Cosmin871 is offline Offline
23 posts
since Sep 2009
Jan 31st, 2011
0

One more caveat

I did some testing on this. As suggested in other posts, these aren't all that accurate, they tend to run low due to the thread accounting, context switches, etc. They are much more useful to locate long-running piggy threads.

QueryPerformanceCounter is the way to go, or RDTSC, both as noted here. QPC uses RDTSC, with some code around it to make sure the value is reasonable.

http://en.wikipedia.org/wiki/Time_Stamp_Counter

The Performance counter is very high res, you don't need to run something 10,000 times. Unless it is really short you can measure it directly. Just watch for context switches in the middle of the measurement. Those will be obvious because the elapsed time will jump to milliseconds and beyond. I routinely measure code timing.

Click to Expand / Collapse  Quote originally posted by JohnDiego ...
BOOL WINAPI GetThreadTimes(
__in HANDLE hThread,
__out LPFILETIME lpCreationTime,
__out LPFILETIME lpExitTime,
__out LPFILETIME lpKernelTime,
__out LPFILETIME lpUserTime
);

http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

It is only updated on context switch so a Sleep(1) is needed before calling it. THERE ARE OTHER CAVEATS so that it may not be suitable for measuring very small increments.

IMPORTANT CAVEATS here!

http://blog.kalmbachnet.de/?postid=28

Vista provides

BOOL WINAPI QueryThreadCycleTime(
__in HANDLE ThreadHandle,
__out PULONG64 CycleTime
);

http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

I don't know what caveats apply to this call.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JohnDiego is offline Offline
2 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Drawing large bitmap/background
Next Thread in C++ Forum Timeline: Buying machine





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC