Permalink
Browse files

[6model/c] fix a memory leak in t/01c-osthreads.c detected up by valg…

…rind
  • Loading branch information...
1 parent 7a6f96e commit 5fa7d52926566d144e3454d358c879c3d855bb8d @mberends mberends committed Aug 9, 2011
Showing with 31 additions and 6 deletions.
  1. +23 −2 c/src/threads.c
  2. +8 −4 c/t/01-toolchain/01c-osthreads.c
View
@@ -1,12 +1,13 @@
/* threads.c */
-/* Abstract the different threading interfaces present in different */
+/* Abstract the various threading interfaces offered by different */
/* operating systems. This library closely resembles pthreads, so */
/* is a very thin wrapper on Posix systems, and does a lightweight */
-/* emulation on the others. */
+/* emulation elsewhere. */
#include "threads.h" /* thread_create */
+
/* thread_create */
int
thread_create(struct thread_info * info, void * (* function)(void *), void * arg )
@@ -22,4 +23,24 @@ thread_create(struct thread_info * info, void * (* function)(void *), void * arg
return status;
}
+
+/* thread_join */
+int
+thread_join(struct thread_info * info)
+{
+ int status;
+ #if defined( __APPLE__ ) || defined( __linux__ )
+ status = pthread_join(&info->thread_id, NULL);
+ #elif defined( _WIN32 )
+ status = WaitForSingleObject(threadhandle[i], INFINITE);
+ #endif
+ return status;
+}
+
+
+/* See also: */
+/* Lawrence Livermore National Laboratory tutorials: */
+/* pthreads https://computing.llnl.gov/tutorials/pthreads/ */
+/* OpenMP https://computing.llnl.gov/tutorials/openMP/ */
+
/* end of threads.c */
@@ -1,5 +1,5 @@
/* 01c-osthreads.c */
-/* Check that threading in the operating system and libraries is ok */
+/* Check that threading in the operating system and libraries work ok */
#include <assert.h> /* assert */
#include <stdio.h> /* perror printf */
@@ -50,7 +50,7 @@ tests1_4sleeps()
HANDLE threadhandle1, threadhandle2;
DWORD threadId1, threadId2;
#else
- pthread_t thread_id;
+ pthread_t thread_id1, thread_id2;
pthread_attr_t thread_attr;
#endif
int threadnumber, threadstacksize;
@@ -69,7 +69,7 @@ tests1_4sleeps()
#else
status = pthread_attr_init(&thread_attr);
pthread_attr_setstacksize(&thread_attr, threadstacksize);
- status = pthread_create(&thread_id, &thread_attr, test1_thread,
+ status = pthread_create(&thread_id1, &thread_attr, test1_thread,
&thread1arguments);
#endif
is_ii(status, 0, "created first thread");
@@ -85,7 +85,7 @@ tests1_4sleeps()
&threadId1);
status = (threadhandle2 == NULL);
#else
- status = pthread_create(&thread_id, &thread_attr, test1_thread,
+ status = pthread_create(&thread_id2, &thread_attr, test1_thread,
&thread2arguments);
#endif
is_ii(status, 0, "created second thread");
@@ -95,6 +95,10 @@ tests1_4sleeps()
/* Give both threads enough time to complete */
sleep(3);
+
+ /* Clean up the thread zombies */
+ pthread_join(thread_id1, NULL);
+ pthread_join(thread_id2, NULL);
}

0 comments on commit 5fa7d52

Please sign in to comment.