LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-10-2009, 07:47 AM   #1
litleguy
LQ Newbie
 
Registered: Feb 2009
Location: a room with no windows
Posts: 3

Rep: Reputation: 0
need to find the best way to exit a thread that is blocked on read


Hello,

I have a driver with two threads. Write is easy, as I use signals to wake it up for write events and write event of 0 is an exit. The read is giving me problems, as I use block on read read(). I don't know the best way to stop the thread. I use fcntl to setup my stream as blocked on read. I use pthread_kill(read_thread, 0); to stop the read thread right now.
Please advice
 
Old 02-10-2009, 07:56 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Do you need to block on a read()? You could try to set up the stream as non-blocking.

Another alternative, is to use select() to determine whether you should read in the first place. A timeout value can be used with select() so that if no activity is detected in a certain delta of time, your thread gets control back, to perhaps look at a semaphore or other flag to determine if it should exit.

For example:

Code:
...

fd_set savefds;
FD_ZERO(&savefds);
FD_SET(fd, &savefds);

while (run)
{
  fd_set readfds = savefds;
  struct timeval timeout = {1,0};

  int sel = select(fd + 1, &readfds, 0, 0, &timeout);
  if (sel > 0)
  {
    // read
  }
}

...
The 'run' flag can be set by your main thread to 'true' under nominal circumstances, or 'false' otherwise.
 
Old 02-11-2009, 03:27 AM   #3
litleguy
LQ Newbie
 
Registered: Feb 2009
Location: a room with no windows
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the advice. I have not used select yet. I will build a version and run a test.

I could not find a way to stop the thread from reading, when it was blocked on read. I don't know, if closing the fd would pull the rag on the blocked on read thread.

I ended up rewriting the interface to use usleep and read every so often. I will attempt the select method as well.

Thanks again for the advice.
 
Old 02-13-2009, 04:55 AM   #4
litleguy
LQ Newbie
 
Registered: Feb 2009
Location: a room with no windows
Posts: 3

Original Poster
Rep: Reputation: 0
The select version worked perfect. I wanted to ask you how would you use the select approach, if you had multiple fd's in the set? Would you read each, or is there a better approach such as setting an individual fd set for each select call? Would you call select individually per stream or combine streams in a single call?

Thanks
 
Old 02-13-2009, 04:59 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You can add more file descriptors to your fd_set using FD_SET(). When select() reports that there is activity, you can use FD_ISSET() to determine which file descriptor requires attention.

There's a basic example given in the man-page for select().
Code:
man 2 select
 
  


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
Exit from blocked pselect() noisebleed Programming 5 12-16-2008 07:10 PM
where can i find details of blocked emails karen.pertierra Linux - Security 1 02-22-2007 02:46 PM
"NIM thread blocked" & "Deadman Switch (DMS) close to trigger" errors sosborne Linux - Networking 1 06-28-2006 02:07 PM
read API is blocked lucky6969b Programming 2 12-08-2005 07:27 PM
Find Blocked Ports on Network artielnx Linux - Newbie 2 09-30-2004 03:01 PM

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

All times are GMT -5. The time now is 01:21 AM.

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