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?
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.
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.
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.
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 ...
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.
No they are not thread-safe and yes you'll have to manage concurrency yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With