일반 for 루프를 Parallel.For 루프로 변환하려고했습니다. this-중첩 된 Parallel.For 루프 내에서의 동기화
Parallel.For(0, bitmapImage.Width - 1, i =>
{
Parallel.For(0, bitmapImage.Height - 1, x =>
{
System.Drawing.Color oc = bitmapImage.GetPixel(i, x);
int gray = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
System.Drawing.Color nc = System.Drawing.Color.FromArgb(oc.A, gray, gray, gray);
bitmapImage.SetPixel(i, x, nc);
});
});
속으로
for (int i = 0; i < bitmapImage.Width; i++)
{
for (int x = 0; x < bitmapImage.Height; x++)
{
System.Drawing.Color oc = bitmapImage.GetPixel(i, x);
int gray = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
System.Drawing.Color nc = System.Drawing.Color.FromArgb(oc.A, gray, gray, gray);
bitmapImage.SetPixel(i, x, nc);
}
}
This-
그것은객체가 다른 곳에서 현재 사용중인 메시지 -
실패합니다.
이하의 줄은 비 스레드 안전 Reasources에 액세스하려고하는 여러 스레드 때문입니다. 내가 어떻게이 일을 할 수 있는지 아는가?
System.Drawing.Color oc = bitmapImage.GetPixel(i, x);
리소스를 동시에 읽거나 변경할 수 없기 때문에 할 수 없습니다. 첫 번째 버전 만 작동하는 유일한 버전이며 잠금을 추가하면 오버 헤드가 증가하고 첫 번째 버전보다 속도가 느려집니다. – Igor
@Igor 감사합니다. 나는 똑같이 생각했다. –
Image가 GUI 관련 클래스이므로 단일 스레드 액세스 사용을 위해 만들어졌습니다. 독립형 matrice에서 계산을 시도한 다음 단일 for 루프에서 이미지를 업데이트 할 수 있습니다. – VMAtm