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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-04-2004, 02:33 PM
|
#1
|
LQ Newbie
Registered: Aug 2004
Posts: 5
Rep:
|
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.
|
|
|
08-04-2004, 02:38 PM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You can use select() to make it work like a timer with that resolution.
|
|
|
08-04-2004, 05:32 PM
|
#3
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
or nanosleep()
|
|
|
08-05-2004, 02:02 AM
|
#4
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
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
|
|
|
08-05-2004, 07:06 AM
|
#5
|
Member
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276
Rep:
|
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(>od_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.
|
|
|
08-05-2004, 07:18 AM
|
#6
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
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.
|
|
|
All times are GMT -5. The time now is 02:44 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|