找到解决方案
我正在使用VS2013,提升1.59.0
我有func run() {
let startTime = DispatchTime.now().uptimeNanoseconds
for i in data.indices { data[i] = Int(arc4random_uniform(1000)) }
print("\((DispatchTime.now().uptimeNanoseconds - startTime)/1_000_000)")
}
let startTime = DispatchTime.now().uptimeNanoseconds
var g = DispatchGroup()
var q = DispatchQueue(label: "myQueue", qos: .userInitiated, attributes: [.concurrent])
(1...100).forEach { i in
q.async(group: g) {
run()
}
}
g.wait()
print("\((DispatchTime.now().uptimeNanoseconds - startTime)/1_000_000)")
的以下例子
我在Windows上停止boost c++ application development cookbook
时遇到问题。我现在找到一种解决方法来阻止它使用boost::ioservice
。
ios_.stopped()
问题是变量#include <boost/thread/thread.hpp>
#include <boost/asio/io_service.hpp>
namespace detail
{
template <class T>
struct task_wrapped
{
private:
T task_unwrapped_;
public:
explicit task_wrapped(const T& task_unwrapped)
: task_unwrapped_(task_unwrapped)
{}
void operator()() const
{
// resetting interruption
try
{
boost::this_thread::interruption_point();
}
catch (const boost::thread_interrupted&){}
try
{
// Executing task
task_unwrapped_();
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
}
catch (const boost::thread_interrupted&)
{
std::cerr << "Thread interrupted\n";
}
catch (...)
{
std::cerr << "Unknown exception\n";
}
}
};
template <class T>
task_wrapped<T> make_task_wrapped(const T& task_unwrapped)
{
return task_wrapped<T>(task_unwrapped);
}
} // namespace detail
class tasks_processor;
tasks_processor* pProcessor = nullptr;
class tasks_processor : private boost::noncopyable {
boost::asio::io_service ios_;
boost::asio::io_service::work work_;
tasks_processor()
: ios_()
, work_(ios_)
{}
public:
static tasks_processor& get()
{
if (pProcessor == nullptr)
{
pProcessor = new tasks_processor;
}
return *pProcessor;
}
template <class T>
inline void push_task(const T& task_unwrapped) {
ios_.post(detail::make_task_wrapped(task_unwrapped));
}
void start() {
ios_.run();
}
void stop() {
ios_.stop();
}
bool IsStopped() {
return ios_.stopped();
}
}; // tasks_processor
int g_val = 0;
void func_test()
{
if (tasks_processor::get().IsStopped())
return;
++g_val;
if (g_val == 3)
{
throw std::logic_error("Just checking");
}
boost::this_thread::interruption_point();
if (g_val == 10)
{
// Emulation of thread interruption.
// Will be caught and won't stop execution.
throw boost::thread_interrupted();
}
if (g_val == 90)
{
tasks_processor::get().stop();
}
}
int main()
{
static const std::size_t tasks_count = 100;
// stop() is called at 90
BOOST_STATIC_ASSERT(tasks_count > 90);
for (std::size_t i = 0; i < tasks_count; ++i)
{
tasks_processor::get().push_task(&func_test);
}
// We can also use result of boost::bind call
// as a task
tasks_processor::get().push_task(
boost::bind(std::plus<int>(), 2, 2) // counting 2 + 2
);
// Processing was not started.
assert(g_val == 0);
// Will not throw, but blocks till
// one of the tasks it is owning
// calls stop().
tasks_processor::get().start();
assert(g_val == 90);
return 0;
}
等于100,这意味着g_val
无效。我作为调试运行,并且已调用此命令。如何停止ios_.stop();
?
答案 0 :(得分:1)
io_service::stop()
只是改变了io_service事件循环的状态,它会导致io_service::run()
调用尽快返回。没有记录stop()
是否取消已排队的处理程序,根据此post,它似乎依赖于操作系统。因此,当g_val == 90
,io_service::stop()
被调用时,剩余的func_test()
仍可能被执行。