Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C# delegates thread-safe?

If you have a class instance with a delegate member variable and multiple threads invoke that delegate (assume it points to a long-running method), is there any contention issues?

Do you need to lock around the delegate or is it safe for each thread to call the method the delegate points to, since each thread gets it's own call stack?

like image 640
manning18 Avatar asked Jun 14 '11 19:06

manning18


People also ask

Is C+ Same as C?

C++ is a superset of C, so both languages have similar syntax, code structure, and compilation. Almost all of C's keywords and operators are used in C++ and do the same thing. C and C++ both use the top-down execution flow and allow procedural and functional programming.

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

What is C used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

Regarding the invocation of the delegate the answer is yes.

Invoking a delegate is thread-safe because delegates are immutable. However, you must make sure that a delegate exists first. This check may require some synchronization mechanisms depending on the level of safety desired.

For example, the following could throw a NullReferenceException if SomeDelegate were set to null by another thread between the null check and the invocation.

if (SomeDelegate != null) {       SomeDelegate(); } 

The following is a little more safe. Here we are exploiting the fact that delegates are immutable. Even if another thread modifies SomeDelegate the code is harded to prevent that pesky NullReferenceException.

Action local = SomeDelegate; if (local != null) {   local(); } 

However, this might result in the delegate never being executed if SomeDelegate was assigned a non-null value in another thread. This has to do with a subtle memory barrier problem. The following is the safest method.

Action local = Interlocked.CompareExchange(ref SomeDelegate, null, null); if (local != null) {   local();   } 

Regarding the execution of the method referenced by the delegate the answer is no.

You will have to provide your own thread-safety guarentees via the use of synchronization mechanisms. This is because the CLR does not automatically provide thread-safety guarentees for the execution of delegates. It might be the case that the method does not require any further synchronization to make it safe especially if it never access shared state. However, if the method reads or writes from a shared variable then you will have to consider how to guard against concurrent access from multiple threads.

like image 81
Brian Gideon Avatar answered Sep 19 '22 17:09

Brian Gideon


No they are not thread-safe and yes you'll have to manage concurrency yourself.

like image 22
Bala R Avatar answered Sep 19 '22 17:09

Bala R