12

I didn't find a concise answer to the following problem:I have a producer - consumer threading model where main thread is the consumer while some worker thread is the producer.The producer thread runs it's thread loop during the application execution and it is possible for it to throw exceptions occasionally.The main thread is UI thread which should pop-up exception messages including those coming from different threads. How can I catch these exceptions in the main thread?

Using boost on Windows with C++0x

WorkerThread.cpp

WorkerThread::WorkerThread(){

   m_thread = boost::thread(&WorkerThread::drawThread,this);

}

void WorkerThread::drawThread()
{

         while(true)
         {
             boost::unique_lock<boost::mutex> lock(m_mutex);
              try{

                ///some work is done here...

              }catch(std::exception &e){

               /// some exception is thrown
               /// notify main thread of the exception
              }

         }


 }

Important to note that I have no ability to wrap WorkerThread in the main thread with try{}catch as it is created at some point and from then on runs on its own till the application termination.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • 11
    Dear internet, **you do not need to use `bind` with `thread`**. Just say `m_thread = boost::thread(&WorkerThread::drawThread, this);` – Jonathan Wakely Aug 13 '14 at 10:08

4 Answers4

22

Firstly, you do not need to use bind with thread. Doing so just adds unnecessary copying and makes the code harder to read. I wish everyone would stop doing that.

WorkerThread::WorkerThread(){

    m_thread = boost::thread(&WorkerThread::drawThread, this);

}

You can store an exception in an exception_ptr and pass that to the other thread, e.g. in std::queue<std::exception_ptr>:

void WorkerThread::drawThread()
{
    while(true)
    {
        boost::unique_lock<boost::mutex> lock(m_mutex);
         try{

            ///some work is done here...

         }catch(std::exception &e){
             m_queue.push(std::current_exception());
         }
    }
}

std::exception_ptr WorkerThread::last_exception()
{
    boost::lock_guard<boost::mutex> lock(m_mutex);
    std::exception_ptr e;
    if (!m_queue.empty())
    {
        e = m_queue.front();
        m_queue.pop();
    }
    return e;
}

Then in the other thread rethrow it and handle it:

if (auto ep = workerThread.last_exception())
{
    // do something with exception
    try
    {
        std::rethrow_exception(ep);
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error in worker thread: " << e.what() << '\n';
    }
}

If you can't use std::exception_ptr Boost has its own implementation of it, but I'm not sure what the Boost equivalent of current_exception is. You might need to wrap the exception in another object so the Boost exception propagation mechanism can store it.

You might want to use a separate mutex for the exception queue from the main work loop (and move the m_mutex lock inside the try block) depending how long m_mutex is usually locked by the worker thread.


A different approach uses C++11 futures, which handle passing exceptions between threads more conveniently. You need some way for the main thread to get a future for each unit of work the worker thread runs, which can be done with std::packaged_task:

class WorkerThread
{
public:
  WorkerThread();   // start m_thread, as before

  template<typename F, typename... Args>
  std::future<void> post(F f, Args&&... args)
  {
    Task task(std::bind<void>(f, std::forward<Args>(args)...));
    auto fut = task.get_future();
    std::lock_guard<std::mutex> lock(m_mutex);
    m_tasks.push(std::move(task));
    return fut;
  }

  private:
    void drawThread();
    std::mutex m_mutex;
    using Task = std::packaged_task<void()>;
    std::queue<Task> m_tasks;
    std::thread m_thread;
  };

 void WorkerThread::drawThread()
 {
    Task task;
    while(true)
    {
        {
            std::lock_guard<std::mutex> lock(m_mutex);
            task = std::move(m_tasks.front());
            m_tasks.pop();
        }
        task();   // run the task
    }
}

When the task is run any exceptions will be caught, stored in an exception_ptr and held until the result is read through the associated future.

// other thread:

auto fut = workerThread.post(&someDrawingFunc, arg1, arg2);
...
// check future for errors
try {
   fut.get();
} catch (const std::exception& e) {
   // handle it
}

