Is it better to create thread safe subclasses or make the main class thread safe?

Say we have a class that is frequently used in a single-thread context:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Foo
{
public List<Bar> Data;
public virtual void ChangeData()
{
for(var i =0; i < Data.Count; i++)
{
if(i % 4 == 0)
{
data[i].Baz();
}
}
}
}
</code>
<code>public class Foo { public List<Bar> Data; public virtual void ChangeData() { for(var i =0; i < Data.Count; i++) { if(i % 4 == 0) { data[i].Baz(); } } } } </code>
public class Foo
{
    public List<Bar> Data;

    public virtual void ChangeData()
    {
        for(var i =0; i < Data.Count; i++)
        {
            if(i % 4 == 0)
            {
                data[i].Baz();
            }
        }
    }
}

If we were to decide we wanted to use the thread in a multi-threaded context (in addition to its current single-threaded usage), are there advantages (assuming it has full unit test coverage) to making a thread-safe subclass ‘ThreadSafeFoo’ vs. altering ‘Foo’ to be thread safe?

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

In this specific example, it is because only a very limited subset of actions on say… Dictionary can be done performantly in ConcurrentDictionary. Subjecting all of your users to the overhead of synchronization (or the functionality limitations) is… not advised.

In the more general case, you should generally err towards using only one class. It reduces the complexity of your code. It is less code to maintain. It makes it easier for people to do “the right thing”. Sometimes it’s too onerous to provide the thread safety, and you get two classes. But if it’s not, or it’s close – just use the one.

The problem with this kind of thinking is that you often can’t just make a class thread-safe (at least not in a way that is useful), without also modifying how it works. And collections demonstrate this nicely: for example, using normal Dictionary, you could implement some kind of caching like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
</code>
<code>if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; </code>
if (!dictionary.ContainsKey(key))
    dictionary.Add(key, GetValue(key));

return dictionary[key];

But such code wouldn’t work well in a multi-threaded environment, even if there was a version of Dictionary with ContainsKey(), Add() and the indexer that are thread-safe.

Instead, using ConcurrentDictionary (with is not a subclass of Dictionary), you could write the same code like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return dictionary.GetOrAdd(key, GetValue);
</code>
<code>return dictionary.GetOrAdd(key, GetValue); </code>
return dictionary.GetOrAdd(key, GetValue);

But you usually don’t need to go that far to achieve thread-safety, the right way of using a lock is enough:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lock (dictionary)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
}
</code>
<code>lock (dictionary) { if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; } </code>
lock (dictionary)
{
    if (!dictionary.ContainsKey(key))
        dictionary.Add(key, GetValue(key));

    return dictionary[key];
}

But this means using the type in a thread-safe way, not changing it or creating a subclass.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it better to create thread safe subclasses or make the main class thread safe?

Say we have a class that is frequently used in a single-thread context:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Foo
{
public List<Bar> Data;
public virtual void ChangeData()
{
for(var i =0; i < Data.Count; i++)
{
if(i % 4 == 0)
{
data[i].Baz();
}
}
}
}
</code>
<code>public class Foo { public List<Bar> Data; public virtual void ChangeData() { for(var i =0; i < Data.Count; i++) { if(i % 4 == 0) { data[i].Baz(); } } } } </code>
public class Foo
{
    public List<Bar> Data;

    public virtual void ChangeData()
    {
        for(var i =0; i < Data.Count; i++)
        {
            if(i % 4 == 0)
            {
                data[i].Baz();
            }
        }
    }
}

If we were to decide we wanted to use the thread in a multi-threaded context (in addition to its current single-threaded usage), are there advantages (assuming it has full unit test coverage) to making a thread-safe subclass ‘ThreadSafeFoo’ vs. altering ‘Foo’ to be thread safe?

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

In this specific example, it is because only a very limited subset of actions on say… Dictionary can be done performantly in ConcurrentDictionary. Subjecting all of your users to the overhead of synchronization (or the functionality limitations) is… not advised.

In the more general case, you should generally err towards using only one class. It reduces the complexity of your code. It is less code to maintain. It makes it easier for people to do “the right thing”. Sometimes it’s too onerous to provide the thread safety, and you get two classes. But if it’s not, or it’s close – just use the one.

