Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use condition variables from the C++ standard or from the Windows API?

When implementing condition variables into a Win32 C++ program, would it be better to use Win32 functions, classes, and data types (e.g. CreateThread, SleepConditionVariableCS, WaitForSingleObjectEx, ReleaseMutex, CONDITION_VARIABLE) or those from the C++11 standard libraries (e.g. thread, wait, join, unlock, condition_variable)?

Since the answer to this question is probably not binary, what considerations should one take into account when making such a decision?

like image 817
Cerran Avatar asked Jan 27 '14 16:01

Cerran


1 Answers

The C++ synchronization mechanisms are designed to C++ principles. They free their resources in the destructor, and they also use RAII to ensure safe locking. They use exceptions to signal errors.

Essentially, they are much harder to use incorrectly than the function-based native Windows API. This means that if you can use them (your implementation supports them), you always should use them.

Oh, and they are cross-platform.

like image 178
Sebastian Redl Avatar answered Sep 22 '22 17:09

Sebastian Redl