Max 5 API Reference
In Max, there are several threads of execution. More...
![]() |
Modules | |
Critical Regions | |
A critical region is a simple mechanism that prevents multiple threads from accessing at once code protected by the same critical region. | |
Mutexes | |
Defines | |
#define | ATOMIC_INCREMENT(pv) (_InterlockedIncrement(pv)) |
Use this routine for incrementing a global counter using a threadsafe and multiprocessor safe method. | |
#define | ATOMIC_DECREMENT(pv) (_InterlockedDecrement(pv)) |
Use this routine for decrementing a global counter using a threadsafe and multiprocessor safe method. | |
Typedefs | |
typedef void | t_thread |
A Max thread. | |
typedef void * | t_systhread |
An opaque thread instance pointer. | |
typedef void * | t_systhread_mutex |
An opaque mutex handle. | |
typedef void * | t_systhread_cond |
An opaque cond handle. | |
Enumerations | |
enum | e_max_systhread_mutex_flags { SYSTHREAD_MUTEX_NORMAL = 0x00000000, SYSTHREAD_MUTEX_ERRORCHECK = 0x00000001, SYSTHREAD_MUTEX_RECURSIVE = 0x00000002 } |
systhread_mutex_new() flags More... | |
Functions | |
void | schedule (void *ob, method fun, long when, t_symbol *sym, short argc, Atom *argv) |
Cause a function to be executed at the timer level at some time in the future. | |
void | schedule_delay (void *ob, method fun, long delay, t_symbol *sym, short argc, t_atom *argv) |
Cause a function to be executed at the timer level at some time in the future specified by a delay offset. | |
long | isr (void) |
Determine whether your code is executing in the Max scheduler thread. | |
void * | defer (void *ob, method fn, t_symbol *sym, short argc, t_atom *argv) |
Defer execution of a function to the main thread if (and only if) your function is executing in the scheduler thread. | |
void * | defer_low (void *ob, method fn, t_symbol *sym, short argc, t_atom *argv) |
Defer execution of a function to the back of the queue on the main thread. | |
long | systhread_create (method entryproc, void *arg, long stacksize, long priority, long flags, t_systhread *thread) |
Create a new thread. | |
long | systhread_terminate (t_systhread thread) |
Forcefully kill a thread -- not recommended. | |
void | systhread_sleep (long milliseconds) |
Suspend the execution of the calling thread. | |
void | systhread_exit (long status) |
Exit the calling thread. | |
long | systhread_join (t_systhread thread, unsigned int *retval) |
Wait for thread to quit and get return value from systhread_exit(). | |
t_systhread | systhread_self (void) |
Return the thread instance pointer for the calling thread. | |
void | systhread_setpriority (t_systhread thread, int priority) |
Set the thread priority for the given thread. | |
int | systhread_getpriority (t_systhread thread) |
Get the thread priority for the given thread. | |
short | systhread_ismainthread (void) |
Check to see if the function currently being executed is in the main thread. | |
short | systhread_istimerthread (void) |
Check to see if the function currently being executed is in the scheduler thread. |
In Max, there are several threads of execution.
The details of these threads are highlighted in the article "Event Priority in Max (Scheduler vs. Queue)" located online at http://www.cycling74.com/story/2005/5/2/133649/9742.
Not all of the details of Max's threading model are expounded here. Most important to understand is that we typically deal the scheduler (which when overdrive is on runs in a separate and high priority thread) and the low priority queue (which always runs in the main application thread).
#define ATOMIC_DECREMENT | ( | pv | ) | (_InterlockedDecrement(pv)) |
Use this routine for decrementing a global counter using a threadsafe and multiprocessor safe method.
pv | pointer to the (int) counter. |
Definition at line 41 of file ext_atomic.h.
#define ATOMIC_INCREMENT | ( | pv | ) | (_InterlockedIncrement(pv)) |
Use this routine for incrementing a global counter using a threadsafe and multiprocessor safe method.
pv | pointer to the (int) counter. |
Definition at line 33 of file ext_atomic.h.
systhread_mutex_new() flags
SYSTHREAD_MUTEX_NORMAL |
Normal. |
SYSTHREAD_MUTEX_ERRORCHECK |
Error-checking. |
SYSTHREAD_MUTEX_RECURSIVE |
Recursive. |
Definition at line 29 of file ext_systhread.h.
Defer execution of a function to the main thread if (and only if) your function is executing in the scheduler thread.
ob | First argument passed to the function fun when it executes. | |
fn | Function to be called, see below for how it should be declared. | |
sym | Second argument passed to the function fun when it executes. | |
argc | Count of arguments in argv. argc is also the third argument passed to the function fun when it executes. | |
argv | Array containing a variable number of t_atom function arguments. If this argument is non-zero, defer allocates memory to make a copy of the arguments (according to the size passed in argc) and passes the copied array to the function fun when it executes as the fourth argument. |
The deferred function should be declared as follows:
Referenced by jit_error_code(), jit_error_sym(), and jit_post_sym().
Defer execution of a function to the back of the queue on the main thread.
ob | First argument passed to the function fun when it executes. | |
fn | Function to be called, see below for how it should be declared. | |
sym | Second argument passed to the function fun when it executes. | |
argc | Count of arguments in argv. argc is also the third argument passed to the function fun when it executes. | |
argv | Array containing a variable number of t_atom function arguments. If this argument is non-zero, defer allocates memory to make a copy of the arguments (according to the size passed in argc) and passes the copied array to the function fun when it executes as the fourth argument. |
The deferred function should be declared as follows:
long isr | ( | void | ) |
Determine whether your code is executing in the Max scheduler thread.
Cause a function to be executed at the timer level at some time in the future.
ob | First argument passed to the function fun when it executes. | |
fun | Function to be called, see below for how it should be declared. | |
when | The logical time that the function fun will be executed. | |
sym | Second argument passed to the function fun when it executes. | |
argc | Count of arguments in argv. argc is also the third argument passed to the function fun when it executes. | |
argv | Array containing a variable number of t_atom function arguments. If this argument is non-zero, defer allocates memory to make a copy of the arguments (according to the size passed in argc) and passes the copied array to the function fun when it executes as the fourth argument. |
void schedule_delay | ( | void * | ob, | |
method | fun, | |||
long | delay, | |||
t_symbol * | sym, | |||
short | argc, | |||
t_atom * | argv | |||
) |
Cause a function to be executed at the timer level at some time in the future specified by a delay offset.
ob | First argument passed to the function fun when it executes. | |
fun | Function to be called, see below for how it should be declared. | |
delay | The delay from the current time before the function will be executed. | |
sym | Second argument passed to the function fun when it executes. | |
argc | Count of arguments in argv. argc is also the third argument passed to the function fun when it executes. | |
argv | Array containing a variable number of t_atom function arguments. If this argument is non-zero, defer allocates memory to make a copy of the arguments (according to the size passed in argc) and passes the copied array to the function fun when it executes as the fourth argument. |
One use of schedule() or schedule_delay() is as an alternative to using the lockout flag. Here is an example click method that calls schedule() instead of outlet_int() surrounded by lockout_set() calls.
void myobject_click (t_myobject *x, Point pt, short modifiers) { t_atom a[1]; a[0].a_type = A_LONG; a[0].a_w.w_long = Random(); schedule_delay(x, myobject_sched, 0 ,0, 1, a); } void myobject_sched (t_myobject *x, t_symbol *s, short ac, t_atom *av) { outlet_int(x->m_out,av->a_w.w_long); }
long systhread_create | ( | method | entryproc, | |
void * | arg, | |||
long | stacksize, | |||
long | priority, | |||
long | flags, | |||
t_systhread * | thread | |||
) |
Create a new thread.
entryproc | A method to call in the new thread when the thread is created. | |
arg | An argument to pass to the method specified for entryproc. Typically this might be a pointer to your object's struct. | |
stacksize | Not used. Pass 0 for this argument. | |
priority | Pass 0 for default priority. The priority can range from -32 to 32 where -32 is low, 0 is default and 32 is high. | |
flags | Not used. Pass 0 for this argument. | |
thread | The address of a t_systhread where this thread's instance pointer will be stored. |
Referenced by jit_qt_movie_new().
void systhread_exit | ( | long | status | ) |
Exit the calling thread.
Call this from within a thread made using systhread_create() when the thread is no longer needed.
status | You will typically pass 0 for status. This value will be accessible by systhread_join(), if needed. |
int systhread_getpriority | ( | t_systhread | thread | ) |
Get the thread priority for the given thread.
thread | The thread for which to find the priority. |
short systhread_ismainthread | ( | void | ) |
Check to see if the function currently being executed is in the main thread.
short systhread_istimerthread | ( | void | ) |
Check to see if the function currently being executed is in the scheduler thread.
long systhread_join | ( | t_systhread | thread, | |
unsigned int * | retval | |||
) |
Wait for thread to quit and get return value from systhread_exit().
thread | The thread to join. | |
retval | The address of a long to hold the return value (status) from systhread_exit(). |
t_systhread systhread_self | ( | void | ) |
Return the thread instance pointer for the calling thread.
void systhread_setpriority | ( | t_systhread | thread, | |
int | priority | |||
) |
Set the thread priority for the given thread.
thread | The thread for which to set the priority. | |
priority | A value in the range -32 to 32 where -32 is lowest, 0 is default, and 32 is highest. |
void systhread_sleep | ( | long | milliseconds | ) |
Suspend the execution of the calling thread.
milliseconds | The number of milliseconds to suspend the execution of the calling thread. The actual amount of time may be longer depending on various factors. |
long systhread_terminate | ( | t_systhread | thread | ) |
Forcefully kill a thread -- not recommended.
thread | The thread to kill. |