469,927 Members | 1,883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

(OpenMP, C++ME, System.Threading.Thread) -> Resource Leak.

Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <omp.h>
  3.  
  4. public __gc class TestClass
  5. {
  6. public:
  7.     void ThreadProc()
  8.     {
  9.         int sum = 0;
  10.  
  11. #pragma omp parallel for reduction(+:sum)
  12.         for ( int i = 0; i < 10000; i++ )
  13.         {
  14.             sum += i;
  15.         }
  16.  
  17.         System::Console::WriteLine( sum.ToString() );
  18.     }
  19. };
  20.  
  21. int main()
  22. {
  23.     for (;;)
  24.     {
  25.         TestClass __gc * o = __gc new TestClass();
  26.         System::Threading::Thread __gc * t = __gc new System::Threading::Thread( 
  27.                     __gc new System::Threading::ThreadStart( o, &TestClass::ThreadProc ) );
  28.  
  29.         t->Start();
  30.  
  31.         System::Threading::Thread::get_CurrentThread()->Sleep( 100 );
  32.     }
  33. }
  34.  
If I put something that is not using Open MP into the ThreadProc no HANDLEs will be leaked.
I realize that what happens is: Omp creates unmanaged threads inside a managed thread (i.e. System::Threading::Thread). I just have very little idea about what could be wrong with it. If somebody will try to reproduce this, I am currently using VS2005.
Thanks for all your comments and suggestions.
Oct 1 '07 #1
2 2220
I think i have got ur problem. there are unmanaged threads when using openmp.is it so?

openmp architecture is like when u specify the openmp pragma, it creates a new thread for ur loop and once finished joins the main thread.this might be ur problem. I am not versatile in openmp. But this is a new concept only less ppl use it.so keep in touch to discuss about openmp
Oct 5 '07 #2
We recently discovered that the thread leak has nothing to do with .NET. The same leak occurs even if we run OpenMP code inside a Win32 thread. So, here is the new example. Suggestions\ideas, please?

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <omp.h>
  4.  
  5. int calculateArithmeticalProgressionSumOMP( int a1, int d, int n )
  6. {
  7.     int sum = 0;
  8.     omp_set_num_threads(3);
  9. #pragma omp parallel 
  10.     {
  11. #pragma omp for reduction (+: sum)
  12.         for( int i = 0; i < n; i++)
  13.         {
  14.             int a = a1 + i * d;
  15.             sum += a;                                                                                                                                                                                      
  16.         }
  17.     }
  18.     return sum;
  19. }
  20.  
  21. DWORD threadFunc(LPVOID pParams)
  22. {
  23.     int sum = calculateArithmeticalProgressionSumOMP(0, 1, 10000);
  24.     printf("%d\n", sum);
  25.     return 0;
  26. }
  27.  
  28. int main()
  29. {
  30.     //create some threads
  31.     for(int i = 0; i < 100; i++)
  32.     {
  33.         HANDLE handle;
  34.         handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )threadFunc, NULL, 0, NULL);
  35.         WaitForSingleObject(handle, INFINITE);
  36.         Sleep(100); //imitating some other work
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
  42.  
Nov 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by Jeff Louie | last post: by
1 post views Thread by Brian C. Barnes | last post: by
5 posts views Thread by Vladimir_petter | last post: by
1 post views Thread by Carl J. Van Arsdall | last post: by
2 posts views Thread by san | last post: by
reply views Thread by BallCOMET | last post: by
16 posts views Thread by Paul Schwann | last post: by
6 posts views Thread by Renato Perini | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.