File tree Expand file tree Collapse file tree 1 file changed +5
-11
lines changed Expand file tree Collapse file tree 1 file changed +5
-11
lines changed Original file line number Diff line number Diff line change @@ -71,8 +71,7 @@ class WorkQueue
71
71
/* * Mutex protects entire object */
72
72
CWaitableCriticalSection cs;
73
73
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;
76
75
bool running;
77
76
size_t maxDepth;
78
77
int numThreads;
@@ -101,15 +100,11 @@ class WorkQueue
101
100
numThreads(0 )
102
101
{
103
102
}
104
- /* ( Precondition: worker threads have all stopped
103
+ /* * Precondition: worker threads have all stopped
105
104
* (call WaitExit)
106
105
*/
107
106
~WorkQueue ()
108
107
{
109
- while (!queue.empty ()) {
110
- delete queue.front ();
111
- queue.pop_front ();
112
- }
113
108
}
114
109
/* * Enqueue a work item */
115
110
bool Enqueue (WorkItem* item)
@@ -118,7 +113,7 @@ class WorkQueue
118
113
if (queue.size () >= maxDepth) {
119
114
return false ;
120
115
}
121
- queue.push_back ( item);
116
+ queue.emplace_back (std::unique_ptr<WorkItem>( item) );
122
117
cond.notify_one ();
123
118
return true ;
124
119
}
@@ -127,18 +122,17 @@ class WorkQueue
127
122
{
128
123
ThreadCounter count (*this );
129
124
while (running) {
130
- WorkItem* i = 0 ;
125
+ std::unique_ptr< WorkItem> i ;
131
126
{
132
127
boost::unique_lock<boost::mutex> lock (cs);
133
128
while (running && queue.empty ())
134
129
cond.wait (lock);
135
130
if (!running)
136
131
break ;
137
- i = queue.front ();
132
+ i = std::move ( queue.front () );
138
133
queue.pop_front ();
139
134
}
140
135
(*i)();
141
- delete i;
142
136
}
143
137
}
144
138
/* * Interrupt and exit loops */
You can’t perform that action at this time.
0 commit comments