10

I'm trying out Windows support for Condition Variables today (as provided by Microsoft for Windows Vista and later). To initialize a condition variable, I call InitializeConditionVariable(), which is straightforward enough, but I don't see any way provided to destroy the condition variable when I'm done using it. Why is there no DeleteConditionVariable() function?

(I'd expect the API to be analogous to the existing CreateCriticalSection() / DestroyCriticalSection() API)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • My understanding is that there's just nothing that needs to be released; initializing a condition variable doesn't allocate memory or any sort of kernel object, and the condition variable itself is not added to any sort of list. So you can simply deallocate it or let it fall out of scope, provided of course that no other thread is currently using it. – Harry Johnston Mar 11 '15 at 01:45
  • MSDN: "Condition variables are user-mode objects". That means it is up to you to create and destroy them. No CreateConditionVariable() function either. Use a variable. – Hans Passant Mar 11 '15 at 08:45
  • @HansPassant: not really convincing, IMO, because the very similar expression "user objects" includes a number of object types that *do* need deletion. (Of course, the documentation for creating such objects always explicitly explains how to delete them, if it is necessary.) – Harry Johnston Mar 11 '15 at 19:37

1 Answers1

9

A conditional variable is a very light-weight object that is internally based on a single global kernel keyed event object that is always available through every process's entire lifetime. The conditional variable simply contains a pointer to that object. So there is nothing that needs to be freed explicitly, thus no delete function is needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770