2013-05-21 4 views
0

this great project을 사용하려고하지만 많은 이미지를 스캔해야하므로 프로세스가 많은 시간을 필요로하므로 멀티 스레딩을 고려하고있었습니다.
그러나 이미지의 실제 처리를하는 클래스는 Static methods을 사용하고 에 의해 ref을 조작하고 있으므로 올바르게 처리하는 방법을 잘 모르겠습니다. 내 메인 스레드에서 호출 방법은 : 대부분의 내가 그것을 디버깅 시간을 보냈어요C에서 스레드에서 정적 메서드 호출 #

List<string> filePaths = new List<string>(); 
     Parallel.For(0, filePaths.Count, a => 
       { 
        ArrayList al = new ArrayList(); 
        BarcodeImaging.ScanPage(ref al, ...); 
       }); 

과 :이 기능을 다음과 같이 사용을 사용하는 것이 안전 경우

public static void ScanPage(ref System.Collections.ArrayList CodesRead, Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types) 
{ 
    //added only the signature, actual class has over 1000 rows 
    //inside this function there are calls to other 
    //static functions that makes some image processing 
} 

내 질문은 내가 얻은 결과는 정확했지만 지금은 재현 할 수없는 몇 가지 오류가 발생했습니다.

편집
는 여기에 클래스의 코드를 붙여 : 당신이 정적 클래스 (이 지역 변수 또는 필드에 값을 저장 알고하지 않는 말하는 방법이있다 http://pastebin.com/UeE6qBHx

+2

메소드 자체를 분석하지 않고도 아무 것도 스레드 안전하지는 않은지 여부를 알 수 있습니다! – Yahia

+1

"위대한 프로젝트"가 아직도 쓸데없는'ArrayList' 클래스를 사용하고 있다는 사실 때문에 스레드의 안전성에 신경 쓰고 있습니다. –

+0

전역 변수 (또는 매개 변수로 사용되지 않거나 함수 내에서 사용되지 않는 변수)를 사용하는 경우에는 no입니다. –

답변

0

하지 없습니다 방법).

모든 로컬 변수는 호출 당 잘되고 인스턴스화되지만 필드는 초기화되지 않습니다.

아주 나쁜 예 :

public static class TestClass 
{ 
    public static double Data; 
    public static string StringData = ""; 

    // Can, and will quite often, return wrong values. 
    // for example returning the result of f(8) instead of f(5) 
    // if Data is changed before StringData is calculated. 
    public static string ChangeStaticVariables(int x) 
    { 
     Data = Math.Sqrt(x) + Math.Sqrt(x); 
     StringData = Data.ToString("0.000"); 
     return StringData; 
    } 

    // Won't return the wrong values, as the variables 
    // can't be changed by other threads. 
    public static string NonStaticVariables(int x) 
    { 
     var tData = Math.Sqrt(x) + Math.Sqrt(x); 
     return Data.ToString("0.000"); 
    } 
} 
1

나는 그것이 스레드 안전입니다 확신 해요. 두 개의 필드가 있습니다.이 필드는 구성 필드이며 클래스 내부에서 수정되지 않습니다. 기본적으로이 클래스에는 상태가 없으며 모든 계산에 부작용이 없습니다. (내가 아주 애매한 것을 보지 않는 한).

참조가 수정되지 않았으므로 여기서 수정 기호는 필요하지 않습니다.

관련 문제