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

Lazy delegate should use LazyThreadSafetyMode.PUBLICATION instead of LazyThreadSafetyMode.NONE #330

Open
valeriyo opened this issue Dec 12, 2024 · 2 comments

Comments

@valeriyo
Copy link

valeriyo commented Dec 12, 2024

I think it's wrong to use LazyThreadSafetyMode.NONE since it is unsafe for general use:

No locks are used to synchronize access and initialization of a Lazy instance value. If the instance is accessed from multiple threads, its behavior is unspecified.
This mode should not be used unless the Lazy instance is guaranteed never to be initialized from more than one thread.

The LazyThreadSafetyMode.PUBLICATION is actually safe for this use-case:

Initializer function can be called several times on concurrent access to an uninitialized Lazy instance value, but only one computed value will be used as the value of a Lazy instance and will be visible by all threads.

Thanks.

@egorikftp
Copy link
Member

egorikftp commented Dec 13, 2024

Thank you for issue 🙂
However, Google typically uses a backing field format that doesn't involve any synchronization at all.

The Lazy property format for the icon was introduced by me for more compact code without synchronization because all icons are loaded on the main thread (later if compose introduce async recompositions the situation can be changed).

If you have real case with synchronization issue, please share it.

@vkatz
Copy link
Contributor

vkatz commented Dec 13, 2024

The only case it may happen is strange code like this:

@Composable 
fun Foo(){
     var icons by remember { mutableStateOf<List<Icon>?>(emptyList() }
     LaunchedEffect(Unit){
           val icon1 = async{ MyIcon }
           val icon2 = async{ MyIcon }
           icons = listOf(icon1.await(), icon2.awit())
     }
     // draw icon
}

and this is looks SUPER VIRED

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

No branches or pull requests

3 participants