diff --git a/lib/modules/Thread.pmod b/lib/modules/Thread.pmod
index 5969c8aadf8ac77f809a913522ff98aaddec812d..06c5052a974c3d1fcad14b3d60627800da6c1214 100644
--- a/lib/modules/Thread.pmod
+++ b/lib/modules/Thread.pmod
@@ -879,6 +879,9 @@ optional class Farm
 //!   @[ResourceCount], @[MutexKey]
 //!
 optional class ResourceCountKey {
+
+  private inherit __builtin.DestructImmediate;
+
   /*semi*/private ResourceCount parent;
 
   /*semi*/private void create(ResourceCount _parent) {
diff --git a/src/builtin.cmod b/src/builtin.cmod
index fd6fc59ace6b0917033f0482b96cc8fab6ee5e99..eb9bfdd12c78a415ead37a602c920e751e0af76d 100644
--- a/src/builtin.cmod
+++ b/src/builtin.cmod
@@ -4581,6 +4581,19 @@ PIKECLASS Null
   }
 }
 
+/*! @endclass
+ */
+
+/*! @class DestructImmediate
+ *! An empty class that can be intherited to get the PROGRAM_DESTRUCT_IMMEDIATE
+ *! flag set.
+ */
+
+PIKECLASS DestructImmediate
+  program_flags PROGRAM_DESTRUCT_IMMEDIATE;
+{
+}
+
 /*! @endclass
  */
 
diff --git a/src/program.c b/src/program.c
index 56b84b704f7d8e988f4c088fc2c90b9233a3daed..470d57bb926b203fdbffc49b643714335e6a6d8e 100644
--- a/src/program.c
+++ b/src/program.c
@@ -4820,10 +4820,10 @@ void lower_inherit(struct program *p,
     return;
   }
 
-  /* Propagate the HAS_C_METHODS and CLEAR_STORAGE flags. */
-  if (p->flags & (PROGRAM_HAS_C_METHODS|PROGRAM_CLEAR_STORAGE)) {
+  /* Propagate the HAS_C_METHODS, CLEAR_STORAGE and DESTRUCT_IMMEDIATE flags. */
+  if (p->flags & (PROGRAM_HAS_C_METHODS|PROGRAM_CLEAR_STORAGE|PROGRAM_DESTRUCT_IMMEDIATE)) {
     Pike_compiler->new_program->flags |=
-      (p->flags & (PROGRAM_HAS_C_METHODS|PROGRAM_CLEAR_STORAGE));
+      (p->flags & (PROGRAM_HAS_C_METHODS|PROGRAM_CLEAR_STORAGE|PROGRAM_DESTRUCT_IMMEDIATE));
   }
 
  /* parent offset was increased by 42 for above test.. */