I've got the following code:
class Foo {
private:
std::thread thread;
void run();
std::atomic_flag running;
std::thread::native_handle_type native;
public:
Foo(const std::string& filename);
virtual ~Foo();
virtual void doOnChange();
void start();
void quit();
};
#include "Foo.h"
#include <functional>
#include <iostream>
Foo::Foo(const std::string& filename) :
thread(), running(ATOMIC_FLAG_INIT) {
file = filename;
native = 0;
}
Foo::~Foo() {
quit();
}
void Foo::start() {
running.test_and_set();
try {
thread = std::thread(&Foo::run, this);
} catch (...) {
running.clear();
throw;
}
native = thread.native_handle();
}
void Foo::quit() {
running.clear();
pthread_cancel(native);
pthread_join(native, nullptr);
//c++11-style not working here
/*if (thread.joinable()) {
thread.join();
thread.detach();
}*/
}
void Foo::run() {
while (running.test_and_set()) {
numRead = read(fd, buf, BUF_LEN);
.....bla bla bla.......
}
}
I'm trying to quit from this thread in my program cleanup code. Using pthread works but I'm wondering if I can do something better with c++11 only (no native handle). It seems to me there's no good way to handle all cases using c++11 code. As you can see here the thread is blocked on a read system call. So even if I clear the flag the thread will be still blocked and join call will block forever. So what I really need is an interrupt (in this case pthread_cancel
). But if I call pthread_cancel
I can't call anymore the c++11 join() method because it fails, I can only call pthread_join()
. So it seems the standard has a really big limitation, am I miss anything?
Edit:
After discussion below I changed the Foo class implementation replacing std::atomic_flag with std::atomic and using signal handler. I used the signal handler because in my opinion is better to have a general base class, using the self-pipe trick is too hard in a base class, the logic should be delegated to the child. Final implementation:
#include <thread>
#include <atomic>
class Foo {
private:
std::thread thread;
void mainFoo();
std::atomic<bool> running;
std::string name;
std::thread::native_handle_type native;
static void signalHandler(int signal);
void run();
public:
Thread(const std::string& name);
virtual ~Thread();
void start();
void quit();
void interrupt();
void join();
void detach();
const std::string& getName() const;
bool isRunning() const;
};
Cpp file:
#include <functional>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <Foo.h>
#include <csignal>
#include <iostream>
Foo::Foo(const std::string& name) :
name(name) {
running = false;
native = 0;
this->name.resize(16, '\0');
}
Foo::~Foo() {
}
void Foo::start() {
running = true;
try {
thread = std::thread(&Foo::mainFoo, this);
} catch (...) {
running = false;
throw;
}
native = thread.native_handle();
pthread_setname_np(native, name.c_str());
}
void Foo::quit() {
if (running) {
running = false;
pthread_kill(native, SIGINT);
if (thread.joinable()) {
thread.join();
}
}
}
void Foo::mainFoo() {
//enforce POSIX semantics
siginterrupt(SIGINT, true);
std::signal(SIGINT, signalHandler);
run();
running = false;
}
void Foo::join() {
if (thread.joinable())
thread.join();
}
void Foo::signalHandler(int signal) {
}
void Foo::interrupt() {
pthread_kill(native, SIGINT);
}
void Foo::detach() {
if (thread.joinable())
thread.detach();
}
const std::string& Foo::getName() const {
return name;
}
bool Foo::isRunning() const {
return running;
}
void Foo::run() {
while(isRunning()) {
num = read(.....);
//if read is interrupted loop again, this time
//isRunning() will return false
}
}