diff options
| -rw-r--r-- | docs/ref/SUMMARY.md | 6 | ||||
| -rw-r--r-- | docs/ref/api/thr/nng_thread.md | 84 | ||||
| -rw-r--r-- | docs/ref/api/thread.md | 88 |
3 files changed, 90 insertions, 88 deletions
diff --git a/docs/ref/SUMMARY.md b/docs/ref/SUMMARY.md index 91aa9601..cec19706 100644 --- a/docs/ref/SUMMARY.md +++ b/docs/ref/SUMMARY.md @@ -18,11 +18,9 @@ - [nng_aio](./api/aio/nng_aio.md) - [aio_cancel](./api/aio/aio_cancel.md) - - [Synchronization Primitves](./api/synch.md) + - [Synchronization](./api/synch.md) - - [Threading and Synchronization](./api/thr/index.md) - - - [nng_thread](./api/thr/nng_thread.md) + - [Threads](./api/thread.md) - [Logging](./api/logging.md) diff --git a/docs/ref/api/thr/nng_thread.md b/docs/ref/api/thr/nng_thread.md deleted file mode 100644 index e13e77c6..00000000 --- a/docs/ref/api/thr/nng_thread.md +++ /dev/null @@ -1,84 +0,0 @@ -# nng_thread - -## NAME - -nng_thread --- thread of execution - -## SYNOPSIS - -```c -#include <nng/nng.h> - -typedef struct nng_thread nng_thread; - -int nng_thread_create(nng_thread **thrp, void (*func)(void *), void *arg); -void nng_thread_destroy(nng_thread *thr); -void nng_thread_set_name(nng_thread *thr, const char *name); -``` - -### DESCRIPTION - -The {{i:`nng_thread`}} structure is used to represent a {{i:thread}} of execution. - -In NNG, a thread has an execution fuction _func_, and can be assumed to run this concurrently -to other threads, including the main thread of the application. The thread persists -until the function _func_ returns. - -> [!TIP] -> The detail of whether the thread represents an operating system thread, -> a process, or a "green" thread (also known as a a fiber or coroutine) is determined by the platform. -> Portable applications should avoid depending on this implementation detail. - -The `nng_thread_create` function creates a thread, -running _func_ with the argument _arg_. -The thread is started immediately. -A pointer to the thread object is returned in _thrp_. - -Using threads created by this function can make it easy to write -programs that use simple sequential execution, using functions in the -_NNG_ suite that would otherwise normally wait synchronously for completion. - -When the thread is no longer needed, the {{i: `nng_thread_destroy`}} -function should be used to reap it. -(This function will block waiting for _func_ to return.) - -> [!IMPORTANT] -> Thread objects created by this function may not be real system-level -> threads capable of performing blocking I/O operations using normal blocking system calls. -> If use of blocking system calls is required (not including APIs provided -> by the _NNG_ library itself of course), then real OS-specific threads -> should be created instead (such as with `pthread_create` or similar functions.) - -> [!IMPORTANT] -> Thread objects created by this function cannot be passed to any system threading functions. - -> [!TIP] -> The system may impose limits on the number of threads that can be created. -> Typically applications should not create more than a dozen of these. -> If greater concurrency or scalability is needed, consider instead using -> an asynchronous model using [`nng_aio`][aio] structures. - -> [!TIP] -> Threads can be synchronized using [mutexes][mutex] and -> [condition variables][condvar]. - -In order to facilitate debugging, {{i:`nng_thread_set_name`}} may be called -to provide a name for the thread. This may change how the thread is represented -in debuggers. Not all platforms support setting the thread name. - -## RETURN VALUES - -The `nng_thread_create` function returns 0 on success, and non-zero otherwise. - -## ERRORS - -- `NNG_ENOMEM`: Insufficient free memory exists. - -## SEE ALSO - -[nng_cv][condvar], -[nng_mutex][mutex] - -[condvar]: ../thr/nng_cv.md -[mutex]: ../thr/nng_mtx.md -[aio]: TODO.md diff --git a/docs/ref/api/thread.md b/docs/ref/api/thread.md new file mode 100644 index 00000000..7d1b2f65 --- /dev/null +++ b/docs/ref/api/thread.md @@ -0,0 +1,88 @@ +# Threads + +Threads {{hi:threads}} provide a means of representing multiple parallel execution contexts. +_NNG_ makes use of this concept internally, but also provides for user applications +to utilize the same thread facilities. This allows one form of {{i:concurrency}} for +applications. + +> [!NOTE] +> Threads in _NNG_ are built upon platform support, which may be based upon operating +> system supplied threads, process, or coroutines. The appearance of concurrency does +> not guarantee true concurrency, and scheduling between threads may not necessarily be +> pre-emptive. While this will not adversely impact applications that use this facility +> for I/O bound concurrency, it may not provide good results for purely {{i:CPU-bound}} operations. + +> [!IMPORTANT] +> Thread objects created by this function may not be real system-level +> threads capable of performing blocking I/O operations using normal blocking system calls. +> If use of blocking system calls is required (not including APIs provided +> by the _NNG_ library itself of course), then real OS-specific threads +> should be created instead (such as with `pthread_create` or similar functions.) +> Blocking _NNG_ library calls can however be made safely from _NNG_ threads. + +> [!TIP] +> The system may impose limits on the number of threads that can be created. +> Typically applications should not create more than a dozen of these. +> If greater concurrency or scalability is needed, consider instead using +> an asynchronous model using [`nng_aio`][aio] structures. + +## Thread Structure + +```c +typedef struct nng_thread nng_thread; +``` + +The {{i:`nng_thread`}} structure represnts a thread, which is a single execution context. +A given thread will have its own stack, and CPU registers. However global state, as well +as values allocated on the heap, will be shared and accessible to all threads in the system +(See the [Synchronization][synchronization] chapter for functions to help with data sharing between different threads.) + +Multiple threads can be thought of as running concurrently, even though +they might not actually do so. + +I/O operations that block (i.e. wait for completion) will block the +thread, while allowing other threads to proceed. + +## Creating a Thread + +```c +int nng_thread_create(nng_thread **thrp, void (*func)(void *), void *arg); +``` + +The {{i:`nng_thread_create`}} function creates a thread, which will execute _func_, with +the given argument _arg_, and returns a pointer to it in _thrp_. + +The thread may begin execution immediately. + +The thread will persist until _func_ returns. + +This function returns zero on success, but may return `NNG_ENOMEM` if insufficient +resources to create a thread are available. + +## Destroying a Thread + +```c +void nng_thread_destroy(nng_thread *thr); +``` + +The {{i:`nng_thread_destroy`}} function waits for the thread _thr_ to finish execution. +This function should be called to reclaim any resources associated with the thread when done. +It also has the effect of blocking execution in the caller until _thr_ has completed executing. + +## Thread Names + +```c +void nng_thread_set_name(nng_thread *thr, const char *name); +``` + +In order to facilitate debugging, {{i:`nng_thread_set_name`}} may be called +to provide a name for the thread. This may change how the thread is represented +in debuggers. Not all platforms support setting the thread name. + +## See Also + +[Synchronization][synchronization], +[Asynchronous Operations][aio] + +[synchronization]: ../api/synch.md +[aio]: TODO.md |
