Closed johanneskoester closed 8 years ago
Thanks! Exciting to see someone trying it out :).
I don't think the constraint can be removed, but I may be wrong. The work and result queues must implement Send + Sync
to be able to be referenced in the worker/joiner threads. The queue implementations I'm using only implement Send + Sync
when their contained types also implement Send + Sync
(which is important). Which is why that generic bound is enforced on the pipeline
function.
So, a solution. Send and Sync can be implemented for any arbitrary type as an unsafe impl (AFAIK, based on the nomicon). The trick IIUC is to manually ensure that they can be used safely with aliasable references (i.e. don't leak the raw pointers with pub fields or as return values, only mutate values behind raw pointers in methods which accept self
/&mut self
), and then implement Sync
as a marker trait. For example, nearly all of the standard library's collections have implementations of the marker traits, despite their use of raw pointers and other unsafe-isms. Granted, you can implement Sync
on any ol' type you want, whether it's safe or not..
Is it possible to audit the Site
type and its raw pointer fields for any potential thread safety issues and to then write unsafe impl Sync for Site {}
?
Thanks for the clarification about sync! I am using rust-htslib here. Would be great if we basically have sync support for free there. I think this is very likely, because we indeed wrap everything into safe methods. I will have a look.
Thanks, I've done that and it seems to work!
Glad to hear it!
Hi Adam, this is nice work! I have the following code:
Unfortunately, it does not compile, because the
Site
objects (which contain a C struct) only implementSend
, notSync
. Here is the error:Do you see a solution for me? Or can the
Sync
constraint be removed perhaps?