Skip to content

Commit

Permalink
do not detach if it has not been created
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Nov 15, 2021
1 parent ed8e9d5 commit 86588af
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(
athr
VERSION 3.0.2
VERSION 3.0.3
LANGUAGES C)
set(PROJECT_DESCRIPTION "Progress indicator library written in C.")

Expand Down
1 change: 1 addition & 0 deletions src/athr/thr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef void(athr_thr_start)(void *);

struct athr_thr
{
int has_been_created;
athr_thr_handle handle;
athr_thr_start *func;
void *arg;
Expand Down
9 changes: 7 additions & 2 deletions src/thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ static WRAPPER_RETURN __thr_wrapper(WRAPPER_ARG_T arg)

int thr_create(struct athr_thr *thr, athr_thr_start *func, void *arg)
{
thr->has_been_created = 0;
thr->func = func;
thr->arg = arg;
int rc = 0;

#ifdef ATHR_WINDOWS
thr->handle = CreateThread(NULL, 0, __thr_wrapper, (LPVOID)thr, 0, NULL);
return !thr->handle;
rc = !thr->handle;
#else
return pthread_create(&thr->handle, 0, __thr_wrapper, (void *)thr);
rc = pthread_create(&thr->handle, 0, __thr_wrapper, (void *)thr);
#endif
thr->has_been_created = !rc;
return rc;
}

void thr_detach(struct athr_thr *thr)
{
if (!thr->has_been_created) return;
#ifdef ATHR_WINDOWS
CloseHandle(thr->handle);
#else
Expand Down

0 comments on commit 86588af

Please sign in to comment.