yuasatakayuki / CxxUtilities

A C++ class library containing utility functions.
1 stars 3 forks source link

pthread resources are not freed #1

Closed cazou closed 4 years ago

cazou commented 4 years ago

Hey,

I have been working a bit with your implementation of Spacewire and a long running test showed me that Threads resources are not freed.

After some time, the system limit is reached and pthread_create starts returning EAGAIN.

To fix that, on linux, you need to call pthread_join to free the pthread resources.

This is the patch I suggest:

From 9309b5d461115f519961746ba6833dc2dd887100 Mon Sep 17 00:00:00 2001
From: Detlev Casanova <detlev.casanova@gmail.com>
Date: Mon, 6 Apr 2020 09:27:42 -0400
Subject: [PATCH] Thread: Call pthread_join() in destructor to free resources

Signed-off-by: Detlev Casanova <detlev.casanova@gmail.com>

diff --git a/includes/CxxUtilities/Thread.hh b/includes/CxxUtilities/Thread.hh
index f7ccdd1..667b116 100644
--- a/includes/CxxUtilities/Thread.hh
+++ b/includes/CxxUtilities/Thread.hh
@@ -41,6 +41,8 @@ public:
    }

    virtual ~Thread() {
+       /* pthread_join must be called to free thread resources */
+       pthread_join(threadid, NULL);
    }

    virtual int start() {

Thanks !

yuasatakayuki commented 4 years ago

Thanks @cazou for reporting this bug and suggesting a fix!

Committed the proposed fix as https://github.com/yuasatakayuki/CxxUtilities/commit/2d7035c433332410636b0ec510ebd9180b578541

Cheers.

cazou commented 4 years ago

Nice, thanks to you !