mono-api-threads.html

## Working with threads

### Thread API

`mono_thread_attach`
Syntax
MonoThread* mono_thread_attach (MonoDomain *domain)

`mono_thread_create`
Syntax
void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)

`mono_thread_current`
Syntax
MonoThread* mono_thread_current (void)

`mono_thread_detach`
Syntax
void mono_thread_detach (MonoThread *thread)

`mono_thread_exit`
Syntax
void mono_thread_exit (void)

`mono_thread_get_managed_id`
Syntax
int32_t mono_thread_get_managed_id (MonoThread *thread)

Return value
the `Thread.ManagedThreadId` value of thread. Returns `-1` if thread is `NULL`.
`mono_thread_get_name_utf8`
Syntax
char* mono_thread_get_name_utf8 (MonoThread *thread)

Return value
the name of the thread in UTF-8. Return `NULL` if the thread has no name. The returned memory is owned by the caller.
`mono_thread_is_foreign`
Syntax
mono_bool mono_thread_is_foreign (MonoThread *thread)

Parameters

thread | the thread to query

Return value
`TRUE` if thread was not created by the runtime.
Description

This function allows one to determine if a thread was created by the mono runtime and has a well defined lifecycle or it's a foreign one, created by the native environment.

`mono_thread_manage`
Syntax
void mono_thread_manage (void)

`mono_thread_new_init`
Syntax
void mono_thread_new_init (intptr_t tid, gpointer stack_start, gpointer func)

`mono_threads_get_default_stacksize`
Syntax
guint32 mono_threads_get_default_stacksize (void)

`mono_threads_set_default_stacksize`
Syntax
void mono_threads_set_default_stacksize (guint32 stacksize)

`mono_thread_set_manage_callback`
Syntax
void mono_thread_set_manage_callback (MonoThread *thread, MonoThreadManageCallback func)

`mono_thread_stop`
Syntax
void mono_thread_stop (MonoThread *thread)

`mono_thread_get_main`
Syntax
MonoThread* mono_thread_get_main (void)

`mono_thread_set_main`
Syntax
void mono_thread_set_main (MonoThread *thread)

Parameters

thread | thread to set as the main thread

Description
This function can be used to instruct the runtime to treat thread as the main thread, ie, the thread that would normally execute the `Main` method. This basically means that at the end of thread, the runtime will wait for the existing foreground threads to quit and other such details.

### Monitors

`mono_monitor_enter`
Syntax
gboolean mono_monitor_enter (MonoObject *obj)

`mono_monitor_enter_v4`
Syntax
void mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)

`mono_monitor_exit`
Syntax
void mono_monitor_exit (MonoObject *obj)

`mono_monitor_try_enter`
Syntax
gboolean mono_monitor_try_enter (MonoObject *obj, guint32 ms)

### Initialization and Shutdown

`mono_thread_cleanup`
Syntax
void mono_thread_cleanup (void)

`mono_thread_detach_if_exiting`
Syntax
mono_bool mono_thread_detach_if_exiting (void)

Description

Detach the current thread from the runtime if it is exiting, i.e. it is running pthread dtors. This should be used at the end of embedding code which calls into managed code, and which can be called from pthread dtors, like `dealloc:` implementations in Objective-C.