简体   繁体   中英

The possible causes of a SynchronizationLockException

I'm making an image processing pipeline (with one thread per processing step) but I have been struggling with a SynchronizationLockException for a while. I can't figure out why it is raised.

Here is the code:

int bufferWriterPointer;
lock (_bufferPointerLock)
{
    bufferWriterPointer = _bufferWriterPointer++;
    if (_bufferWriterPointer == BufferNbr)
          _bufferWriterPointer = 0;
}

BitmapFrame frame = BytesBuffers[bufferWriterPointer];
lock(frame)
{
    SIMDHelper.Copy(newBuffer, frame.Data);
}

lock (_bufferPointerLock)
    UnReadBufferNbr++;

Here is the context

And here is the code of the bitmapFrame

public class BitmapFrame : Frame
{
    public Bitmap Bitmap { get; set; }

    public BitmapFrame(byte[] data) : base(data) { Bitmap = null; }

    public BitmapFrame(byte[] data, Bitmap bmp) : base(data)
    {
        Bitmap = bmp;
    }
}

And the SIMDHelper class is mapping a c++ lib:

    public static void Copy(byte[] sourceBuffer, byte[] destinationBuffer)
    {
        GCHandle pinnedSrcBuffer = GCHandle.Alloc(sourceBuffer, GCHandleType.Pinned);
        IntPtr srcBufferPtr = pinnedSrcBuffer.AddrOfPinnedObject();

        GCHandle pinnedDestBuffer = GCHandle.Alloc(destinationBuffer, GCHandleType.Pinned);
        IntPtr destBuffer = pinnedDestBuffer.AddrOfPinnedObject();

        SIMD.SimdCopy(srcBufferPtr, sourceBuffer.Length, sourceBuffer.Length, 1, 1, destBuffer, sourceBuffer.Length);

        pinnedDestBuffer.Free();
        pinnedSrcBuffer.Free();
    }

With:

[DllImport(SIMD_LIBRARY_FILENAME, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SimdCopy(IntPtr sourcePointer, nint sourceStride, nint width, nint height, nint pixelSize, IntPtr destinationPointer, nint destinationStride);

I figured out that the destination byte[] array was too small (by replacing my copy fuunction by Array.Copy()), so there probably was a segmentation fault in the unmanaged code and it was creating unexpected behavior such as a SynchronizationLockException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM