-
Notifications
You must be signed in to change notification settings - Fork 0
Thread
A Thread is a wrapper around a function that will be executed on a different thread in parallel to the code running on the main thread or in different threads. That parallelism can be used to utilised the processing powers of modern CPS more effectively compared to a program running in a single thread as modern CPUs often have multiple cores, where each core can usually run two threads at the same time.
Using Threads can speed up your code using parallelism, but it introduces a risk known as „race condition“. There, two threads want to access the same data at the same time, where one thread writes and one thread reads. There, the order of reading and writing is undefined, so the reading thread could read the value before or after the write operation.
In C++ the thread structure contains an instance of an std::thread object. It is fully wrapped by the structure and direct access to it is discouraged because porting this to C is harder than using the built-in functions where each function has a defined C binding.
In C, the structure only contains a single uint8_t called byte. This is only due to portability reasons, as the Microsoft Visual Studio compiler dosn‘t allow empty structures to exist. The byte element is never used by the program and should not be used, as it only contains garbage data. The size of the actual structure is also not a single byte, and using it on the stack will lead to undefined behaviour.
The constructor takes in a function pointer to a function returning a void type and taking in a single void* containing arbitrary user defined data. The function has the following layout:
void foo(void* params)
The constructor also takes a parameter for the void* to pass to the function. It is ment to contain all the data the thread will use. But it is not wrapped to be thread-safe, all synchronisation between threads must be done by the end user.
When the constructor is called, the function will directly start execution on a separat thread. A thread expects to be created when the function should be called, so the start is not deferred.
The destructor takes in no arguments. But, if the thread is in a joinable state and the execution of the function is not done, it will throw an exception.
If the thread is in a joinable state, this function will wait until the function running on the separate thread is done with its execution.
Because this holds the execution on the main thread it should only be used when necessary. It should also be used as late as possible to give the function running on the thread enough time to finish, so the join command can return immediately.
The detach function makes the thread independent from the main thread. This has the effect that it won‘t be able to join the main thread again. If the main thread is terminated, a detached thread will continue operation in the background and keep the program running.
This should also be used very carefully. If the detached thread contains an infinite loop, it will run indefinitely, potentially without notifying the user. This will only waste computational recources.
This function checks wether a thread can be joined or not. If the thread is detached or if it is already joined, this function will return false. Else, it will return true.
This function swaps two thread objects. It takes in a pointer to the thread object to swap with and swaps the INTERNAL interfaces. All pointers pointing to this thread will now point to the other thread automatically.
This function is static (can be called by calling Thread:: hardware_concurrency() and it returns an ESTIMATION on the amount of available hardware threads. This can be used to thread bigger workloads, as using more threads than the CPU has is wasting CPU resources because the CPU must now schedule all threads to run on the lower number of physical threads.
The constructor takes in a function pointer to a function returning a void type and taking in a single void* containing arbitrary user defined data. The function has the following layout:
void foo(void* params)
The constructor also takes a parameter for the void* to pass to the function. It is ment to contain all the data the thread will use. But it is not wrapped to be thread-safe, all synchronisation between threads must be done by the end user.
When the constructor is called, the function will directly start execution on a separat thread. A thread expects to be created when the function should be called, so the start is not deferred.
This function returns the pointer to the new thread object or NULL if an error in the construction occurred.
The destructor takes in a pointer to the thread object to destroy. First, if the thread is not joined, an exception will be thrown. Else, the thread will close successfully and the memory allocated for the thread object will be freed. If the thread object is stored on the stack, this function will result in undefined behaviour. Because of this, it is a good practice to store thread objects on the heap.
This function takes in a pointer to the thread object to join. Internally, the C++ function join is called on the object. See its documentation for more information.
This function takes in a pointer to the thread object to detach. Internally, the C++ function detach is called on the object. See its documentation for more information.
This function takes in a pointer to the thread object to check if it can be joined. Internally, the C++ function joinable is called on the object. See its documentation for more information.
The function returns a boolean. true means that the thread object can be joined, while false means that the thread is not joinable.
This function takes in two pointers to the thread objects to swap. Internally, the C++ function swap is called. See its documentation for more information.
This function takes in no arguments. It returns an ESTIMATION on the amount of available hardware threads. Internally, the static function hardware_concurrency belonging to the tread structure is called. See its documentation for more information.