r/linux_programming • • Jun 04 '18

forked processes finish sequence dilemma

/r/cpp_questions/comments/8oegn3/forked_processes_finish_sequence_dilemma/
2 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/promach Jun 05 '18

@Hodorgasm

I have solved all compilation errors, but during runtime, I have some race condition.

For this thread.cpp code, how to clean up data race ?

1

u/[deleted] Jun 05 '18 edited Jun 05 '18

I'm afraid I don't have too much experience with multi-threading or race conditions.

The only other thing I can suggest is to refactor your code to fully utilize C++, and particularly modern C++. You have multiple memory leaks. For instance, you call malloc in the rgb2yuv function and return a pointer to a struct YUV_packet. You end up using them in the initialization of uint8_t expected_Y, uint8_t expected_U, and uint8_t expected_Z, but you don't properly free them. There is almost never a reason to use malloc/free instead of new/delete, almost never a reason to use new/delete outside of constructors/destructors (i.e., RAII), and since C++11, rarely a reason to not use smart pointers to avoid even these. In the rgb2yuv function, there is really no reason to use any resource allocation. You should just adjust rgb2yuv to return a struct YUV_packet, not a pointer, and in the function, initialize it without malloc. i.e., YUV_packet yuv_result;.