Results 1 to 5 of 5
I ran into an inconsistency in handling timers (VTALRM) between AMD and intel platforms with threads. My understanding was that the timers are per process. I discovered I must call ...
- 04-20-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 19
VTALRM in multi-threaded programs
I ran into an inconsistency in handling timers (VTALRM) between AMD and intel platforms with threads. My understanding was that the timers are per process. I discovered I must call setitimer in the thread on intel though. AMD allows me to make the setitimer call once in the main thread as expected. The code below demonstrates the issue. I must add the code in the INTEL define for it to work properly on intel cpu's. Am I missing something dumb??? Thankx, Mike
Code:#include <stdio.h> #include <math.h> #include <sys/time.h> #include <pthread.h> #include <signal.h> int die; void *threadWorker(void *clientData) { struct itimerval vtime; int i; double x; printf("Worker main begins; set timer and then work..\n"); /* Why do I need this extra call to setitimer on intel CPU's? */ #ifdef INTEL vtime.it_interval.tv_usec = 500000; vtime.it_interval.tv_sec = 0; vtime.it_value.tv_usec = 500000; vtime.it_value.tv_sec = 0; setitimer(ITIMER_VIRTUAL, &vtime, NULL); #endif printf("Worker main begins ck loop\n"); i = 0; die = 1; while(die) { if( i++ > 100000 ) i = 0; } } void vtProc(int dummy) { printf("VT Signal...\n"); die = 0; } main() { struct itimerval vtime; pthread_attr_t attr; pthread_t thread, dthread; signal(SIGVTALRM, vtProc); vtime.it_interval.tv_usec = 500000; vtime.it_interval.tv_sec = 0; vtime.it_value.tv_usec = 500000; vtime.it_value.tv_sec = 0; setitimer(ITIMER_VIRTUAL, &vtime, NULL); printf("Check if virt timer is one per thread on intel\n"); pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&thread, &attr, threadWorker, (void *) NULL); printf("Main process waiting for thread\n"); pthread_join(thread, NULL); }
- 04-21-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
In my mind, the Intel behavior is correct, otherwise, all threads should be interrupted when the timer expires. How does the system know which thread to interrupt? So, in any case, setitimer() and related calls are obsolete, and should be replaced with the POSIX timer_create(), timer_settime(), timer_gettime(), and timer_delete() calls. FWIW, there is a thread-specific timer that the POSIX timers support.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-22-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 19
thanks
Wow, that was quite a surprise that they obsoleted setitimer in one version... Looks like an ifdef for older ports, since the new method is not available pre 2.6. I don't think either is right or wrong, it surprised me that amd/intel were different. The new method does look much more flexible than the old way.
- 04-22-2011 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Well, I have always had Intel chips, so I have not had the chance to compare how system software behaves on one (Intel) versus the other (AMD). I just sort of assumed they pretty much behaved the same way. Caveat User!
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-29-2011 #5Just Joined!
- Join Date
- Apr 2011
- Posts
- 19
Post-mortem
Well, it turns out I ended up using timer_create for the 2,6 kernel port and called setitimer
for the 2.4 port. 2.4 (& early versions of 2.6 kernel) do not have the timer_create
function implemented, so my customer was segv-ing as they were using an old version
of linux. So if you have to support old kernels a workaround on intel using VTALRM's is to
call setitimer on the new threads.


Reply With Quote