LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


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

Rep: Reputation: 0
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, 02:38 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can use select() to make it work like a timer with that resolution.
 
Old 08-04-2004, 05:32 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
or nanosleep()
 
Old 08-05-2004, 02:02 AM   #4
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
nanosleep() is good but if you give too much time to sleep lets say 2 sec, it may not work because of overflow, so you can use usleep() that takes input in microseconds or if you want to use nanosleep, use it in a loop like if you want to sleep for i milisecond, give value for 1/100 milisecond and then put that statement in a loop.

// sleep for 1 milisecond

for (i=0;i<100;i++)
//nanosleep for 1/100 miliseconds
 
Old 08-05-2004, 07:06 AM   #5
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
timeval gtod_now_time;//Do a gettimeofday on this when the prog starts

You may also use such a function to query (roughly) millisecond accurate time
progress in a thread:

unsigned long _my_mmtime(void){
unsigned long result=0;
// long vtime=0;
#ifdef _WIN32
result=timeGetTime();
return result;
#endif
#ifdef LINUX_APP
timeval gtod_now_time;
gettimeofday(&gtod_now_time,0);
result=(gtod_now_time.tv_usec-gtod_start_time.tv_usec)/1000;
result+=(gtod_now_time.tv_sec-gtod_start_time.tv_sec)*1000;
return result;
#endif
return 666;//Not a recognized os
}


Do a nanosleep/usleep in your thread and query the exact time which has evolved after each successing sleep.

BTW If you're thinking of a timing inside a thread which is *guaranteed* to be as
accurate as 1 millisecond, I believe it will not work, as Linux (nor Windows) is
a realtime os, although at least Linux behaves nicely under that respect. But you don't have too much influence on thread scheduling.
 
Old 08-05-2004, 07:18 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Quote:
Originally posted by cppkid
nanosleep() is good but if you give too much time to sleep lets say 2 sec, it may not work because of overflow, so you can use usleep() that takes input in microseconds or if you want to use nanosleep, use it in a loop like if you want to sleep for i milisecond, give value for 1/100 milisecond and then put that statement in a loop.

// sleep for 1 milisecond

for (i=0;i<100;i++)
//nanosleep for 1/100 miliseconds
No, you don't need to do that.
Nanosleep() takes a struct as a parameter which has a seperate filed for seconds, just like pselect().

From "man nanosleep":
Code:
int nanosleep(const struct timespec *req, struct timespec *rem);

struct timespec
{
    time_t  tv_sec;         /* seconds */
    long     tv_nsec;       /* nanoseconds */
};
So, just fill in the seconds and nanoseconds in the right member of the struct, and you won't have that problem.

Also, you cannot use usleep() together with sleep() or alarm(). And nanosleep can also be used to continue the "sleep" when interrupted. All-in-all, nanosleep() is the best to use. (or select(), or pselect() for that matter, though they're more complex to use).

Last edited by Hko; 08-05-2004 at 07:26 AM.
 
  


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



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
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:28 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration