Skip to content

Commit 091d6e0

Browse files
committed
http: Do a pending c++11 simplification
Use std::unique_ptr for handling work items. This makes the code more RAII and, as mentioned in the comment, is what I planned when I wrote the code in the first place.
1 parent 8206835 commit 091d6e0

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/httpserver.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ class WorkQueue
7171
/** Mutex protects entire object */
7272
CWaitableCriticalSection cs;
7373
CConditionVariable cond;
74-
/* XXX in C++11 we can use std::unique_ptr here and avoid manual cleanup */
75-
std::deque<WorkItem*> queue;
74+
std::deque<std::unique_ptr<WorkItem>> queue;
7675
bool running;
7776
size_t maxDepth;
7877
int numThreads;
@@ -101,15 +100,11 @@ class WorkQueue
101100
numThreads(0)
102101
{
103102
}
104-
/*( Precondition: worker threads have all stopped
103+
/** Precondition: worker threads have all stopped
105104
* (call WaitExit)
106105
*/
107106
~WorkQueue()
108107
{
109-
while (!queue.empty()) {
110-
delete queue.front();
111-
queue.pop_front();
112-
}
113108
}
114109
/** Enqueue a work item */
115110
bool Enqueue(WorkItem* item)
@@ -118,7 +113,7 @@ class WorkQueue
118113
if (queue.size() >= maxDepth) {
119114
return false;
120115
}
121-
queue.push_back(item);
116+
queue.emplace_back(std::unique_ptr<WorkItem>(item));
122117
cond.notify_one();
123118
return true;
124119
}
@@ -127,18 +122,17 @@ class WorkQueue
127122
{
128123
ThreadCounter count(*this);
129124
while (running) {
130-
WorkItem* i = 0;
125+
std::unique_ptr<WorkItem> i;
131126
{
132127
boost::unique_lock<boost::mutex> lock(cs);
133128
while (running && queue.empty())
134129
cond.wait(lock);
135130
if (!running)
136131
break;
137-
i = queue.front();
132+
i = std::move(queue.front());
138133
queue.pop_front();
139134
}
140135
(*i)();
141-
delete i;
142136
}
143137
}
144138
/** Interrupt and exit loops */

0 commit comments

Comments
 (0)