aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/loader.c10
-rw-r--r--src/loader.h1
2 files changed, 10 insertions, 1 deletions
diff --git a/src/loader.c b/src/loader.c
index 0e7079e..72843a5 100644
--- a/src/loader.c
+++ b/src/loader.c
@@ -50,6 +50,13 @@ void imv_init_loader(struct imv_loader *ldr)
pthread_mutex_init(&ldr->lock, NULL);
/* ignore this signal in case we accidentally receive it */
block_usr1_signal();
+
+ /* create new threads with PTHREAD_CREATE_DETATCHED set, so that the threads
+ * are automatically cleaned up by the operating system, without an explicit
+ * call to pthread_join()
+ */
+ pthread_attr_init(&ldr->thread_attrs);
+ pthread_attr_setdetachstate(&ldr->thread_attrs, PTHREAD_CREATE_DETACHED);
}
void imv_destroy_loader(struct imv_loader *ldr)
@@ -57,6 +64,7 @@ void imv_destroy_loader(struct imv_loader *ldr)
/* wait for any existing bg thread to finish */
pthread_join(ldr->bg_thread, NULL);
pthread_mutex_destroy(&ldr->lock);
+ pthread_attr_destroy(&ldr->thread_attrs);
if(ldr->bmp) {
FreeImage_Unload(ldr->bmp);
@@ -89,7 +97,7 @@ void imv_loader_load(struct imv_loader *ldr, const char *path,
} else if (ldr->fi_buffer != NULL) {
FreeImage_CloseMemory(ldr->fi_buffer);
}
- pthread_create(&ldr->bg_thread, NULL, &bg_new_img, ldr);
+ pthread_create(&ldr->bg_thread, &ldr->thread_attrs, &bg_new_img, ldr);
pthread_mutex_unlock(&ldr->lock);
}
diff --git a/src/loader.h b/src/loader.h
index d2418a2..d8db6e8 100644
--- a/src/loader.h
+++ b/src/loader.h
@@ -42,6 +42,7 @@ struct imv_loader {
int next_frame;
int num_frames;
double frame_time;
+ pthread_attr_t thread_attrs;
};
/* Initialises an instance of imv_loader */