I'm writing a program that replys on SIGTERM/SIGINT to terminate. This is a multi-thread program. Basically I have a dedicated thread that is waiting for SIGTERM and SIGINT by calling sigwait and I will notify the main thread upon receving the signals in this thread.
I sent the SIGTERM signal to this program by executing command "killall nameofprocess". However, I found that it will work as expcted only in 10%. Most of times I have to run the "killall" command twice to terminate this program. I'm wondering why the sigwait doesn't capture the SIGTERM the first time. (Same case for SIGINT when I run kill -2)
Is it possible that the signal is received by some other threads?
static std::atomic<bool> shutdown_requested(false);
static std::mutex cv_mutex;
static std::condition_variable cv;
int main()
{
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGTERM);
pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
....do something including creating some threads.....
//a dedicated thread that waits for signals
auto signal_handler = [&sigset]() {
int signum = 0;
// wait until a signal is delivered:
sigwait(&sigset, &signum);
shutdown_requested.store(true);
// notify all waiting workers to check their predicate:
cv.notify_all();
return signum;
};
auto signal_handler_async = std::async(std::launch::async, signal_handler);
//wait for cv
while(shutdown_requested.load() == false)
{
std::unique_lock lock(cv_mutex);
cv.wait(lock, [](){return shutdown_requested.load();});
}
..........clean up and exit..............
return 0;
}
Related
I have Windows multi-threaded code that I'm trying to make portable by using the C++11 thread and synchronization classes. How should the main thread wait for a worker thread with a timeout? I tried using a condition variable, but it's possible for the main thread to miss the worker's notification by waiting too late. In Windows I would use an "event object" as shown below:
#include "Windows.h"
HANDLE hWorkerDoneEvent; // synchronization event
DWORD WINAPI ThreadFunc(LPVOID lpParameter)
{
printf("worker: launched; working\n");
Sleep(1000); // pretend to work
printf("worker: signal done\n");
SetEvent(hWorkerDoneEvent); // signal main thread that we're done working
printf("worker: exit\n");
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
hWorkerDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // create auto-reset event
if (hWorkerDoneEvent == NULL) {
printf("main: error creating event\n");
return 0;
}
printf("main: launch worker\n");
if (CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL) == NULL) { // create worker thread
printf("main: error launching thread\n");
return 0;
}
printf("main: delay a bit\n");
Sleep(2000); // demonstrate that it's impossible to miss the worker's signal by waiting too late
printf("main: wait for worker's done signal\n");
if (WaitForSingleObject(hWorkerDoneEvent, 5000) == WAIT_OBJECT_0) { // wait for worker's signal
printf("main: worker finished normally\n");
} else {
printf("main: worker timeout or error\n");
}
return 0;
}
The Windows program's output is as expected:
main: launch worker
main: delay a bit
worker: launched; working
worker: signal done
worker: exit
main: wait for worker's done signal
main: worker finished normally
My attempt to replicate the above using C++11:
#include "thread"
#include "chrono"
#include "condition_variable"
using namespace std;
condition_variable cvWorkerDone;
mutex mtxWorkerDone;
void ThreadFunc()
{
printf("worker: launched; working\n");
this_thread::sleep_for(std::chrono::milliseconds(1000)); // pretend to work
printf("worker: signal done\n");
cvWorkerDone.notify_all(); // signal main thread that we're done working
printf("worker: exit\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("main: launch worker\n");
thread worker(ThreadFunc);
printf("main: delay a bit\n");
this_thread::sleep_for(std::chrono::milliseconds(2000)); // causes timeout because we waited too late and missed worker's notification
printf("main: wait for worker's done signal\n");
unique_lock<mutex> lk(mtxWorkerDone);
if (cvWorkerDone.wait_for(lk, std::chrono::milliseconds(5000))) {
printf("main: worker finished normally\n");
} else {
printf("main: worker timeout or error\n");
}
worker.join();
return 0;
}
The C+11 program's output; note that the main thread times out even though the worker did its work and signaled:
main: launch worker
worker: launched; working
main: delay a bit
worker: signal done
worker: exit
main: wait for worker's done signal
main: worker timeout or error
In the C++11 code, the main thread incorrectly times out because notification isn't sticky, in other words notify_all only works if a thread is already waiting. I understand that std::binary_semaphore would solve my problem, but that's only available in C++20 which I don't currently have access to. And yes, I could put a short wait at the start of the worker to give the main thread time to get to its wait, but that's gross and race-prone and inefficient.
I also tried having the worker lock a timed_mutex while the main thread does a try_lock_for on that same mutex, as shown below, but this is equally wrong (it deadlocks):
#include "thread"
#include "chrono"
#include "mutex"
using namespace std;
timed_mutex mtxWorkerDone;
void ThreadFunc()
{
printf("worker: launched\n");
printf("worker: delay a bit\n");
this_thread::sleep_for(std::chrono::milliseconds(500)); // fools main thread into locking mutex before we do, causing deadlock
printf("worker: locking mutex\n");
unique_lock<timed_mutex> lk(mtxWorkerDone);
printf("worker: mutex locked; working\n");
this_thread::sleep_for(std::chrono::milliseconds(1000)); // pretend to work
printf("worker: exit (releasing lock)\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("main: launch worker\n");
thread worker(ThreadFunc);
printf("main: wait for worker's done signal\n");
unique_lock<timed_mutex> lk(mtxWorkerDone, defer_lock);
if (lk.try_lock_for(std::chrono::milliseconds(5000))) {
printf("main: worker finished normally\n");
} else {
printf("main: worker timeout or error\n");
}
worker.join();
return 0;
}
The output; the main thread thinks the worker finished normally, but the worker is actually blocked at the unique_lock and never gets to its work.
main: launch worker
worker: launched
worker: delay a bit
main: wait for worker's done signal
main: worker finished normally
worker: locking mutex
To reiterate, I'm looking for a stdlib solution in C++11 that doesn't depend on sleeping to avoid races. I have often ported firmware from Windows to embedded platforms, and often faced similar issues. Windows is the Cadillac of synchronization APIs, with a tool for every occasion. Even twenty years later I still see posts asking how to replace WaitForMultipleObjects. Anyway I expected this to be a PITA and I'm not disappointed. :)
I very well may be missing something, and had to adapt your code a bit as I am doing this quickly on lunch, but I believe this is basically what you are looking for and as far as I can tell is the correct way to go about this. Note the addition of a lambda to the wait() call and the lock_guard in ThreadFunc():
#include "thread"
#include "chrono"
#include "condition_variable"
using namespace std;
condition_variable cvWorkerDone;
mutex mtxWorkerDone;
bool finished = false;
void ThreadFunc()
{
printf("worker: launched; working\n");
this_thread::sleep_for(std::chrono::milliseconds(1000)); // pretend to work
printf("worker: signal done\n");
std::lock_guard<std::mutex> lk(mtxWorkerDone);
finished = true;
cvWorkerDone.notify_all(); // signal main thread that we're done working
printf("worker: exit\n");
}
int main(int argc, char* argv[])
{
printf("main: launch worker\n");
thread worker(ThreadFunc);
printf("main: delay a bit\n");
this_thread::sleep_for(std::chrono::milliseconds(2000)); // causes timeout because we waited too late and missed worker's notification
printf("main: wait for worker's done signal\n");
{
unique_lock<mutex> lk(mtxWorkerDone);
if (cvWorkerDone.wait_for(lk, std::chrono::milliseconds(5000), []{return finished;})) {
printf("main: worker finished normally\n");
} else {
printf("main: worker timeout or error\n");
}
}
worker.join();
return 0;
}
I know that this is next question about connect signal/slot mechanism between threads. I wrote working Worker application.
Main problem
I have worker class that has been moved to another thread. Second part of application is GUI interface with button. When I click button thread starts:
void MainWindow::startStopThreadA()
{
...
else
{
threadA = new QThread;
workerA = new WorkerObject;
workerA->setMessage("Thread A running");
workerA->moveToThread(threadA);
connect(threadA, SIGNAL(started()), workerA, SLOT(process()), Qt::QueuedConnection);
connect(workerA, SIGNAL(finished()), threadA, SLOT(quit()));
connect(workerA, SIGNAL(finished()), workerA, SLOT(deleteLater()));
connect(threadA, SIGNAL(finished()), threadA, SLOT(deleteLater()));
//Connect signal from thread with slot from MainWindow
connect(workerA, SIGNAL(printMessage(QString)), this, SLOT(printMessage(QString)), Qt::QueuedConnection);
threadA->start();
ui->threadAButton->setText("Stop A");
}
}
When thread starts then emits signal:
void WorkerObject::process(void)
{
//Infinity thread loop
forever
{
//Exit loop part
mutex.lock();
if(m_stop)
{
m_stop = false;
mutex.unlock();
break;
}
mutex.unlock();
//Hold/unhold loop part
mutex.lock();
if(!m_hold)
{
mutex.unlock();
//Here signal is emited
emit printMessage(messageStr); //That not works
//qDebug() << "Thread A test message."; //That works properly
}
mutex.unlock();
}
emit finished();
}
In main GUI thread I have timer for show that GUI thread works. So qDebug() works fine and prints messages from my thread. Also timer from GUI thread works fine and prints message inside textEdit GUI field.
Now when printMessage signal is emited, GUI thread executes slot method:
void MainWindow::printMessage(QString str)
{
ui->textEdit->append(str);
}
And this is most important part of my problem:
When signal printMessage from workerA object is connected with GUI slot printMessage with Qt::QueuedConnection my application hangs up. There is no possible to click something button or even exit app.
When signal/slot are connected with Qt::BlockingQueuedConnection everything works fine. Messages are emitted and received between threads and also GUI timer works fine.
So my question is why connection Qt::QueuedConnection causes that app freezes ?
I figured out this problem with #m7913d help.
You can try to perform a QThread::sleep inside your forever loop (f.e.
1 second) to check if it solves your problem.
So worker's thread emitted signal too frequently. Adding QThread::msleep(5) after emitted signal really helped. There ` also need to be included.
I'm making a timer program the counts the time passed since the program started. In the background I'm also checking for keyboard input (enter/return to exit, click on the window); this is done in a separate thread I've run as detached.
It seems the second thread cannot receive the input from the main thread. When I use the keyboard or mouse, nothing happens. Also, nothing appears on the screen, just white.
std::mutex g_mutex;
std::condition_variable cv;
// check for input from the user using the window object
// sets stopProgram to true if the user wishes to exit
void poll(sf::RenderWindow& window, bool& stopProgram) {
std::unique_lock<std::mutex> lk(g_mutex);
// wait for main thread to open window
cv.wait(lk, [&] { return !stopProgram && window.isOpen(); });
sf::Event event;
while (true) {
if (window.pollEvent(event)) {
// if user wants to exit program
if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed &&
(event.key.code == sf::Keyboard::Return || event.key.code == sf::Keyboard::Escape))) {
window.close();
// main thread will explicitly exit the main loop
stopProgram = true;
break;
}
}
}
}
int main()
{
int hour = 0, minute = 0, second = 0;
auto text = textObject();
bool stopProgram = false;
// run a background thread that checks for input while the main program runs
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
std::once_flag flag;
std::lock_guard<std::mutex> lk(g_mutex);
while (window.isOpen()) {
// notify once window opens
std::call_once(flag, [&] { cv.notify_one(); });
// set timestamp
text->setString(makeTimeStamp(hour, minute, second));
// if the background thread set stopProgram, end the program
if (stopProgram) break;
window.clear(sf::Color::White);
window.draw(*text);
window.display();
// update time
second = (second + 1) % MAX_SEC;
if (second == 0) minute = (minute + 1) % MAX_MIN;
if (second == 0 && minute == 0) hour = (hour + 1) % MAX_HOUR;
// sleep one second
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
Is my use of multithreading correct? If so, can only the main thread receive input and that's why it's not working?
Update: If I get rid of the while (true) and use while (window.pollEvent(event)) and move the lock_guard to just before if (stopProgram) then the text (timestamp) appears on the screen, but I still cannot process input.
main thread launches poll thread.
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
main thread acquires g_mutex and never ever releases it.
std::lock_guard<std::mutex> lk(g_mutex);
poll thread waits for g_mutex to be released:
std::unique_lock<std::mutex> lk(g_mutex);
but the main thread never releases it, so the poll thread never does anything.
To fix it. Change the beginning of the main() function:
int main()
{
int hour = 0, minute = 0, second = 0;
auto text = textObject();
volatile bool stopProgram = false;
// run a background thread that checks for input while the main program runs
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
while (!window.isOpen()) { /* busy loop */ }
{
std::lock_guard<std::mutex> lk(g_mutex);
cv.notify_all();
}
while (window.isOpen()) {
...
This SFML API makes things more difficult than other windowing frameworks that I've used. It would be sooo useful if there were a thread-safe window.pushCustomEvent() function.
I want to make a condition in code to wait until all threads are finished in the main thread final slot, following is the test code ..
testClass::testClass()
{
m_count = 0;
m_flag = false;
for( int i = 0; i < 3; i++)
{
QThread *thread = new QThread();
WorkerThread *worker = new WorkerThread();
connect(thread, SIGNAL(started()), worker, SLOT(startThread()));
connect(worker, SIGNAL(workerFinished()), this, SLOT(threadFinished()));
connect(worker, SIGNAL(workerFinished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
m_workerList.append(worker);
}
}
void testClass::threadFinished()
{
// wait untill all thread are finished, how to achive this ?
if(!m_flag)
{
// print << m_flag;
m_flag = true;
}
}
void WorkerThread::startThread()
{
emit workerFinished();
}
here testClass is in main thread and I want the application to wait in threadFinished slot until all threads I started in testClass constructor to finish, could anyone suggest best way to do this ?
I am using Qt 5.4.0 in Windows 7.
If using boost in addition to Qt is an option, you can use a thread group and call join_all, which waits until all threads in the group are done.
Also, QThreadPool provides the waitForDone() function ("Waits for each thread to exit and removes all threads from the thread pool."), however you might have to restructure your worker objects slightly to comply to the QRunnable interface.
A more basic solution would be to set a QVector or QMap member variable with one entry for each thread and set them to true when the respective thread finishes. Continue program execution in your slot only when all threads have reached "finished = true" state in your member variable.
My program is setup as follows:
There is a thread-safe queue class, one thread pushing data onto it while sitting in an infinite loop and a second thread popping data off of it while sitting in an infinite loop. I am trying to think of a way to use windows events, or some other mechanism to make the thread_1 (below), wait in the infinite while loop and only iterate when the queue depth is greater than or equal to 1.
class thread-safe_Queue
{
public:
push();
pop();
};
DWORD thread_1()
{
while(1)
{
// wait for thread-safe queue to have data on it
// pop data off
// process data
}
}
DWORD thread_2()
{
while(1)
{
// when data becomes available, push data onto thread-safe queue
}
}
I think this might do the trick. Derive class Event and overload the Process() function.
#include <process.h> // Along with all the normal windows includes
//*********************************************
using namespace os;
Mutex globalQueueMutex;
class QueueReader : public Event
{
public:
virtual void Process()
{
// Lock the queue
Locker l(globalQueueMutex);
// pop data off
// process data
return; // queue will automatically unlock
}
};
QueueReader myQueueReader;
//*********************************************
// The queue writer would have functions like :
void StartQueueReader()
{
Thread(QueueReader::StartEventHandler, &myQueueReader);
}
void WriteToQueue()
{
Locker l(globalQueueMutex);
// write to the queue
myQueueReader.SignalProcess(); // tell reader to wake up
}
// When want to shutdown
void Shutdown()
{
myQueueReader.SignalShutdown();
}
Here are the classes that perform the magic.
namespace os {
// **********************************************************************
/// Windows implementation to spawn a thread.
static uintptr_t Thread (void (*StartAddress)(void *), void *ArgList)
{
return _beginthread(StartAddress, 0, ArgList);
}
// **********************************************************************
/// Windows implementation of a critical section.
class Mutex
{
public:
// Initialize section on construction
Mutex() { InitializeCriticalSection( &cs_ ); }
// Delete section on destruction
~Mutex() { DeleteCriticalSection( &cs_ ); }
// Lock it
void lock() { EnterCriticalSection( &cs_ ); }
// Unlock it
void unlock() { LeaveCriticalSection( &cs_ ); }
private:
CRITICAL_SECTION cs_;
}; // class Mutex
/// Locks/Unlocks a mutex
class Locker
{
public:
// Lock the mutex on construction
Locker( Mutex& mutex ): mutex_( mutex ) { mutex_.lock(); }
// Unlock on destruction
~Locker() { mutex_.unlock(); }
private:
Mutex& mutex_;
}; // class Locker
// **********************************************************************
// Windows implementation of event handler
#define ProcessEvent hEvents[0]
#define SetTimerEvent hEvents[1]
#define ShutdownEvent hEvents[2]
/// Windows implementation of events
class Event
{
/// Flag set when shutdown is complete
bool Shutdown;
/// Max time to wait for events
DWORD Timer;
/// The three events - process, reset timer, and shutdown
HANDLE hEvents[3];
public:
/// Timeout is disabled by default and Events assigned
Event( DWORD timer = INFINITE) : Timer(timer)
{
Shutdown = false;
ProcessEvent = CreateEvent( NULL,TRUE,FALSE,NULL );
SetTimerEvent = CreateEvent( NULL,TRUE,FALSE,NULL );
ShutdownEvent = CreateEvent( NULL,TRUE,FALSE,NULL );
}
/// Close the event handles
virtual ~Event()
{
CloseHandle(ProcessEvent);
CloseHandle(SetTimerEvent);
CloseHandle(ShutdownEvent);
}
/// os::Thread calls this to start the Event handler
static void StartEventHandler(void *pMyInstance)
{ ((Event *)pMyInstance)->EventHandler(); }
/// Call here to Change/Reset the timeout timer
void ResetTimer(DWORD timer) { Timer = timer; SetEvent(SetTimerEvent); }
/// Set the signal to shutdown the worker thread processing events
void SignalShutdown() { SetEvent(ShutdownEvent); while (!Shutdown) Sleep(30);}
/// Set the signal to run the process
void SignalProcess() { SetEvent(ProcessEvent); }
protected:
/// Overload in derived class to process events with worker thread
virtual void Process(){}
/// Override to process timeout- return true to terminate thread
virtual bool Timeout(){ return true;}
/// Monitor thread events
void EventHandler()
{
DWORD WaitEvents;
while (!Shutdown)
{
// Wait here, looking to be signaled what to do next
WaitEvents = WaitForMultipleObjects(3, hEvents, FALSE, Timer);
switch (WaitEvents)
{
// Process event - process event then reset for the next one
case WAIT_OBJECT_0 + 0:
Process();
ResetEvent(ProcessEvent);
break;
// Change timer event - see ResetTimer(DWORD timer)
case WAIT_OBJECT_0 + 1:
ResetEvent(SetTimerEvent);
continue;
// Shutdown requested so exit this thread
case WAIT_OBJECT_0 + 2:
Shutdown = true;
break;
// Timed out waiting for an event
case WAIT_TIMEOUT:
Shutdown = Timeout();
break;
// Failed - should never happen
case WAIT_FAILED:
break;
default:
break;
}
}
}
};
} // namespace os
How about this one (I assume you are familiar with events mechanism).
1.
thread_safe_Queue::push(something)
{
// lock the queue
...
// push object
// Signal the event
SetEvent(notification);
// unlock the queue
}
2.
thread_safe_Queue::pop(something)
{
WaitForSingleObject(notification);
// lock the queue
...
// get object
// reset the event
if (queue is empty)
ResetEvent(notification);
// unlock the queue
}
3. thread_1 just tries to pop the object and handle it. As something is pushed, the event is enabled, so pop can be successfully called. Otherwise it will wait inside pop. Actually you can use other synchronizing objects like mutexes or critical sections instead of the events in this case.
UPDATE. External events:
thread 1:
void thread_1()
{
while(1)
{
WaitForSingleObject(notification);
if (!pop(object)) // pop should return if there are any objects left in queue
SetEvent(notification);
}
}
thread_2
void thread_2()
{
while(1)
{
// push the object and than signal event
ResetEvent(notification)
}
}
You can use named events. Each thread would call CreateEvent passing in the same name. Then use WaitForMultipleObjects to wait for either queue related event or end program event. The pop thread would wait on queue_has_data and end_program events. The push thread would wait on data_available and end_program events and set the queue_has_data event when it puts something into the queue.