我有此代码有效。
void MediaRecorder::sendData(int timeslice)
{
using namespace std::literals::chrono_literals;
while (!s_finished)
{
requestData();
std::this_thread::sleep_for(5s);
}
}
ExceptionOr<void> MediaRecorder::startRecording(Optional<int> timeslice)
{
...
if (timeslice)
{
sendData(timeslice.value());
}
return { };
}
ExceptionOr<void> MediaRecorder::stopRecording()
{
s_finished = true;
queueTaskKeepingObjectAlive(*this, TaskSource::Networking, [this] {
if (!m_isActive || state() == RecordingState::Inactive)
return;
stopRecordingInternal();
ASSERT(m_state == RecordingState::Inactive);
m_private->fetchData([this, protectedThis = makeRef(*this)](auto&& buffer, auto& mimeType) {
if (!m_isActive)
return;
dispatchEvent(BlobEvent::create(eventNames().dataavailableEvent, Event::CanBubble::No, Event::IsCancelable::No, buffer ? Blob::create(buffer.releaseNonNull(), mimeType) : Blob::create()));
if (!m_isActive)
return;
dispatchEvent(Event::create(eventNames().stopEvent, Event::CanBubble::No, Event::IsCancelable::No));
});
});
return { };
}
ExceptionOr<void> MediaRecorder::requestData()
{
m_private->fetchData([this, protectedThis = makeRef(*this)](auto&& buffer, auto& mimeType) {
if (!m_isActive)
return;
dispatchEvent(BlobEvent::create(eventNames().dataavailableEvent, Event::CanBubble::No, Event::IsCancelable::No, buffer ? Blob::create(buffer.releaseNonNull(), mimeType) : Blob::create()));
});
return { };
}
但是我想在另一个线程中执行sendData
。我尝试过
ExceptionOr<void> MediaRecorder::startRecording(Optional<int> timeslice)
{
...
if (timeslice)
{
std::thread th1 (sendData, timeslice.value());
th1.join();
}
return { };
}
但是我在此行reference to non-static member function
上收到错误std::thread th1 (sendData, timeslice.value());
这是为什么,如何解决?
对于非静态成员函数,线程构造函数需要成员函数的地址以及对象指针:
std::thread th1 (&MediaRecorder::sendData, this, timeslice.value());