The problem with this kind of thinking is that you often can’t just make a class thread-safe (at least not in a way that is useful), without also modifying how it works. And collections demonstrate this nicely: for example, using normal Dictionary, you could implement some kind of caching like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
</code>
<code>if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; </code>
if (!dictionary.ContainsKey(key))
    dictionary.Add(key, GetValue(key));

return dictionary[key];

But such code wouldn’t work well in a multi-threaded environment, even if there was a version of Dictionary with ContainsKey(), Add() and the indexer that are thread-safe.

Instead, using ConcurrentDictionary (with is not a subclass of Dictionary), you could write the same code like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return dictionary.GetOrAdd(key, GetValue);
</code>
<code>return dictionary.GetOrAdd(key, GetValue); </code>
return dictionary.GetOrAdd(key, GetValue);

But you usually don’t need to go that far to achieve thread-safety, the right way of using a lock is enough:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lock (dictionary)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
}
</code>
<code>lock (dictionary) { if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; } </code>
lock (dictionary)
{
    if (!dictionary.ContainsKey(key))
        dictionary.Add(key, GetValue(key));

    return dictionary[key];
}

But this means using the type in a thread-safe way, not changing it or creating a subclass.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it better to create thread safe subclasses or make the main class thread safe?

Say we have a class that is frequently used in a single-thread context:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Foo
{
public List<Bar> Data;
public virtual void ChangeData()
{
for(var i =0; i < Data.Count; i++)
{
if(i % 4 == 0)
{
data[i].Baz();
}
}
}
}
</code>
<code>public class Foo { public List<Bar> Data; public virtual void ChangeData() { for(var i =0; i < Data.Count; i++) { if(i % 4 == 0) { data[i].Baz(); } } } } </code>
public class Foo
{
    public List<Bar> Data;

    public virtual void ChangeData()
    {
        for(var i =0; i < Data.Count; i++)
        {
            if(i % 4 == 0)
            {
                data[i].Baz();
            }
        }
    }
}

If we were to decide we wanted to use the thread in a multi-threaded context (in addition to its current single-threaded usage), are there advantages (assuming it has full unit test coverage) to making a thread-safe subclass ‘ThreadSafeFoo’ vs. altering ‘Foo’ to be thread safe?

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

I was wondering about this because there are specific subclasses in System.Collections that are intended to be used across threads, and I wanted to know whether there was a reason this thread-safety wasn’t simply rolled into List, etc.

In this specific example, it is because only a very limited subset of actions on say… Dictionary can be done performantly in ConcurrentDictionary. Subjecting all of your users to the overhead of synchronization (or the functionality limitations) is… not advised.

In the more general case, you should generally err towards using only one class. It reduces the complexity of your code. It is less code to maintain. It makes it easier for people to do “the right thing”. Sometimes it’s too onerous to provide the thread safety, and you get two classes. But if it’s not, or it’s close – just use the one.

The problem with this kind of thinking is that you often can’t just make a class thread-safe (at least not in a way that is useful), without also modifying how it works. And collections demonstrate this nicely: for example, using normal Dictionary, you could implement some kind of caching like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
</code>
<code>if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; </code>
if (!dictionary.ContainsKey(key))
    dictionary.Add(key, GetValue(key));

return dictionary[key];

But such code wouldn’t work well in a multi-threaded environment, even if there was a version of Dictionary with ContainsKey(), Add() and the indexer that are thread-safe.

Instead, using ConcurrentDictionary (with is not a subclass of Dictionary), you could write the same code like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return dictionary.GetOrAdd(key, GetValue);
</code>
<code>return dictionary.GetOrAdd(key, GetValue); </code>
return dictionary.GetOrAdd(key, GetValue);

But you usually don’t need to go that far to achieve thread-safety, the right way of using a lock is enough:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lock (dictionary)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, GetValue(key));
return dictionary[key];
}
</code>
<code>lock (dictionary) { if (!dictionary.ContainsKey(key)) dictionary.Add(key, GetValue(key)); return dictionary[key]; } </code>
lock (dictionary)
{
    if (!dictionary.ContainsKey(key))
        dictionary.Add(key, GetValue(key));

    return dictionary[key];
}

But this means using the type in a thread-safe way, not changing it or creating a subclass.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật