Skip to content

Commit 81e1564

Browse files
committed
rt: Detach pthreads before exiting
Joinable pthreads need to be either joined or detached and we no longer join with the scheduler threads.
1 parent 5d8d591 commit 81e1564

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/rt/rust_task_thread.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ rust_task_thread::create_task(rust_task *spawner, const char *name,
319319

320320
void rust_task_thread::run() {
321321
this->start_main_loop();
322+
detach();
322323
sched->release_task_thread();
323324
}
324325

src/rt/sync/rust_thread.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
rust_thread::rust_thread() : thread(0) {
55
}
66

7+
rust_thread::~rust_thread() {
8+
}
9+
710
#if defined(__WIN32__)
811
static DWORD WINAPI
912
#elif defined(__GNUC__)
@@ -41,3 +44,12 @@ rust_thread::join() {
4144
#endif
4245
thread = 0;
4346
}
47+
48+
void
49+
rust_thread::detach() {
50+
#if !defined(__WIN32__)
51+
// Don't leak pthread resources.
52+
// http://crosstantine.blogspot.com/2010/01/pthreadcreate-memory-leak.html
53+
pthread_detach(thread);
54+
#endif
55+
}

src/rt/sync/rust_thread.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ class rust_thread {
1212
pthread_t thread;
1313
#endif
1414
rust_thread();
15+
virtual ~rust_thread();
16+
1517
void start();
1618

1719
virtual void run() {
1820
return;
1921
}
2022

2123
void join();
22-
23-
virtual ~rust_thread() {} // quiet the compiler
24+
void detach();
2425
};
2526

2627
#endif /* RUST_THREAD_H */

0 commit comments

Comments
 (0)