HOWTO configure and use a thread in the Programming-Language-Micro-Kernel (PLMK).
- Date
- 27 dez 2024 - link with C# and Java dokument.
-
17 dez 2024 - setup of the initial document
INDEX
Thread basics
INDEX
- See also
- https://en.wikipedia.org/wiki/Thread_(computing)
Thread (computing) from wikipedia
Thread requirements
INDEX
A thread in Programming-Language-Micro-Kernel (PLMK) is very simple:
In PLMK a server-instance always runs in an isolated environment, which means it doesn't matter whether a new process or a separate thread is used.
!on remote host! !on local host!
server1---------x x----------server2
| | | |
| child-context-1 child-context-2 |
| | | | server
parent-context-1-----x x-----parent-context-2
| |
(MqConfigS::server) (example: MqConfigS::server --fork --uds … --file …)
| |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| |
(--tcp) (--pipe, --uds, --tcp)
| |
parent-context-1-----x x-----parent-context-2
| | | | client
| child-context-1 child-context-2 |
| | | |
x------------x--------client-------x-------------x
!on local host!
- See also
- libmqmsgque - link - api
-
libmqmsgque - service - fac
-
libmqmsgque - service - api
- From (1) it follows that a thread never accesses a resource that is outside its environment, which means, for example, that a server-instance never accesses an object from another server-instance.
- There is a 1:1 relationship between a server-instance and a thread.
- All data types of a server-instance are in a structure, e.g. MkRuntimeC, which is always defined as thread-local-storage.
- Every object is firmly linked to its server-instance via MkObjectS::objRt.
- Transferring an object-pointer from one server-instance to another server-instance is a design flaw.
Thread startup
INDEX
A thread in Programming-Language-Micro-Kernel (PLMK) is created via pIoStartServer
, whereby all types of instances (pipe, spawn, fork, thread, etc.) can be created in the function.
- The thread itself is finally created by the internal function
MqSysServerThread
using the value of MqLalS::MqSysServerThreadCB.
- By default, the value of MqLalS::MqSysServerThreadCB is preset to the function
sSysServerThread
:
int thread_status,
) {
#if META_HAS_THREAD
mqthread_t threadId;
# if defined(HAVE_PTHREAD)
# define check0(cmd,...) if (cmd(__VA_ARGS__)!=0) {MkErrorSetC_1XS("Error in '" #cmd "'");goto error;}
int ret;
pthread_attr_t attr;
check0(pthread_attr_init, &attr);
check0(pthread_attr_setdetachstate, &attr, thread_status);
check0(pthread_attr_setscope, &attr, PTHREAD_SCOPE_SYSTEM);
do {
ret = pthread_create(&threadId,&attr,sSysServerThreadInit,argP);
} while (ret == EAGAIN);
check0(pthread_attr_destroy, &attr);
MkErrorDbV_1_XS (MK_ERROR_CAN_NOT_START_SERVER, name);
goto error;
}
# else
if (
unlikely ( (threadId = _beginthreadex(NULL, 0, sSysServerThreadInit, argP, 0, NULL)) == 0)) {
MkErrorDbV_1_XS (MK_ERROR_CAN_NOT_START_SERVER, name);
MkErrorSysAppend (_beginthreadex, errno);
goto error;
}
# endif
error:
#if MqRuntimeC_SysServerThread_size
#else
#endif
#else
return MkErrorDbV_1_XS (MK_ERROR_NOT_SUPPORTED, "sSysServerThread");
#define MkSysFreeNonNull(pointer)
#define MqContextCT_X(instance)
MkBufferStream16384R argvR
#endif
}
- The creation of the thread is left to the operating system, which in LINUX is for example a pthread.
- Alternatively the externe function of type MqSysServerThreadF can be assigned to MqLalS::MqSysServerThreadCB (Thread pool) which creates the thread and then has to call MqSysServerThreadMain with the argument argP.
Thread storage
INDEX - MkKernel_Storage_C_API - MkRuntimeC_C_API
The primary memory for a multi-threaded application is the thread-local-storage.
Thread pool
INDEX
The initial implementation of the Programming-Language-Micro-Kernel (PLMK) thread was without thread-pool.
The biggest difference between a single pthread-thread and an already used thread-pool-thread is:
The biggest difference between a single-threaded application (process) and a multi-threaded application is:
- The memory of a single-threaded application is cleaned up by the operating system at the end of the process.
- The memory of a multi-threaded application must be cleaned up by the programmer at the end of the thread.
The biggest difference between a multi-thread-once and a multi-thread-pool application is:
- When a thread is used once, the thread-local-storage memory is released by the operating system but not the pointer to the heap contained therein.
- The memory must therefore be cleaned up.
- When a thread is used multiple times, the thread-local-storage memory is not released and is reused.
- However, the memory must be initialized again.
- See also
- C# Thread-Pool
-
JAVA Thread-Pool
Weak or Strong reference
INDEX
A weak reference is a reference that is controlled by Target-Programming-Language (TPL), and a strong reference is a reference that is controlled by Programming-Language-Micro-Kernel (PLMK).
The difference lies in the lifetime of the object:
With the switch to thread pool in C# and Java, the reference-technology was also switched from strong to weak because there is always a memory problem in a multi-threaded application and a language like C# and Java is better equipped with the sophisticated garbage-collection than Programming-Language-Micro-Kernel (PLMK).
- See also
- C# references
-
JAVA references