The producer thread could store the future objects in a queue when posting work to the consumer, and some other piece of code could check each future in the queue to see if it's ready and call get() to handle any exception.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 3
    +1 for `packaged_task`. People jump to bare `std::thread` far too quickly, when for many purposes it is actually too low-level. – ComicSansMS Aug 13 '14 at 11:21
  • In this way, `post`ing is required to catch the exceptions, isn't it? zz.. Btw, I think this approach is very similar to `boost::asio::io_service::work` – ikh Aug 13 '14 at 11:22
  • @Jonathan Wakely Any special reason you use lock_guard in your example? – Michael IV Aug 13 '14 at 11:23
  • @ikh, no, calling `post` can only throw an exception if memory allocation fails while creating the `packaged_task` or adding it to the queue. The task is run in the `drawThread`, so any exception happens there and is caught by the `packaged_task` and stored in the `future`. – Jonathan Wakely Aug 13 '14 at 11:23
  • @MichaelIV, any reason you used `unique_lock`? Do you need to defer the lock? Or release it early? Or move it to another object? If not, why use the more complicated lock type? `lock_guard` just locks and unlocks, nothing more, and that's all the code requires. – Jonathan Wakely Aug 13 '14 at 11:25
  • @JonathanWakely thanks again for the tip.I didn't realize it is more complicated lock. – Michael IV Aug 13 '14 at 11:26
  • I think I will use your first part of the answer as I didn't really understand how the second one (C++ 11 Task) would work in my case.Also,I see that in both cases I need to run some periodic check in the main thread for the exception queue to see if it has got new exceptions. – Michael IV Aug 13 '14 at 11:34
  • Yes, you need to poll for exceptions, you can't just have exceptions get magically thrown in the main thread while it's running some other code (see n.m.'s answer) – Jonathan Wakely Aug 13 '14 at 11:39
  • why you are not directly doing m_queue.push(e); I could not understand the purpose of std::current_exception() – PapaDiHatti Mar 08 '17 at 09:30
  • @Kapil, `as I said at the start of the answer, `current_exception()` gives you an `std::exception_ptr` which can be rethrown later, and will throw the original exception without changing it. If you store `e` then you only store a `std::exception` object created by slicing. If the original exception was a `std::runtime_error` then you lose that information and only store the sliced copy of the base-class of type `std::exception`. If you store a `std::exception_ptr` there is no copying, no slicing, and no loss of information. – Jonathan Wakely Mar 08 '17 at 16:22
6

Those answer suggest you to send exception_ptr to main thread manually. That's not bad way, but I suggest you another way: std::promise / boost::promise.

(Since I don't have boost in this computer now, so I'll go with std::promise. However, there may be no big difference with boost.)

Look the example code:

#include <iostream>
#include <exception>
#include <thread>
#include <future>
#include <chrono>

void foo()
{
    throw "mission failure >o<";
}

int main()
{
    std::promise<void> prm;

    std::thread thrd([&prm] {
        try
        {
            std::this_thread::sleep_for(std::chrono::seconds(5));
            foo();
            prm.set_value();
        }
        catch (...)
        {
            prm.set_exception(std::current_exception());
        }
    });

    std::future<void> fu = prm.get_future();
    for (int i = 0; ; i++)
    {
        if (fu.wait_for(std::chrono::seconds(1)) != std::future_status::timeout)
            break;
        std::cout << "waiting ... [" << i << "]\n";
    }

    try
    {
        fu.get();
        std::cout << "mission complete!\n";
    }
    catch (const char *msg)
    {
        std::cerr << "exception: " << msg << "\n";
    }

    thrd.join(); /* sorry for my compiler's absence of std::promise::set_value_at_thread_exit */
}

The benefit of this way is 1. you don't have to manage exceptions manually - std::promise and std::future will do everything and 2. you can use all feature around std::future. In this case, I'm doing other things (outputing waiting... message) while waiting the thread exit, through std::future::wait_for.

ikh
  • 10,119
  • 1
  • 31
  • 70
  • 1
    The problem with `promise` is you can only use it once, the question shows catching exceptions in a loop, implying it needs to keep handling exceptions after the first – Jonathan Wakely Aug 13 '14 at 10:52
  • 1
    The way to do it with `future` would be for the producer to pass work to the consumer in the form of `std::packaged_task` objects, keeping hold of a `future` that shares state with each packaged_task. That way for every task posteed to the consumer there is a corresponding future, and a channel for getting the exception back – Jonathan Wakely Aug 13 '14 at 10:56
  • @JonathanWakely `packaged_task`?? I can't understand.. could you explain more? – ikh Aug 13 '14 at 11:08
  • I added another example to my answer, using `packaged_task` – Jonathan Wakely Aug 13 '14 at 11:14
2

In the worker thread, you can catch the exception, and then retrieve a std::exception_ptr using std::current_exception. You can then store this somewhere, pick it up in the main thread, and throw it with std::rethrow_exception.

  • I mean that what you said I have already read in several places but none really explains how to do it. – Michael IV Aug 13 '14 at 09:29
  • What do you mean "how to do it"? You get a `std::exception_ptr`, pass it off to the main thread, and then rethrow it. – Robert Allan Hennigan Leahy Aug 13 '14 at 09:31
  • And how do I retrieve it in the main thread?Man,if you want to answer please invest some time into concise explanation with example. – Michael IV Aug 13 '14 at 09:36
  • 1
    The same way you pass any object from one thread to another. There are various options, which is best depends on your requirements. A `std::queue` guarded by a mutex would be one way. – Jonathan Wakely Aug 13 '14 at 10:05
1

Exceptions are synchronous. Which means there is no way to pass them between threads as exceptions. You cannot tell to any old thread "stop whatever you are doing and handle this". (Well you can if you deliver a POSIX signal to it, but that's not quite a C++ exception).

You can of course always pass an object with the exception data (as opposed to the state of being in an exception-handling mode) to another thread in the same way you would pass any other data between threads. A concurrent queue will do. Then you process it in the target thread. The target thread should be actively reading data from the queue.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243