LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 08-04-2004, 02:30 PM   #1
cardias
LQ Newbie
 
Registered: Aug 2004
Posts: 5

Rep: Reputation: 0
Problems creating a milisecond timeout timer for threads


Hi!
I have a few threads (pthread) that are created at the same time and run simultaneously. Each of them should last for a given amount time and then finish. For example, if thread#1 should last for 5 seconds and thread#2 for 3 seconds, thread#2 should finish 2s before thread#1, but the total amount of time (for both) has to be around 5s.
If I use the time() function (time.h), it works but with a resolution of 1s. In order to have a resolution of 1 millisecond, I was using the clock() function but it works completely wrong depending of the number of threads.
I would appreciate ANY comments about a accurate millisecond timer to create a timeout (or virtual interruption) for threads.
Thanks.
 
Old 08-04-2004, 03:10 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 47
hi,

Code:
/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
* Copyright (C) 2001 by New Riders Publishing                          *
* See COPYRIGHT for license information.                               *
***********************************************************************/

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void timer_handler (int signum)
{
  static int count = 0;
  printf ("timer expired %d times\n", ++count);
}

int main ()
{
  struct sigaction sa;
  struct itimerval timer;

  /* Install timer_handler as the signal handler for SIGVTALRM.  */
  memset (&sa, 0, sizeof (sa));
  sa.sa_handler = &timer_handler;
  sigaction (SIGVTALRM, &sa, NULL);

  /* Configure the timer to expire after 250 msec...  */
  timer.it_value.tv_sec = 0;
  timer.it_value.tv_usec = 250000;
  /* ... and every 250 msec after that.  */
  timer.it_interval.tv_sec = 0;
  timer.it_interval.tv_usec = 250000;
  /* Start a virtual timer.  It counts down whenever this process is
     executing.  */
  setitimer (ITIMER_VIRTUAL, &timer, NULL);

  /* Do busy work.  */
  while (1);
}
hope that helps
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Threads lucky6969b Linux - Software 2 12-02-2005 01:44 AM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Creating a countdown timer window in Fedora Core statman Linux - Software 0 06-18-2005 04:42 PM
Creating Linux Threads in C++ George_gk Programming 2 01-27-2005 03:29 AM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 07:18 AM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration