nlib
nn::nlib::LockFreePipe< N > Class Template Referencefinal

When a single sender thread sends data and a single receiver thread receives that data, you can use this class to transfer data in a lock-free manner. More...

#include "nn/nlib/LockFree.h"

Public Member Functions

 LockFreePipe () noexcept
 Instantiates the object with default parameters (default constructor).
 
size_t GetBufferSize () noexcept
 Returns the buffer size. This is thread-safe.
 
errno_t Read (void *dest, size_t nbytes) noexcept
 Reads data from a pipe. This is thread-safe. More...
 
errno_t Write (const void *src, size_t nbytes) noexcept
 Writes data to a pipe. This is thread-safe. More...
 

Detailed Description

template<size_t N>
class nn::nlib::LockFreePipe< N >

When a single sender thread sends data and a single receiver thread receives that data, you can use this class to transfer data in a lock-free manner.

Template Parameters
NThe size of the buffer. This value must be a power of two.
Description
Note that data is copied via a buffer. This class is suited for transferring data of a medium or small size. By using two LockFreePipe, you can send and receive data each other between two threads.
Sample code
LockFreePipe<1024> pipe;
.....
{
// master thread
.....
uint8_t command[16];
.....
// Send the command to the slave thread
while (pipe.Write(command, sizeof(command)) != 0) {
nlib_yield(); // or do other things
}
.....
}
{
// slave thread
.....
uint8_t command[16];
// Receive the command from the master thread
while (pipe.Read(command, sizeof(command)) != 0) {
nlib_yield(); // or do other things
}
// process command
.....
}

Definition at line 362 of file LockFree.h.

Member Function Documentation

◆ Read()

template<size_t N>
nn::nlib::LockFreePipe< N >::Read ( void *  dest,
size_t  nbytes 
)
inlinenoexcept

Reads data from a pipe. This is thread-safe.

Parameters
[out]destThe region that stores read data.
[in]nbytesThe number of bytes to be read.
Return values
0Success.
EAGAINThe buffer contains data of less than nbytes bytes.

Definition at line 371 of file LockFree.h.

◆ Write()

template<size_t N>
nn::nlib::LockFreePipe< N >::Write ( const void *  src,
size_t  nbytes 
)
inlinenoexcept

Writes data to a pipe. This is thread-safe.

Parameters
[in]srcThe region that stores data to be written.
[in]nbytesThe number of bytes to be written.
Return values
0Success.
EAGAINThe amount of free space in the buffer is less than nbytes bytes.

Definition at line 402 of file LockFree.h.


The documentation for this class was generated from the following files: