LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-17-2003, 08:22 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
How to make a thread sleep or std::recv timeout?


I've written a program that sends and receives messages through TCP sockets using std::send/recv. On rare occasion, one machine will receive a message, but not send back an answer - this causes the machine that's waiting for the answer to hang at the recv call. My thought was to split the send/recv out into a separate thread, then kill the thread if it has to wait longer than a second for an answer. I can do that by creating the send/recv thread, then making a sleep(1) call, then creating a killing thread that calls pthread_cancel on the first thread.

This works fine and dandy (albeit ineloquent), but it forces a 1 second sleep every time the function gets called. What I'd prefer to have happen is to spawn both threads, make the "kill" thread sleep for a second, and have the program wait for the send/recv thread to finish with a pthread_join function. This way, if the send/recv thread finishes, the "kill" thread tries to kill something that doesn't exist, and everyone is happy, furthermore (if my thinking is correct), the program execution time won't be effected because even though the kill thread is sleeping, or pausing, or pthread_delay_np'ing, no one is waiting on it to finish - so everyone continues on their merry way.

Is this a good way to go about it? If so, how can I make the thread sleep (sleep(1) doesn't seem to work correctly in a thread - it makes all threads sleep(1), and if I try it without a sleep, the kill thread kills the send/recv thread before it's had a good chance to finish)? As an alternative, is there a way to make the recv function time out?
 
Old 10-09-2003, 08:41 PM   #2
oopicmaster
Member
 
Registered: Sep 2003
Posts: 154

Rep: Reputation: 30
You and I have the same problem. The signals sent by all of the timer fucntions sleep() usleep() nanosleep() setitimer() etc, all seen to occur at the Process level.

Have you found the answer?

If this is all that linux provides... Hay Carrumba!
 
Old 10-09-2003, 09:11 PM   #3
oopicmaster
Member
 
Registered: Sep 2003
Posts: 154

Rep: Reputation: 30
Oops... I think nanosleep() works, at least in my test program.
 
Old 10-10-2003, 02:35 AM   #4
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
U can use select() instead of recv() to get call blocked on fixed period of time.

Last edited by dimm_coder; 10-10-2003 at 02:37 AM.
 
  


Reply



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
perl: can I make the system function timeout? apeekaboo Programming 7 07-24-2007 07:52 AM
make harddrive sleep and blank screen? Emanon Linux - Software 3 09-15-2004 04:09 AM
How do I add a timeout for the recv() cmd? dravya Programming 6 07-14-2004 09:28 AM
Can I make my computer sleep? crab_2004 Linux - Software 1 10-09-2003 05:59 PM
cant make instance of std::locale... azazil Programming 0 02-20-2002 05:39 AM

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

All times are GMT -5. The time now is 03:37 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
Open Source Consulting | Domain Registration