C++ Timer pausing execution of program until finished - c++

C++, XCode 4.6.3, OSX 10.8.2, deploying on iOS
I am trying to create a timed event.
My thought process was to create a thread, do the timing in it and then at the end have it call another function. This is working however it is pausing the rest of the program.
//Launch a thread
std::thread t1(start_thread);
//Join the thread with the main thread
t1.join();
void start_thread()
{
std::cout << "thread started" << std::endl;
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_until(start + std::chrono::seconds(20));
stop_thread();
}
void stop_thread()
{
std::cout << "thread stopped." << std::endl;
}
Is there a way to do this that doesn't pause program execution?
Update:
I could declare the thread in the header file and join in the stop_thread():
void stop_thread()
{
std::cout << "thread stopped." << std::endl;
ti.join();
}
but that throws:
Type 'std::thread' does not provide a call operator
UPDATE 2: Calling t1.detach() instead of join seems to work.

You're right it works:
Here is an example coming from cpp reference
http://en.cppreference.com/w/cpp/thread/thread/detach
#include <iostream>
#include <chrono>
#include <thread>
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
Output:
Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.
We see that the concurrent thread ends after the thread caller ends. This is not possible if detach is not called.
Hope that helps, but Jason found the solution.

Use a class instead.
enum{ PAUSED, STARTED, STOPPED };
class AsyncEvent
{
protected:
unsigned char mState;
public:
AsyncEvent():mState(PAUSED){ mThread = std::thread(&AsyncEvent::run,this); }
~AsyncEvent(){ mThread.join(); }
private:
std::thread mThread;
void run()
{
std::cout << "thread started" << std::endl;
while(mState != STOPPED)
{
if(mState == PAUSED)break;
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_until(start + std::chrono::seconds(20));
}
}
void stop()
{
mState = STOPPED;
}
void pause()
{
mState = PAUSED;
}
};

Related

Executing a non blocking thread in C++

I have a main program, this main program executes a thread that perform an action until the user triggers a stop. The problem that I have is if I add th.join() the main program won't continue until the thread finishes. And If there is no .join() the program crashs.
#include <iostream>
#include <thread>
#include <optional>
static bool s_finished = false;
using namespace std::literals::chrono_literals;
void SendData(int id)
{
std::cout << "Working thread: " << id << std::endl;
std::cout << "Started thread id: " << std::this_thread::get_id() << std::endl;
while (!s_finished)
{
std::cout << "Working\n";
std::this_thread::sleep_for(1s);
}
}
void startRecording(std::optional<int> t)
{
std::thread th1 (SendData, 1);
//th1.join();
std::cout << "[startRecording] Other Task" << std::endl;
}
void stopRecording()
{
s_finished = true;
std::cout << "[stopRecording] Other Task" << std::endl;
}
int main()
{
std::cout << "Start Program!" << std::endl;
startRecording();
std::this_thread::sleep_for(5s);
stopRecording();
return 0;
}
How can I do this?
Joining a thread will cause the program to stop until that thread is finished, and that's why the program blocks. We have to call join() eventually so that all child threads finish before the program exits, but we shouldn't call join until we need the child thread to be finished.
The simplest way to get the program to work is to return the thread from startRecording, so that we have control of it inside main. Then, we join the thread at the end of main, after we call stopRecording.
#include <iostream>
#include <thread>
#include <optional>
#include <atomic>
// (1) This needs to be atomic to avoid data races
std::atomic<bool> s_finished { false };
using namespace std::literals::chrono_literals;
void SendData(int id)
{
std::cout << "Working thread: " << id << std::endl;
std::cout << "Started thread id: " << std::this_thread::get_id() << std::endl;
while (!s_finished)
{
std::cout << "Working\n";
std::this_thread::sleep_for(1s);
}
}
std::thread startRecording(std::optional<int> t)
{
std::thread th1 (SendData, 1);
std::cout << "[startRecording] Other Task" << std::endl;
// (2) We return the thread so we can join it in main:
return th1;
}
void stopRecording()
{
s_finished = true;
std::cout << "[stopRecording] Other Task" << std::endl;
}
int main()
{
std::cout << "Start Program!" << std::endl;
// (3) We save the thread to a variable named 'worker'
// so we can join it later. I also added an input to startRecording b/c it needed one
std::thread worker = startRecording(std::optional<int>{1});
std::this_thread::sleep_for(5s);
stopRecording();
// (4) Join here, at the end
worker.join();
return 0;
}
Now, the program prints the expected output, then exits without problems:
Start Program!
[startRecording] Other Task
Working thread: 1
Started thread id: 139985258444544
Working
Working
Working
Working
Working
[stopRecording] Other Task
I marked my changes with (1), (2), (3), and (4) in the comments of the code. They're pretty small, and if you have questions about any of them I can provide additional explanation!
Addendum - using global variables when the signature of startRecording can't be changed
In general, it's best to avoid global variables, but I know it's not always possible to do so. if startRecording's signature can't be changed, we can't return a thread, so the thread has to be accessed globally. Here's how to do that:
#include <iostream>
#include <thread>
#include <optional>
#include <atomic>
// (1) This needs to be atomic to avoid data races
std::atomic<bool> s_finished { false };
// (2) we initialize this in startRecording
std::thread worker;
using namespace std::literals::chrono_literals;
void SendData(int id)
{
std::cout << "Working thread: " << id << std::endl;
std::cout << "Started thread id: " << std::this_thread::get_id() << std::endl;
while (!s_finished)
{
std::cout << "Working\n";
std::this_thread::sleep_for(1s);
}
}
void startRecording(std::optional<int> t)
{
// (3) worker gets initialized, and thread starts
worker = std::thread(SendData, 1);
std::cout << "[startRecording] Other Task" << std::endl;
}
void stopRecording()
{
s_finished = true;
std::cout << "[stopRecording] Other Task" << std::endl;
}
int main()
{
std::cout << "Start Program!" << std::endl;
startRecording(std::optional<int>{1});
std::this_thread::sleep_for(5s);
stopRecording();
// (4) Join here, at the end
worker.join();
return 0;
}

