Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/ActiveObject.cpp

Issue 29706560: Issue 5179 - Implement asynchronous executor with a controllable lifetime (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Feb. 23, 2018, 10:29 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <AdblockPlus/ActiveObject.h>
18
19 using namespace AdblockPlus;
20
21 ActiveObject::ActiveObject()
22 : isRunning(true)
23 {
24 thread = std::thread([this]
25 {
26 ThreadFunc();
27 });
28 }
29
30 ActiveObject::~ActiveObject()
31 {
32 Post([this]
33 {
34 isRunning = false;
35 });
36 thread.join();
37 }
38
39 void ActiveObject::Post(const Call& call)
40 {
41 if (!call)
42 return;
43 calls.push_back(call);
44 }
45
46 void ActiveObject::Post(Call&& call)
47 {
48 if (!call)
49 return;
50 calls.push_back(std::move(call));
51 }
52
53 void ActiveObject::ThreadFunc()
54 {
55 while (isRunning)
56 {
57 Call call = calls.pop_front();
58 try
59 {
60 call();
61 }
62 catch (...)
63 {
64 // do nothing, but the thread will be alive.
65 }
66 }
67 }
OLDNEW

Powered by Google App Engine
This is Rietveld