线程手册

内容表

关于

Additionally to creating jobs ( 作业手册 ) it is possible to create custom threads. Such custom threads can run in parallel to the main thread to perform various operations.

Custom Threads

A custom thread is defined by implementing a custom class based on maxon::ThreadInterfaceTemplate :

// This example shows a simple thread implementation. The thread calculates pi; // the longer the thread runs, the more accurate the result will be.

// This thread calculates pi until it is cancelled. // The longer the thread runs, the more precise the result will be. class CalculatePiThread : public maxon::ThreadInterfaceTemplate <CalculatePiThread> { public : ~CalculatePiThread() { _totalPointCount = 0; _insidePointCount = 0; }

// worker maxon::Result<void> operator ()() { // init random number generator maxon::LinearCongruentialRandom<maxon::Float32> random; random. Init (0);

// calculate pi until the thread is stopped while (! IsCancelled ()) { // check if random point is inside the unit-circle maxon::Vector2d64 point; point. x = random. Get01 (); point. y = random. Get01 (); if (point. GetSquaredLength () < 1.0) ++_insidePointCount;

++_totalPointCount;

// maximum number of points that can be handled with Int reached // stop thread if ( MAXON_UNLIKELY (_totalPointCount == maxon::LIMIT<maxon::Int>::MAX )) 取消 (); } return maxon::OK ; }

// Returns the calculated value of pi or an error if no calculation has happened yet. maxon::Result<maxon::Float> GetPI() { if (_totalPointCount == 0) return maxon::IllegalStateError( MAXON_SOURCE_LOCATION , "Thread has not calculated anything yet." _s);

const maxon::Float pi = 4.0 * maxon::Float (_insidePointCount) / maxon::Float (_totalPointCount); return pi; } const maxon::Char * GetName () const { return "CalculatPiThread" ; } private : maxon::Int _totalPointCount; maxon::Int _insidePointCount; };

用法

An instance of a custom thread is typically stored as a global static variable:

// This example defines a global static variable to store the custom thread. static maxon::ThreadRefTemplate<CalculatePiThread> g_calculatePiThread;

Custom threads are based on maxon::ThreadInterface and maxon::JobInterface . maxon::ThreadInterface functions are:

// This example shows how the custom thread is created, started and closed.

if (!g_calculatePiThread) { // create and start thread g_calculatePiThread = CalculatePiThread::Create() iferr_return ; g_calculatePiThread.Start() iferr_return ; } else { // cancel thread if it is still running g_calculatePiThread-> CancelAndWait (); // get result const maxon::Float pi = g_calculatePiThread->GetPI() iferr_return ; // print result with 20 digits after the comma DiagnosticOutput ( "PI: @" , maxon::String::FloatToString(pi, 1, 20)); // clear thread g_calculatePiThread = nullptr ; }

Further static utility functions are:

延伸阅读

maxon::JobInterface::Cancel
void Cancel()
定义: job.h:283
maxon::Vec2::y
T y
定义: vec2.h:32
maxon::JobInterface::IsCancelled
Bool IsCancelled() const
定义: job.h:293
maxon::JobInterface::GetName
const Char * GetName() const
定义: job.h:180
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::Vec2::x
T x
定义: vec2.h:31
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
maxon::Float
Float64 Float
定义: apibase.h:193
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_UNLIKELY
#define MAXON_UNLIKELY(X)
定义: compilerdetection.h:482
maxon::Result< void >
maxon::ThreadRefTemplate
定义: thread.h:203
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
maxon::LIMIT
定义: apibasemath.h:53
maxon::LinearCongruentialRandom
定义: lib_math.h:18
maxon::LinearCongruentialRandom::Init
void Init(UInt32 seed)
maxon::Vec2
A vector consisting of three components X, Y and Z.
定义: vec2.h:14
maxon::ThreadInterfaceTemplate
定义: thread.h:179
maxon::Char
char Char
signed 8 bit character
定义: apibase.h:180
maxon::Vec2::GetSquaredLength
constexpr T GetSquaredLength() const
Returns the squared length of the vector.
定义: vec2.h:388
maxon::ThreadRefTemplate::CancelAndWait
void CancelAndWait(WAITMODE mode=WAITMODE::DEFAULT)
定义: thread.h:316
maxon::LinearCongruentialRandom::Get01
FLOAT Get01()
Returns the next random value in the range of [0..1].

Copyright  © 2014-2025 乐数软件    

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