C++ - How not to miss multiple notifications from multiple threads?

In my application, many threads notify a waiting thread. Sometimes these notifications are very close to each other in time and the waiting thread misses the notification. Is there any easy way to counter this issue? A small example code is given below. In the code, the task2 notifies the waiting thread but the waiting thread, waitingForWork, miss the notification.
#include <condition_variable>
#include <iostream>
#include <thread>
std::mutex mutex_;
std::condition_variable condVar;
bool dataReady{ false };
void waitingForWork() {
for (int i = 0; i < 2; i++)
{
std::cout << "Waiting " << std::endl;
std::unique_lock<std::mutex> lck(mutex_);
condVar.wait(lck, [] { return dataReady; });
dataReady = false;
std::cout << "Running " << std::endl;
}
}
void task1() {
std::this_thread::sleep_for(std::chrono::milliseconds{ 45 });
std::lock_guard<std::mutex> lck(mutex_);
dataReady = true;
std::cout << "Task1 Done:" << std::endl;
condVar.notify_one();
}
void task2() {
std::this_thread::sleep_for(std::chrono::milliseconds{ 46 });
std::lock_guard<std::mutex> lck(mutex_);
dataReady = true;
std::cout << "Task2 Done" << std::endl;
condVar.notify_one();
}
int main() {
std::cout << std::endl;
std::thread t1(waitingForWork);
std::thread t2(task1);
std::thread t3(task2);
t1.join();
t2.join();
t3.join();
std::cout << std::endl;
system("pause");
}
It's a multiple producer single consumer problem. Which is described here:
Multiple consumer single producer problem
So basically you have to change your code in a way that each thread have to write notifications into a threadsafe queue.
And then your worker thread has to work on this queue and will not miss anymore notifications.

Resolve deadlock issue, waiting in the main thread for multiple worker threads to finish (C++11)

