Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 315: making cereal thread safe #320

Merged
merged 7 commits into from
Jul 29, 2016
Merged

Conversation

ChrisBFX
Copy link

I've found two place which manipulate static objects in an unsafe manner:
Versions::find() in helpers.hpp
PolymorphicVirtualCaster() in polymorphic_impl.hpp

These are now guarded by std::unique_lock and std::mutex. These guards are enabled with the new compile define CEREAL_THREAD_SAFE or the cmake define THREAD_SAFE (default off to maintain backwards compatibility)

@AzothAmmo
Copy link
Contributor

Looks good at first glance, I'll try and test this out and merge asap. Do you think these are the only two places we need to add a mutex? I'll look around myself when I go to merge this.

@AzothAmmo
Copy link
Contributor

I'm thinking of moving the mutex locking over to the getInstance() function of StaticObject. I feel like this is the safest option to take here since these are all global objects.

It may be the case that this causes some extra locks, mostly when cereal is doing its initial set-up, but it catches all potential shared accesses in a clean way that doesn't involve any thought when writing future code for cereal.

Another change I'm going to make is to also add your preprocessor definition to cereal/macros.hpp, since users may not always be using CMAKE to handle compilation.

@AzothAmmo
Copy link
Contributor

see discussion #315, #210

@ChrisBFX
Copy link
Author

I'm thinking of moving the mutex locking over to the getInstance() function of StaticObject. I feel like this is the safest option to take here since these are all global objects.

I'm not sure this is possible as this would only guard the creation and returning of the object, but would not prevent two threads changing the same instance of a StaticObject.

@AzothAmmo
Copy link
Contributor

You're correct. Only thing I'm debating is whether it would look nicer to have an interface that would allow something like

auto lock = StaticObject<InputBindingMap<Archive>>::lock();

versus the need to have a lot of #ifdef in code.

@ChrisBFX
Copy link
Author

Yes, that's a lot better.

@AzothAmmo AzothAmmo merged commit 869a132 into USCiLab:develop Jul 29, 2016
@AzothAmmo AzothAmmo added this to the v1.2.1 milestone Jul 29, 2016
@AzothAmmo
Copy link
Contributor

Try out develop and let me know how this works out. I added some tests to try and capture the threading but I don't know if they do a good job; I wasn't able to create any errors with them.

@ChrisBFX
Copy link
Author

ChrisBFX commented Aug 1, 2016

Works for me. 👍
The tests run without errors with the new compile flag and fail otherwise (i commented the #ifdef out in the tests to see what happens), so I guess they are good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants