Condition Variables Manual

Table of Contents


A condition variable is used to wake a waiting thread to perform some action. It is typically used together with custom threads, see Threads Manual.


A condition variable is based on maxon::ConditionVariableInterface:

Waiting for a condition variable in a job might result in a deadlock. Consider using LazyInit or LazyInitThreaded for initialization instead.

A condition variable can have multiple dependencies. This means that Set() must be called multiple times:

A condition variable is typically used as a member variable of a custom thread:

// This example shows how a thread uses a condition variable to wait for a command.
class ExampleThread : public maxon::ThreadInterfaceTemplate<ExampleThread>
~ExampleThread() { }
// create the condition variable
return maxon::OK;
// worker
maxon::Result<void> operator ()()
while (!IsCancelled())
// check if condition is set, if not wait for another second
const maxon::Seconds timeValue = maxon::Seconds(1);
if (_condition.Wait(timeValue))
// clear the condition variable to wait again
return maxon::OK;
// set the condition variable to wake the thread
void Action()
const maxon::Char* GetName() const { return "ExampleThread"; }
maxon::ConditionVariableRef _condition;
Timer value in seconds.
Definition: timevalue.h:411
Definition: thread.h:180
char Char
signed 8 bit character
Definition: apibase.h:184
static auto Create(ARGS &&... args)
Definition: apibase.h:2750
return OK
Definition: apibase.h:2667
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
const Char * GetName() const
Definition: job.h:179
Bool IsCancelled() const
Definition: job.h:292
The maxon namespace contains all declarations of the MAXON API.
Definition: autoweight.h:14
#define iferr_scope
Definition: resultbase.h:1374
#define iferr_return
Definition: resultbase.h:1465

This example thread can be used like this:

// This example creates, starts and controls a thread with its condition variable.
if (!g_exampleThread)
// create thread and call Init()
g_exampleThread = ExampleThread::Create() iferr_return;
g_exampleThread->Init() iferr_return;
DiagnosticOutput("Start thread...");
g_exampleThread.Start() iferr_return;
if (g_exampleThread)
// wake the thread

Further Reading