I'm trying to write a program with c++11 in which multiple threads are run, and, during each cycle the main thread will wait for each thread to be finished. The program below is a testing program for this concept.
Apparently I'm missing something trivial in my implementation as it looks like I'm experiencing a deadlock (Not always, just during some random runs).
#include <iostream>
#include <stdio.h>
#include <thread>
#include <chrono>
#include <condition_variable>
#include <mutex>
using namespace std;
class Producer
{
public:
Producer(int a_id):
m_id(a_id),
m_ready(false),
m_terminate(false)
{
m_id = a_id;
m_thread = thread(&Producer::run, this);
// ensure thread is available before it is started
this_thread::sleep_for(std::chrono::milliseconds(100));
}
~Producer() {
terminate();
m_thread.join();
}
void start() {
//cout << "start " << m_id << endl;
unique_lock<mutex> runLock(m_muRun);
m_ready = true;
runLock.unlock();
m_cond.notify_all();
}
void wait() {
cout << "wait " << m_id << endl;
unique_lock<decltype(m_muRun)> runLock(m_muRun);
m_cond.wait(runLock, [this]{return !m_ready;});
}
void terminate() {
m_terminate = true;
start();
}
void run() {
do {
unique_lock<decltype(m_muRun)> runLock(m_muRun);
m_cond.wait(runLock, [this]{return m_ready;});
if (!m_terminate) {
cout << "running thread: " << m_id << endl;
} else {
cout << "exit thread: " << m_id << endl;
}
runLock.unlock();
m_ready = false;
m_cond.notify_all();
} while (!m_terminate);
}
private:
int m_id;
bool m_ready;
bool m_terminate;
thread m_thread;
mutex m_muRun;
condition_variable m_cond;
};
int main()
{
Producer producer1(1);
Producer producer2(2);
Producer producer3(3);
for (int i=0; i<10000; ++i) {
cout << i << endl;
producer1.start();
producer2.start();
producer3.start();
producer1.wait();
producer2.wait();
producer3.wait();
}
cout << "exit" << endl;
return 0;
}
The program's output when the deadlock is occurring:
....
.......
running thread: 2
running thread: 1
wait 1
wait 2
wait 3
running thread: 3
Looking at the program's output when the deadlock occurs, I suspect the bottleneck of the program is that sometimes the Producer::wait function is called, before the corresponding thread is actually started, i.e. the command Producer::start should have triggered the start, a.k. unlocking of the mutex, however it is not yet picked up by the thread's run method (Producer::run), (NB: I'm not 100% sure of this!). I'm a bit lost here, hopefully somebody can provide some help.
You have race condition in this code:
runLock.unlock();
m_ready = false;
m_ready variable must be always protected by mutex for proper synchronization. And it is completely unnecessary to wait for thread to start this_thread::sleep_for() - proper synchronization would take care of that as well so you can simply remove that line. Note this is pretty inefficient way of doing proper multithreading - there should be thread pool instead of individual object with separate mutex and condition variable each.

Why program hangs when threads detached

I wrote this code to test some behaviour of boost asio together with detached threads.
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string>
#include <thread>
class printer {
public:
printer(boost::asio::io_service &io)
: timer_(io, boost::posix_time::seconds(1)), count_(0) {
timer_.async_wait(boost::bind(&printer::print, this));
}
~printer() { std::cout << "Final count is " << count_ << "\n"; }
void print() {
if (count_ < 10) {
std::cout << "thread " << std::this_thread::get_id()
<< ", count = " << count_ << std::endl;
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
boost::asio::io_service io;
int main() {
boost::asio::io_service::work work(io);
std::cout << "main thread " << std::this_thread::get_id() << std::endl;
std::thread t([] { io.run(); });
std::thread t2([] { io.run(); });
t.detach();
t2.detach();
printer p(io);
std::string name;
std::cout << "Press a key";
std::getline(std::cin, name);
std::cout << "finished" << std::endl;
return 0;
}
I wanted to see what happens when I have two worker threads, running io_service.run method, and what happens when they are detached (especially what happens when program exits).
The first problem is that when I run this program on linux I can see only one thread id when in Printer. Somehow the second thread does not take the task from io_service, even though it should, as it is running io_service.run method.
The second problem I see is that sometimes when I press ENTER before all 10 printouts from Printer are made, the program exits normally, and sometimes not (console hangs). Why is that?
What am I doing wrong here?
The main problem in your code is that the printer get called even after its destruction: the threads are detached, so they might be running even when the main function is ended and the printer is destroyed. With this issue it is not possible to have a defined behaviour, since the threads still might work with the printer that is destroyed. The hanging sometimes happens sometimes not - undefined behavior. Why this happens hard to say specifically. What is obvious here is that the threads are working with garbage data.
Summing up the flaws:
There is a possibility for the printer instance to be used even after destruction;
There is a possibility for the io_service instance to be used even after destruction: the threads' labmdas are holding references and the run method might still be in the process of execution while the objects are destroyed (there is no any guaranties on the relative order of static variable destruction and the detached thread terminations, as well as boost::asio::io_service does not block the destructor for the run method to be finished).
My suggestion is to introduced a defined order of destruction. Unfortunately, you cannot just say: ok, I am done, threads are detached, I quit. Because there is still work going on in the threads, but the relevant objects are destroyed.
class printer {
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)), count_(0) {
timer_.async_wait(
boost::bind(&printer::print, this));
}
~printer() { std::cout << "Final count is " << count_ << "\n"; }
void print() {
if (count_ < 10) {
std::cout << "thread " << std::this_thread::get_id() << ", count = " << count_
<< std::endl;
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(
boost::bind(&printer::print, this));
}
}
boost::asio::deadline_timer timer_;
int count_;
};
boost::asio::io_service io;
int main() {
auto work = std::unique_ptr<boost::asio::io_service::work>(
new boost::asio::io_service::work(io));
std::cout << "main thread " << std::this_thread::get_id() << std::endl;
std::thread t([&] { io.run(); });
std::thread t2([&] { io.run(); });
printer p(io);
std::string name;
std::cout << "Press a key";
std::getline(std::cin, name);
work.reset();
io.stop();
t.join();
t2.join();
std::cout << "finished" << std::endl;
return 0;
}
The result of the program depends on the order in which the two detached threads are executed. It's possible that sometimes they both start running after the main program has finished, and therefore the io object has already been destroyed.
You should try to force them to run before the main program exits, either by making them joinable or, if you really want to try them as detached, by adding a sleep before the program exits.

p_thread: exit from multiple threads

I created two threads in my program. I wanted to terminate thread_1 inside thread_2 function and vice versa based on flag. I tried exit() and pthread_exit(Thread_id) but it does not work. I wanted to cancel the thread execution by calling pthread_cancel but the problem is that I cannot pass the thread id before pthread_create. Any suggestions ??
You can see how pthread_cancel works in the manpage.
However, since you mention C++, why not use the language features? Signaling one or many other threads can be done with condition-variables.
See it Live On Coliru
If you don't have C++11, you can use Boost Threads.
#include <thread>
#include <condition_variable>
#include <iostream>
using namespace std;
struct workers
{
mutex mx;
condition_variable cv;
bool canceled;
workers() : canceled(false) {}
void thread1()
{
cout << __PRETTY_FUNCTION__ << " start\n";
this_thread::sleep_for(chrono::seconds(2));
{
unique_lock<mutex> lk(mx);
cout << __PRETTY_FUNCTION__ << " signaling cancel\n";
canceled = true;
cv.notify_all();
}
this_thread::sleep_for(chrono::seconds(2));
cout << __PRETTY_FUNCTION__ << " done\n";
}
void thread2()
{
cout << __PRETTY_FUNCTION__ << " start\n";
for(;;)
{
// do some work
unique_lock<mutex> lk(mx);
if (cv.wait_for(lk, chrono::milliseconds(10), [this] { return canceled; }))
break;
}
cout << __PRETTY_FUNCTION__ << " done\n";
}
};
int main()
{
workers demo;
std::thread t1(&workers::thread1, ref(demo));
std::thread t2(&workers::thread2, ref(demo));
t1.join();
t2.join();
}
Output:
void workers::thread1() start
void workers::thread2() start
void workers::thread1() signaling cancel
void workers::thread2() done
void workers::thread1() done
Update The C++03 version with boost is Live On Coliru now too. I've added timestamps for fun:
thread1:21 2014-Mar-26 00:01:40.074269 start
thread2:37 2014-Mar-26 00:01:40.074275 start
thread1:26 2014-Mar-26 00:01:42.074873 signaling cancel
thread2:47 2014-Mar-26 00:01:42.074991 done
thread1:32 2014-Mar-26 00:01:44.075062 done