need to find the best way to exit a thread that is blocked on read
ProgrammingThis 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.
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.
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
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.
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.
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?
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().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.