3
제 질문은 클래스 기능과 스레드 안전에 관해서입니다. 이것은 좀 더 나은 이해를 얻으려고 함께 노크 한 몇 가지 테스트 코드입니다.싱글 톤 스레드 안전
public sealed class Singleton
{
public static Singleton Instance { get { return _lazyInit.Value; } }
private static readonly Lazy<Singleton> _lazyInit = new Lazy<Singleton> (() => new Singleton());
public long ExecuteAlgorithm(int n)
{
n += 2;
return new Algorithm().Fibonacci(n);
}
}
public class Algorithm
{
public long Fibonacci(int n)
{
long a = 0;
long b = 1;
for (long i = 0; i < n; i++)
{
long temp = a;
a = b;
b = temp + b;
}
return a;
}
}
}
싱글 안전 클래스의 작성,하지만 내 질문은 함수 ExecuteAlgorithm의 사용을 간주한다. ExecuteAlgorithm()을 여러 스레드에서 안전하게 사용합니까?
내 이해는 각 스레드가 로컬 함수 변수를 밀어 넣을 수있는 자체 스택을 가지고 있기 때문에 경쟁 조건을 발생시키지 않아야하며 알고리즘 인스턴스 생성은 응용 프로그램 인 힙에 만들어 질 것이라고합니다. 넓은.
내 이해가 정확합니까?
다음 내용을 읽어 보시기 바랍니다. [스레드 안전이라고하는 것은 무엇입니까?] (http://blogs.msdn.com/b/ericlippert/archive/2009/10/19/what-is- this-thing-you-call-thread-safe.aspx) – I4V