﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Yan Cui commented on Multi threaded design guidelines for libraries: Part I</title><description>Hey Ayende, how about writing concurrent code using the actor model, such as with F#'s agents (http://www.developerfusion.com/article/140677/writing-concurrent-applications-using-f-agents/) instead? 

What are you thoughts on them?

By eradicating the use of shared state altogether and given that actors are thread-safe by default since everything inside an actor is single-threaded there is no need for synchronization. If you need higher throughput then just employ multiple agents in a simple fan-out setup.

This model of concurrency is also employed in Erlang and the basis of many highly scalable NoSQL solutions such as CouchBase and Riak.

Nowadays, the PostSharp toolkit also has support to help you with actor-based programming in C# too (http://www.sharpcrafters.com/blog/post/Actor-Based-Programming-with-C-50-and-PostSharp-Threading-Toolkit.aspx).</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment6</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment6</guid><pubDate>Sun, 25 Nov 2012 00:06:07 GMT</pubDate></item><item><title>Peter Ritchie commented on Multi threaded design guidelines for libraries: Part I</title><description>I'd argue that doing any work to make sure there's no shared state in a library *is* multi-threaded (i.e. thread-safe) design.</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment5</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment5</guid><pubDate>Wed, 17 Oct 2012 14:21:08 GMT</pubDate></item><item><title>Ayende Rahien commented on Multi threaded design guidelines for libraries: Part I</title><description>James,
Absolutely, and it is actually quite elegant.
I use similar methods here:
http://ayende.com/blog/17409/caching-the-funny-way

I discuss those approaches in my next post.</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment4</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment4</guid><pubDate>Tue, 16 Oct 2012 21:26:49 GMT</pubDate></item><item><title>James Newton-King commented on Multi threaded design guidelines for libraries: Part I</title><description>Ayende check out ThreadSafeStore:

https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs

Creating a new dictionary for every change isn't elegant but their is a finite amount of type data to cache so that isn't a problem. Its getto but it is compatible with every version of the .NET Framework and it has yet to break.</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment3</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment3</guid><pubDate>Tue, 16 Oct 2012 20:24:16 GMT</pubDate></item><item><title>Ayende Rahien commented on Multi threaded design guidelines for libraries: Part I</title><description>Demis,
Wait for tomorrow's post, it discuss exactly that.</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment2</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment2</guid><pubDate>Tue, 16 Oct 2012 16:56:26 GMT</pubDate></item><item><title>Demis Bellot commented on Multi threaded design guidelines for libraries: Part I</title><description>There's a difference between not spawning multiple threads in your libraries vs ensuring your library is thread-safe. I agree you generally want to avoid creating threads in your libraries but if you provide static utils that maintains static state you need to make it thread-safe.

i.e. ServiceStack's JSON, JSV + CSV Serializers employs aggressive static delegate caching, it maintains the caches statically so the cost of creating delegates is only incurred once. I personally wouldn't want to use a serializer that doesn't do this, paying for start-up costs more than once is not a good perf strategy.

I'm sure every many other fast .NET libs that touches reflection does the same thing and there are different strategies to achieve thread-safety: i.e. In ServiceStack.Text we use lock-free code + immutable collections. 

IMO this post could've been improved if it listed the different strategies to make your code thread-safe...

Personally I like to take advantage of static constructors and try to isolate multi-threaded code in as small surface area as possible, e.g. Rather than ensuring a single RedisClient and its socket connection is thread-safe in every API, I pass around a thread-safe Redis Client Factory instead that's used to retrieve non-thread-safe RedisClient connection instances. This confines the multi-threaded code to one spot and allows us to not have to worry about thread-safety in your instance clients.</description><link>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment1</link><guid>http://ayende.com/159201/multi-threaded-design-guidelines-for-libraries-part-i#comment1</guid><pubDate>Tue, 16 Oct 2012 16:50:25 GMT</pubDate></item></channel></rss>