stream: make std:thread member rather than derive from it

When deriving, C++ might believe the derived class is still movable. But
it's not and rather than deleting move constructor and operator, this
approach made simpler code.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-10-01 23:27:02 +02:00
parent 9365f0252c
commit b791621a2a

View File

@ -1090,7 +1090,7 @@ namespace stdex
_w->cv.notify_one(); _w->cv.notify_one();
} }
for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w)
w->get()->thread.join(); w->get()->join();
} }
/// ///
@ -1114,7 +1114,7 @@ namespace stdex
_w->op = worker::op_t::quit; _w->op = worker::op_t::quit;
} }
_w->cv.notify_one(); _w->cv.notify_one();
_w->thread.join(); _w->join();
m_workers.erase(w); m_workers.erase(w);
return; return;
} }
@ -1159,7 +1159,7 @@ namespace stdex
} }
protected: protected:
class worker class worker : public std::thread
{ {
public: public:
worker(_In_ basic* _source) : worker(_In_ basic* _source) :
@ -1167,9 +1167,10 @@ namespace stdex
op(op_t::noop), op(op_t::noop),
data(nullptr), data(nullptr),
length(0), length(0),
num_written(0), num_written(0)
thread(process_op, std::ref(*this)) {
{} *static_cast<std::thread*>(this) = std::thread(process_op, std::ref(*this));
}
protected: protected:
static void process_op(_Inout_ worker& w) static void process_op(_Inout_ worker& w)
@ -1211,7 +1212,6 @@ namespace stdex
size_t num_written; ///< Number of bytes written size_t num_written; ///< Number of bytes written
std::mutex mutex; std::mutex mutex;
std::condition_variable cv; std::condition_variable cv;
std::thread thread;
}; };
void foreach_worker(_In_ worker::op_t op) void foreach_worker(_In_ worker::op_t op)