Skip to content

Commit 6f7e812

Browse files
committed
fix bugs
1 parent 07d38a9 commit 6f7e812

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

paddle/fluid/operators/reader/create_double_buffer_reader_op.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ class DoubleBufferReader : public framework::DecoratedReader {
4848

4949
void start_thread() {
5050
buffer_ = framework::MakeChannel<Item>(kDoubleBufferSize);
51-
std::thread prefetch([this] { PrefetchThreadFunc(); });
52-
prefetch.detach();
51+
prefetcher_ = std::thread([this] { PrefetchThreadFunc(); });
5352
}
5453

5554
void ReadNext(std::vector<framework::LoDTensor>* out) override;
5655
void ReInit() override;
5756

58-
~DoubleBufferReader() { buffer_->Close(); }
57+
~DoubleBufferReader() {
58+
buffer_->Close();
59+
prefetcher_.join();
60+
delete buffer_;
61+
}
5962

6063
bool HasNext() const override;
6164

6265
private:
6366
void PrefetchThreadFunc();
6467

68+
std::thread prefetcher_;
6569
framework::Channel<Item>* buffer_;
6670
platform::Place place_;
6771
std::vector<std::unique_ptr<platform::DeviceContext>> ctxs_;
@@ -134,6 +138,8 @@ void DoubleBufferReader::ReadNext(std::vector<framework::LoDTensor>* out) {
134138
void DoubleBufferReader::ReInit() {
135139
reader_->ReInit();
136140
buffer_->Close();
141+
prefetcher_.join();
142+
delete buffer_;
137143
start_thread();
138144
}
139145

@@ -159,8 +165,8 @@ void DoubleBufferReader::PrefetchThreadFunc() {
159165

160166
if (!buffer_->Send(&batch)) {
161167
VLOG(5) << "WARNING: The double buffer channel has been closed. The "
162-
"prefetch thread terminate.";
163-
return;
168+
"prefetch thread will terminate.";
169+
break;
164170
}
165171
}
166172
buffer_->Close();

0 commit comments

Comments
 (0)