Imwrite is taking too long to write images in some cases

For context I have a Jetson Xavier NX and a camera connected to it, the camera can easily reach 67 fps. I convert each of these frames to a cv::Mat, this conversion is fast enough and does not affect the overall program. Between each frame grab I have 14.8 ms to do whatever I want.
What I want to do is to write each one of these frames in my SSD to tag them latter. The frames are in grayscale so I’m saving them in .PGM, I choose this format becuase in a quick test of 100 frames cv::imwrite only took ~6ms to write each image.
The frames are grabed inside a while loop and each frame captured is sent to a image writer thread via a thread safe queue, this thread calls the cv::imwrite function to write the frame to the SSD.

With what I hope it’s enough context, here’s my problem:
While most frames are written to the SSD in ~6 ms, some frames take as long as 200ms and in some rare occasions even 5 seconds! Could you help me with this?
Also I did a quick test without threads saving an image 10000 times and still most images took ~6ms but some took >50 ms too.

Here’s the capture loop of my app.

        frame = camera.GrabFrame();
            dst = fullpath + std::string("/") + std::to_string(i) + ".pgm";
            resize(frame, scaledFrame, Size(1920,1080));
            imageWriter.SaveImage(scaledFrame, dst);


And here’s is the ImageWriter worker thread.

void ImageWriter::MainWorker(){

    frameData frame_data;
    std::chrono::steady_clock::time_point begin;
    std::chrono::steady_clock::time_point end;
    float timedif = 0;

        frame_data = DequeueFrame();
        if(std::string("NULL").compare(frame_data.path) == 0){
        begin = std::chrono::steady_clock::now();
        //std::cout << frame_data.path << std::endl;
        imwrite(frame_data.path.c_str(), frame_data.frame);
        end = std::chrono::steady_clock::now();

        timedif = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();

        if(timedif > 8){
            std::cout << "imwrite is taking to much time " << timedif << "\n";

    std::cout << "Exit thread" << std::endl;

there is nothing we can do on the level of OpenCV.

this is an operating system and hardware issue. maybe your OS sucks at write caching (misconfigured?). maybe it decides to goof off and do something else while it’s supposed to write. or maybe your SSD is defective.

or maybe you’re telling it to write faster than it can actually write. consider your data rate (frame rate * resolution * bytes per pixel). consider if your storage can sustain that data rate. OSes cache writes into RAM before actually executing them. SSDs commonly have “fast cache” and when that’s full, you write at regular speeds.