Код: Выделить всё
vector all_camera_frameэто функция, которая у меня есть сейчас. он работает нормально, даже еслиframe_buffers и fram_renew являются локальными параметрами, созданными в подпотоке. можно заменить локальный параметр глобальным параметром. потому что он меняет матаданные, а не указатель на матаданные. Судя по общему мнению, я обнаружил, что он не является атомарным, поэтому необходима блокировка, я добавляю блокировку в функцию.
Код: Выделить всё
void collect_frames_from_buffers(std::vector &frames,
std::vector &frame_update,
std::vector &frame_buffers,
std::vector &frame_renew,
std::vector &set_of_frame_mutex_ptr)
{
size_t num_cameras = frames.size();
// Ensure that the number of mutexes matches the number of cameras
if (set_of_frame_mutex_ptr.size() != num_cameras) {
throw std::invalid_argument("Insufficient number of mutexes provided.");
}
// Create a vector to hold the unique locks
std::vector locks;
locks.reserve(num_cameras);
// Acquire all locks in a consistent order to avoid deadlocks
for (size_t i = 0; i < num_cameras; ++i) {
if (set_of_frame_mutex_ptr[i]) { // Check if the unique_ptr is not null
locks.emplace_back(*set_of_frame_mutex_ptr[i]);
}
else {
throw std::runtime_error("Mutex pointer is null for camera index " + std::to_string(i));
}
}
// Now all buffers are locked, proceed to swap
std::swap(frames, frame_buffers);
std::swap(frame_update, frame_renew);
// Locks will be automatically released when they go out of scope
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... lti-thread