Fix double free error by moving mutex lock

This commit is contained in:
30hours 2024-09-11 14:02:39 +00:00
parent 95a8b2da7c
commit fb839736cf
2 changed files with 18 additions and 10 deletions

View file

@ -86,10 +86,11 @@ int main(int argc, char **argv)
} }
// create shared queue // create shared queue
double tBuffer; double tCpi, tBuffer;
tree["process"]["data"]["cpi"] >> tCpi;
tree["process"]["data"]["buffer"] >> tBuffer; tree["process"]["data"]["buffer"] >> tBuffer;
IqData *buffer1 = new IqData((int) (tBuffer*fs)); IqData *buffer1 = new IqData((int) (tCpi*tBuffer*fs));
IqData *buffer2 = new IqData((int) (tBuffer*fs)); IqData *buffer2 = new IqData((int) (tCpi*tBuffer*fs));
// run capture // run capture
std::thread t1([&]{capture->process(buffer1, buffer2, std::thread t1([&]{capture->process(buffer1, buffer2,
@ -97,8 +98,6 @@ int main(int argc, char **argv)
}); });
// setup process CPI // setup process CPI
double tCpi;
tree["process"]["data"]["cpi"] >> tCpi;
uint32_t nSamples = fs * tCpi; uint32_t nSamples = fs * tCpi;
IqData *x = new IqData(nSamples); IqData *x = new IqData(nSamples);
IqData *y = new IqData(nSamples); IqData *y = new IqData(nSamples);
@ -227,13 +226,12 @@ int main(int argc, char **argv)
std::thread t2([&]{ std::thread t2([&]{
while (true) while (true)
{ {
buffer1->lock();
buffer2->lock();
if ((buffer1->get_length() > nSamples) && (buffer2->get_length() > nSamples)) if ((buffer1->get_length() > nSamples) && (buffer2->get_length() > nSamples))
{ {
time.push_back(current_time_us()); time.push_back(current_time_us());
// extract data from buffer // extract data from buffer
buffer1->lock();
buffer2->lock();
for (uint32_t i = 0; i < nSamples; i++) for (uint32_t i = 0; i < nSamples; i++)
{ {
x->push_back(buffer1->pop_front()); x->push_back(buffer1->pop_front());
@ -327,6 +325,14 @@ int main(int argc, char **argv)
std::string t0_string = std::to_string(time[0]/1000); std::string t0_string = std::to_string(time[0]/1000);
socket_timestamp.sendData(t0_string); socket_timestamp.sendData(t0_string);
time.clear(); time.clear();
}
else
{
buffer1->unlock();
buffer2->unlock();
// short delay to prevent tight looping
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
} }
}); });

View file

@ -54,11 +54,13 @@ void IqData::push_back(std::complex<double> sample)
std::complex<double> IqData::pop_front() std::complex<double> IqData::pop_front()
{ {
if (data->empty()) {
throw std::runtime_error("Attempting to pop from an empty deque");
}
std::complex<double> sample = data->front(); std::complex<double> sample = data->front();
data->pop_front(); data->pop_front();
return sample; return sample;
} }
void IqData::print() void IqData::print()
{ {
int n = data->size(); int n = data->size();