Skip to content

Commit

Permalink
Address Mike's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Apr 4, 2017
1 parent f912a93 commit f722010
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/aikido/util/ExecutorThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class ExecutorThread final
public:
/// Constructs from callback and period. The thread begins execution
/// immediately upon construction.
/// \param[in] callback
/// \param[in] period
/// \param[in] callback Callback to be repeatedly executed by the thread.
/// \param[in] period The period of calling the callback.
template <typename Duration>
ExecutorThread(std::function<void ()> callback, const Duration& period);

/// Default destructor. The thread is stopped as ExecutorThread is destructed.
/// Default destructor. The thread stops as ExecutorThread is destructed.
~ExecutorThread();

/// Returns true if the thread is running.
bool isRunning() const;

/// Stops the thread. It is safe to call this function even when the thread is
/// Stops the thread. It is safe to call this function even when the thread
/// already stopped.
void stop();

Expand Down
5 changes: 5 additions & 0 deletions src/util/ExecutorThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ void ExecutorThread::spin()
catch (const std::exception& e)
{
std::cerr << "Exception thrown by callback: " << e.what() << std::endl;
// TODO: We should find another way to handle this error, so we don't
// print directly to std::cerr. Unfortunately, we don't have a better
// solution yet since Aikido doesn't use any particular logging framework.
mIsRunning.store(false);

break;
}

currentTime += mPeriod;
Expand Down
11 changes: 11 additions & 0 deletions tests/util/test_Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ TEST(ExecutorThread, Execute)
exec.stop();
EXPECT_TRUE(!exec.isRunning());
}

//==============================================================================
TEST(ExecutorThread, ExceptionThrownByCallback)
{
ExecutorThread exec(
[]() { throw std::exception(); }, std::chrono::milliseconds(1));

std::this_thread::sleep_for(std::chrono::seconds(3));

EXPECT_TRUE(!exec.isRunning());
}

0 comments on commit f722010

Please sign in to comment.