# Loss frame when CPU usage high

**URL:** <https://forum.opencv.org/t/loss-frame-when-cpu-usage-high/10722>\
**Category:** C++\
**Tags:** videoio, programming\
**Created:** [October 28, 2022, 6:07am UTC](https://forum.opencv.org/t/loss-frame-when-cpu-usage-high/10722 "2022-10-28T06:07:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmjimm](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@jimmjimm](https://forum.opencv.org/u/jimmjimm)\
**Post date:** [October 28, 2022, 6:07am UTC](https://forum.opencv.org/t/loss-frame-when-cpu-usage-high/10722/1 "2022-10-28T06:07:04Z")

</div>

hi, i’m new to OpenCV  
I create a thread with while loop of (grab and retrieve) to get image from camera  
then found when the CPU usage is high the image start to loss.  
there is my while loop code

```
while (1)
{
	if (!cap.isOpened())
		return CAM_ERROR_OPEN;
	i = 0;
	cap.grab();
	res = cap.retrieve(frame, 0);
	if (res==0) {
		frame_count = frame_count;
		printf("no image\n");
	}
	else {
			if (frame_count > c_depth)
			{
				frame_count = 0;
			}
			printf("%d frame get\n", frame_count);

			///////////////memory copy and split part///////////
			memcpy(img_T, frame.data, c_width * c_height * 2);
			for (size_t i = 0; i < c_total; i = i + 8)
			{
				img_L[(i / 2) + 0] = img_T[i + 3];

				img_L[(i / 2) + 1] = img_T[i + 2];

				img_L[(i / 2) + 2] = img_T[i + 1];

				img_L[(i / 2) + 3] = img_T[i + 0];

				img_R[(i / 2) + 0] = img_T[i + 7];

				img_R[(i / 2) + 1] = img_T[i + 6];

				img_R[(i / 2) + 2] = img_T[i + 5];

				img_R[(i / 2) + 3] = img_T[i + 4];
			}
			mtx.lock();
			memcpy(img_L_T + (frame_count * c_width * c_height), img_L, c_width * c_height);
			memcpy(img_R_T + (frame_count * c_width * c_height), img_R, c_width * c_height);
			frame_count++;
			mtx.unlock();
	}

}

```
