DungeonCrawl
Loading...
Searching...
No Matches
thread_handler.c File Reference

The implementation of a thread handler for working with threads. More...

#include "thread_handler.h"
#include <stdlib.h>
#include <pthread.h>

Go to the source code of this file.

Data Structures

struct  thread_func_wrapper_t

Functions

void * thread_wrapper (void *arg)
 A wrapper function arround a thread for multi platform thread implementation.
void start_simple_thread (void(*thread_func)(void))
 Starts a new thread with the given function.

Detailed Description

The implementation of a thread handler for working with threads.

Definition in file thread_handler.c.

Function Documentation

◆ start_simple_thread()

void start_simple_thread ( void(* thread_func )(void))

Starts a new thread with the given function.

The thread will be detached, so it will run independently.

Parameters
thread_funcA simple function pointer to the function that will be executed in the thread.

Definition at line 56 of file thread_handler.c.

56 {
57 pthread_t thread;
58 thread_func_wrapper_t* arg = malloc(sizeof(thread_func_wrapper_t));
59 if (!arg) return;// Fehlerbehandlung
60 arg->func = thread_func;
61
62 if (pthread_create(&thread, NULL, thread_wrapper, arg) == 0) {
63 pthread_detach(thread);// Detach den Thread
64 } else {
65 free(arg);// Fehlerbehandlung
66 }
67}
void * thread_wrapper(void *arg)
A wrapper function arround a thread for multi platform thread implementation.

◆ thread_wrapper()

void * thread_wrapper ( void * arg)

A wrapper function arround a thread for multi platform thread implementation.

Parameters
argThe arguments to pass to the thread.

Definition at line 49 of file thread_handler.c.

49 {
50 thread_func_wrapper_t* wrapper_arg = (thread_func_wrapper_t*) arg;
51 wrapper_arg->func();
52 free(wrapper_arg);
53 return NULL;
54}