Using kqueue with 2 threads
Ben Short
ben at benshort.co.uk
Tue Apr 10 14:33:00 UTC 2012
Hi,
I'm trying to use a kqueue to listen for VNODE events on a worker thread. I
want new events to be registered with the kqueue by a separate thread.
I have been referring to this example [URL="
http://doc.geoffgarside.co.uk/kqueue/file.html"]
http://doc.geoffgarside.co.uk/kqueue/file.html[/URL]
Here are the relevant parts of code as it is at the moment.
In my worker thread:
[CODE]std::cerr << "Started worker" << std::endl;
struct kevent ke;
int i;
while ( !mStopRequested ) {
memset(&ke, 0x00, sizeof(kevent));
i = kevent(kq, NULL, 0, &ke, 1, NULL);
if ( i == -1 ) {
std::cerr << "kqueue produced error: " << strerror(i) <<
std::endl;
continue; // todo is this the best thing todo?
}
std::cerr << "Beep: " << i << std::endl;
}
std::cerr << "Shutting down worker" << std::endl;[/CODE]
Other thread
[CODE]int fd = open(fileName.c_str(), O_RDONLY);
if ( fd == -1 ) {
std::cerr << "Failed to open file: " << fileName << " Error: " <<
strerror(errno) << std::endl;
// todo throw exception
}
struct kevent ke;
EV_SET(&ke, fd, EVFILT_VNODE, EV_ADD, NOTE_DELETE | NOTE_RENAME |
NOTE_EXTEND, 0, NULL);
if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1) {
std::cerr << "kevent produced error: " << strerror(errno) <<
std::endl;
}[/CODE]
When I run my code the kevent call in the worker code doesn't block at all.
Any ideas what I'm missing? or if what I want to do is even possible?
Ben
More information about the freebsd-questions
mailing list