Condition Variables Manual

内容表

关于

A condition variable is used to wake a waiting thread to perform some action. It is typically used together with custom threads, see 线程手册 .

Condition

A condition variable is based on maxon::ConditionVariableInterface :

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> { public : ~ExampleThread() { }
maxon::Result<void> Init() { iferr_scope ; // create the condition variable _condition = maxon::ConditionVariableRef::Create() iferr_return ; 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)) { DiagnosticOutput ( "Action" );

// clear the condition variable to wait again _condition.Clear(); } } return maxon::OK ; }

// set the condition variable to wake the thread void Action() { _condition.Set(); } const maxon::Char * GetName () const { return "ExampleThread" ; } private : maxon::ConditionVariableRef _condition; };

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 g_exampleThread->Action(); }

延伸阅读

MESSAGERESULT::OK
@ OK
Ok.
maxon::JobInterface::IsCancelled
Bool IsCancelled() const
定义: job.h:293
maxon
The maxon namespace contains all declarations of the MAXON API.
定义: c4d_basedocument.h:15
maxon::JobInterface::GetName
const Char * GetName() const
定义: job.h:180
maxon::Seconds
Timer value in seconds.
定义: timevalue.h:425
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::Result< void >
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
maxon::ThreadInterfaceTemplate
定义: thread.h:179
maxon::Char
char Char
signed 8 bit character
定义: apibase.h:180

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1