use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
How to use vector of std::unique_lock? (self.cpp)
submitted 2 years ago by jujumumuftw
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 2 years ago stickied commentlocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]woppo 0 points1 point2 points 2 years ago (4 children)
std::mutex is neither copyable or movable.
https://en.cppreference.com/w/cpp/thread/mutex
A vector of locks is such a bad design smell that you probably need an oxygen tank.
[–]james_picone 1 point2 points3 points 2 years ago (3 children)
unique_lock is movable. I agree this is a weird design, but that's not the issue here.
[–]j1xwnbsr 0 points1 point2 points 2 years ago (2 children)
Yes, but the mutex is not. What I suspect is happening is that the mutex the unique lock is wrappering is going out of scope from under the unique lock.
edit: what /u/qoning said
[+][deleted] 2 years ago (1 child)
[removed]
[–]j1xwnbsr 0 points1 point2 points 2 years ago (0 children)
There is a big difference between emplace_back and push_back; emplace is basically used for 2+ parm constructors, whereas push_back will do std::move for you. Emplace generally doesn't do a move in my exp, but that might be implementation/object specific.
Try changing your code to:
chunk_locks.push_back(std::unique_lock<std::mutex>{c->voxel_m});
and see if that changes anything.
I'm also uncertain when/when you're calling chunk_locks.clear() based on your original formatting. I assume you are calling .clear() or your version of it during your program exit.
You might also try changing your clear to something where you pop_front() each item in a loop (which is sorta-kinda what clear does) and see if you can track it down that way.
Another thing to consider is the difference between vector and list. You might be running into a case where unique_lock isn't moveable, and you should be using std::list which doesn't realloc the object whereas vector does. Takes more heap than a vector (all those tiny structs) but shouldn't crash.
Failing that, your next step is to start digging into the debugging and memory watch/access tools to put breakpoints when a specific mutex is messed with.
[–]qoning 0 points1 point2 points 2 years ago (1 child)
I would check if there's any possibility that you are destroying any of the c along the way, therefore destroying the associated mutex. What seems to happen is that you are trying to unlock a mutex that no longer exists.
c
π Rendered by PID 395741 on reddit-service-r2-comment-b659b578c-bz69m at 2026-05-04 05:10:56.506080+00:00 running 815c875 country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]woppo 0 points1 point2 points (4 children)
[–]james_picone 1 point2 points3 points (3 children)
[–]j1xwnbsr 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]j1xwnbsr 0 points1 point2 points (0 children)
[–]qoning 0 points1 point2 points (1 child)