Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've been hearing the term atomic variable for a while now and so far I don't no what is that,
so I'd like to see an example and why we use atomic variable if possible in C#
and thank you very much.
Atomic operations are thread-safe operations that execute atomically, that is there is no thread-switch while the operation is executing (or at least the result of a thread-switch is not observable from the outside) so practically the operation is executed as a one-step. On the .Net platform this is provided by the Interlocked class. Other platforms, such as Java provide various other classes, like AtomicInteger. An instance of the AtomicInteger (in Java) could be called an atomic variable, so I'm guessing that's what you are referring to when you say an atomic variable.
The main point about Atomic/Interlocked objects is that they don't require any outside locks or other synchronization objects to achieve atomicity and thus thread-safety.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I often find some methods in some API are not thread safe. For example, let consider the EF context. Here is the warning I can quote from asp.net core tutorial:
An EF context is not thread safe: don't try to do multiple operations in parallel. When you call any async EF method, always use the await keyword.
Questions
Generally, the thread safeness of a method is decided by design or it is a mistake that is spotted later on? For the EF context above, why don't make it thread safe?
In general make mutable datastructure thread-safe is very hard and has negative impact on single threaded use cases.
Often is required different (more complex) api to do the changes (i.e. Dictionary vs. ConcurrentDictionary).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was asked at an interview today about how I would implement my own Thread. As a hint I was told that implmenting Runnable was one thing to consider, and what are the others?
I was completely stumped but even after researching it online, I still have no idea what the answer is or if it was even a valid question. I'm leaning towards the latter.
So my question is:
What things would you need to consider if you wanted to write your own implementation of the Thread class in C#?
First up, Runnable probably refers to Action. Action is a delegate type used to store a reference to a method. You use this to tell a thread what method to start on.
I expect they only wanted you to illustrate how to;
start a thread with a delegate (that is how Runnable enters the fray).
-or-
schedule work to be performed on a worker thread. I would use a call to Task.Run(new Action(....)) for this. It is concise and modern.
I would always do the latter until instructed to detail the lower level Thread class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am frustrated that my programs crash due to the following two problems:
Infinite loops (e.g. C# or Javascript)
SQL joins where I forgot to add a join clause
These seem to be preventable problems if the compilers were competent enough. How can these problems be prevented programatically?
Modern compilers can and do unroll loops for optimization reasons, but without knowing some of the data ahead of time, can't even make a heuristic for whether your loops will terminate (See: dataflow programming). In fact, deciding whether your program itself will terminate is called the Halting Problem
In other cases, you want infinite loops. For example a graphics engine usually does something like this:
while(true)
render
As for your SQL joins... I guess it should be pretty obvious when you miss one. In some cases, an INNER JOIN is implied when you don't give one, so in that sense your compiler is fixing this exact issue.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm implementing a network library, and I'm making it asynchronous (with beginxxx-endxxx, not async/await). I also want to leave some synchronous methods, which I plan on implementing like
xxx()
{
var r=BeginXXX();
EndXXX(r);
}
I know that doing something similar with async/await is a bad idea and will cause deadlocks.
Is this a good idea with Begin/End, or will this cause any problems down the stream/kill app's performance?
This way of doing synchronous operations will be less performant, in particular around memory allocations. Immediately blocking on the IAsyncResult also (usually) requires you to allocate your AsyncWaitHandle, so you're also allocating an extra kernel object for each request.
In the end, it just depends on how performant you need your synchronous apps to be. If this is a general-purpose, widely-used library (e.g., System.Net.Sockets.Socket), then you should definitely not take this approach. If it's just a basic library and you don't need to support extreme scenarios, then you could probably get away with wrappers like this.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Does anyone know if there's any planned support in upcoming versions of .NET for storing objects in the ambient execution context? There's a definite need for it, as highlighted in the following pages:
Limitations of CallContext: https://connect.microsoft.com/VisualStudio/feedback/details/276325/provide-better-context-support-to-async-operations
Implicit vs Explicit Context: http://msmvps.com/blogs/jon_skeet/archive/2010/11/08/the-importance-of-context-and-a-question-of-explicitness.aspx
The types of object which could be added to ambient execution context are:
Those which are not suitable for DI. I.e. extrinsic dependencies which should be defined by the call-site, but cross-cut the application-domain in a way which makes them impractical to be passed as method arguments. Examples: CancellationToken, IProgress, Transactions, SynchronizationContext, SecurityContext. (In fact, ExecutionContext has been hardcoded to pass the latter 2 around... any good reason why an open collection cannot be exposed for custom code?)
Those which are used by generic code emitted into method bodies... code which has no knowledge about the explicit dependencies available to it as arguments on the housing method, or properties of its declaring type. Consider PostSharp Aspects in the AOP world.
Would be great to hear people's thoughts on this one :)
EDIT:
Have posted question here too: http://social.msdn.microsoft.com/Forums/en-US/async/thread/ea21ca57-5340-439c-8ee9-f0185b5787a1