Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threads and Memory Allocation

Threads and Memory Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
c++toolsperformancehelpquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylno
    wrote on last edited by
    #1

    Hello, This is my first attempt at using threads so go easy. Could some one look at the code below and tell me if I'm going to run into trouble if I keep calling this function i.e. loss of memory. The reason why I'm trying to use a Thread is this: we are writing .dll files to add functionality to a scripting language which is used in a software. The problem is that when a c++ function is called from a .dll file from within this scripting language the next line of code (in the script) doesn't get called until the .dll's function has returned. I was thinking that I could use a thread to get around this problem. Does this make sense? or is there another way of doing it? Thanks

    #include #include using namespace std;

    typedef struct PulseData {
    int iData1;
    int iData2;
    } DATA, *PDATA;

    DWORD WINAPI TestFunction(LPVOID lpParam){
    PDATA pPassedData = (PDATA)lpParam;
    Sleep(1);
    cout << pPassedData->iData1 << endl;
    cout << pPassedData->iData2 << endl;
    Sleep(1);
    return 0;
    }

    int main(){
    PDATA pPulseData[1];
    DWORD dwThread[1];
    HANDLE hThread[1];
    // Allocate memory for thread
    pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

    pPulseData\[0\]->iData1 = 100;
    pPulseData\[0\]->iData2 = 200;
    
    cout << "creating thread" << endl;
    hThread\[0\] = CreateThread(NULL, 0,TestFunction, pPulseData, 0, &dwThread\[0\]);
    cout << "thread finished" << endl;
    
    system("PAUSE");
    return 0;
    

    }

    p.s. the includes are: #include #include I don't know why they don't show up in the code

    P M Y 3 Replies Last reply
    0
    • Y Ylno

      Hello, This is my first attempt at using threads so go easy. Could some one look at the code below and tell me if I'm going to run into trouble if I keep calling this function i.e. loss of memory. The reason why I'm trying to use a Thread is this: we are writing .dll files to add functionality to a scripting language which is used in a software. The problem is that when a c++ function is called from a .dll file from within this scripting language the next line of code (in the script) doesn't get called until the .dll's function has returned. I was thinking that I could use a thread to get around this problem. Does this make sense? or is there another way of doing it? Thanks

      #include #include using namespace std;

      typedef struct PulseData {
      int iData1;
      int iData2;
      } DATA, *PDATA;

      DWORD WINAPI TestFunction(LPVOID lpParam){
      PDATA pPassedData = (PDATA)lpParam;
      Sleep(1);
      cout << pPassedData->iData1 << endl;
      cout << pPassedData->iData2 << endl;
      Sleep(1);
      return 0;
      }

      int main(){
      PDATA pPulseData[1];
      DWORD dwThread[1];
      HANDLE hThread[1];
      // Allocate memory for thread
      pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

      pPulseData\[0\]->iData1 = 100;
      pPulseData\[0\]->iData2 = 200;
      
      cout << "creating thread" << endl;
      hThread\[0\] = CreateThread(NULL, 0,TestFunction, pPulseData, 0, &dwThread\[0\]);
      cout << "thread finished" << endl;
      
      system("PAUSE");
      return 0;
      

      }

      p.s. the includes are: #include #include I don't know why they don't show up in the code

      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #2

      Ylno wrote:

      p.s. the includes are: #include #include I don't know why they don't show up in the code

      I assume you want the < and > characters around your include files. They don't show up because putting the characters in explicitly makes the browser treat them as an HTML tag. Use &lt; for the < symbol and &gt; for the > symbol. Regards, --Perspx

      "I've got my kids brainwashed: You don't use Google, and you don't use an iPod." - Steve Ballmer
      "Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen an angry penguin charging at them in excess of 100mph." - Linus Torvalds

      1 Reply Last reply
      0
      • Y Ylno

        Hello, This is my first attempt at using threads so go easy. Could some one look at the code below and tell me if I'm going to run into trouble if I keep calling this function i.e. loss of memory. The reason why I'm trying to use a Thread is this: we are writing .dll files to add functionality to a scripting language which is used in a software. The problem is that when a c++ function is called from a .dll file from within this scripting language the next line of code (in the script) doesn't get called until the .dll's function has returned. I was thinking that I could use a thread to get around this problem. Does this make sense? or is there another way of doing it? Thanks

        #include #include using namespace std;

        typedef struct PulseData {
        int iData1;
        int iData2;
        } DATA, *PDATA;

        DWORD WINAPI TestFunction(LPVOID lpParam){
        PDATA pPassedData = (PDATA)lpParam;
        Sleep(1);
        cout << pPassedData->iData1 << endl;
        cout << pPassedData->iData2 << endl;
        Sleep(1);
        return 0;
        }

        int main(){
        PDATA pPulseData[1];
        DWORD dwThread[1];
        HANDLE hThread[1];
        // Allocate memory for thread
        pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

        pPulseData\[0\]->iData1 = 100;
        pPulseData\[0\]->iData2 = 200;
        
        cout << "creating thread" << endl;
        hThread\[0\] = CreateThread(NULL, 0,TestFunction, pPulseData, 0, &dwThread\[0\]);
        cout << "thread finished" << endl;
        
        system("PAUSE");
        return 0;
        

        }

        p.s. the includes are: #include #include I don't know why they don't show up in the code

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Ylno wrote:

        pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

        Why HeapAlloc and not "new"?

        Ylno wrote:

        cout << "thread finished" << endl;

        Wrong! You started a thread but when that line is reached you have no idea if the thread has ended yet or not. Besides the memory leak, I don't see any problems in the simple code shown. You should assume all threads run in parallel - at the same time. You can make no assumptions about when a thread ends relative to another thread. It is up to you to synchronize threads where necessary and also synchronize access to data used by multiple concurrently running threads. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        Y 1 Reply Last reply
        0
        • M Mark Salsbery

          Ylno wrote:

          pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

          Why HeapAlloc and not "new"?

          Ylno wrote:

          cout << "thread finished" << endl;

          Wrong! You started a thread but when that line is reached you have no idea if the thread has ended yet or not. Besides the memory leak, I don't see any problems in the simple code shown. You should assume all threads run in parallel - at the same time. You can make no assumptions about when a thread ends relative to another thread. It is up to you to synchronize threads where necessary and also synchronize access to data used by multiple concurrently running threads. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          Y Offline
          Y Offline
          Ylno
          wrote on last edited by
          #4

          Hi, Sorry. I was just writing "thread finished" as a test in the program. I should have written "thread called" or something similar. I just wanted to see if the thread would work independantly of the main program/thread. You say that you don't see anything wrong with the code. Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory? Also you said << Why HeapAlloc and not "new"? >>. Why do you mean? thanks for your reply, Y

          M 1 Reply Last reply
          0
          • Y Ylno

            Hi, Sorry. I was just writing "thread finished" as a test in the program. I should have written "thread called" or something similar. I just wanted to see if the thread would work independantly of the main program/thread. You say that you don't see anything wrong with the code. Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory? Also you said << Why HeapAlloc and not "new"? >>. Why do you mean? thanks for your reply, Y

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Ylno wrote:

            Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory?

            No, I said there's a memory leak. I don't see any HeapFree() call releasing the memory.

            Ylno wrote:

            you said << Why HeapAlloc and not "new"? >>. Why do you mean?

            I'm just curious why you're directly calling Win32 memory management functions instead of using the C++ "new" operator. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            Y 1 Reply Last reply
            0
            • M Mark Salsbery

              Ylno wrote:

              Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory?

              No, I said there's a memory leak. I don't see any HeapFree() call releasing the memory.

              Ylno wrote:

              you said << Why HeapAlloc and not "new"? >>. Why do you mean?

              I'm just curious why you're directly calling Win32 memory management functions instead of using the C++ "new" operator. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              Y Offline
              Y Offline
              Ylno
              wrote on last edited by
              #6

              Hi Mark, I will put a heap free call in. Thanks.

              1 Reply Last reply
              0
              • Y Ylno

                Hello, This is my first attempt at using threads so go easy. Could some one look at the code below and tell me if I'm going to run into trouble if I keep calling this function i.e. loss of memory. The reason why I'm trying to use a Thread is this: we are writing .dll files to add functionality to a scripting language which is used in a software. The problem is that when a c++ function is called from a .dll file from within this scripting language the next line of code (in the script) doesn't get called until the .dll's function has returned. I was thinking that I could use a thread to get around this problem. Does this make sense? or is there another way of doing it? Thanks

                #include #include using namespace std;

                typedef struct PulseData {
                int iData1;
                int iData2;
                } DATA, *PDATA;

                DWORD WINAPI TestFunction(LPVOID lpParam){
                PDATA pPassedData = (PDATA)lpParam;
                Sleep(1);
                cout << pPassedData->iData1 << endl;
                cout << pPassedData->iData2 << endl;
                Sleep(1);
                return 0;
                }

                int main(){
                PDATA pPulseData[1];
                DWORD dwThread[1];
                HANDLE hThread[1];
                // Allocate memory for thread
                pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));

                pPulseData\[0\]->iData1 = 100;
                pPulseData\[0\]->iData2 = 200;
                
                cout << "creating thread" << endl;
                hThread\[0\] = CreateThread(NULL, 0,TestFunction, pPulseData, 0, &dwThread\[0\]);
                cout << "thread finished" << endl;
                
                system("PAUSE");
                return 0;
                

                }

                p.s. the includes are: #include #include I don't know why they don't show up in the code

                Y Offline
                Y Offline
                Ylno
                wrote on last edited by
                #7

                Ok, I've implemented a heap clean up at the end of the code. Problem now is that when i call this function it waits untill the thread finishes until returning. Can anyone see a way of perhaps having the code below return the address of the Thread and the pPulseData and have another code clean the memory for these two addresses. Thanks

                float LPTPulseThreaded(float aPortAddress, float aValue1, float aValue2, float aTime1, float aTime2, float aTimes, float aControlAddress, float aOffOnValue){
                PDATA pPulseData[1]; // Pointer to the funtion
                DWORD dwThread[1];
                HANDLE hThread[1];
                // Allocate memory for thread
                pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));
                // Populate the data struct
                pPulseData[0]->PortAddress = aPortAddress;
                pPulseData[0]->Value1 = aValue1;
                pPulseData[0]->Value2 = aValue2;
                pPulseData[0]->Time1 = aTime1;
                pPulseData[0]->Time2 = aTime2;
                pPulseData[0]->Times = aTimes;
                pPulseData[0]->ControlAddress = aControlAddress;
                pPulseData[0]->OffOnValue = aOffOnValue;
                // Create the thread
                hThread[0] = CreateThread(NULL, 0,PulseThread, pPulseData, 0, &dwThread[0]);
                // Check to see if thread creation failed
                if(hThread[0] == NULL){return 1;}
                // Wait untill all threads have finished
                WaitForMultipleObjects(1, hThread, TRUE, 10000);
                // Free up the memory
                HeapFree(GetProcessHeap(), 0, pPulseData[0]);
                pPulseData[0] = NULL;

                return 0;
                

                }

                M 1 Reply Last reply
                0
                • Y Ylno

                  Ok, I've implemented a heap clean up at the end of the code. Problem now is that when i call this function it waits untill the thread finishes until returning. Can anyone see a way of perhaps having the code below return the address of the Thread and the pPulseData and have another code clean the memory for these two addresses. Thanks

                  float LPTPulseThreaded(float aPortAddress, float aValue1, float aValue2, float aTime1, float aTime2, float aTimes, float aControlAddress, float aOffOnValue){
                  PDATA pPulseData[1]; // Pointer to the funtion
                  DWORD dwThread[1];
                  HANDLE hThread[1];
                  // Allocate memory for thread
                  pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));
                  // Populate the data struct
                  pPulseData[0]->PortAddress = aPortAddress;
                  pPulseData[0]->Value1 = aValue1;
                  pPulseData[0]->Value2 = aValue2;
                  pPulseData[0]->Time1 = aTime1;
                  pPulseData[0]->Time2 = aTime2;
                  pPulseData[0]->Times = aTimes;
                  pPulseData[0]->ControlAddress = aControlAddress;
                  pPulseData[0]->OffOnValue = aOffOnValue;
                  // Create the thread
                  hThread[0] = CreateThread(NULL, 0,PulseThread, pPulseData, 0, &dwThread[0]);
                  // Check to see if thread creation failed
                  if(hThread[0] == NULL){return 1;}
                  // Wait untill all threads have finished
                  WaitForMultipleObjects(1, hThread, TRUE, 10000);
                  // Free up the memory
                  HeapFree(GetProcessHeap(), 0, pPulseData[0]);
                  pPulseData[0] = NULL;

                  return 0;
                  

                  }

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Ylno wrote:

                  when i call this function it waits untill the thread finishes until returning.

                  Then there's no reason to use a separate thread. You've just functionally created an overly complicated single thread. Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  Y 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Ylno wrote:

                    when i call this function it waits untill the thread finishes until returning.

                    Then there's no reason to use a separate thread. You've just functionally created an overly complicated single thread. Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    Y Offline
                    Y Offline
                    Ylno
                    wrote on last edited by
                    #9

                    Mark, I know i've created a useless piece of code. Please give me a hint about how to make this piece of code fit what i'm trying to do... How do I make sure the heap is freed up and let this function return so that the scripting language can continue its excecution. En espérant

                    M 1 Reply Last reply
                    0
                    • Y Ylno

                      Mark, I know i've created a useless piece of code. Please give me a hint about how to make this piece of code fit what i'm trying to do... How do I make sure the heap is freed up and let this function return so that the scripting language can continue its excecution. En espérant

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      Assuming the DLL and the scripting language need no further interaction after the call...

                      DLLFunctionCalledByScriptingLanguage
                      {
                      createthread(ThreadProc)
                      return
                      }

                      ThreadProc
                      {
                      allocate some memory
                      do some stuff
                      free memory
                      return
                      }

                      That's the same as yours except the memory operations are all in the thread procedure and the function doesn't wait for the thread to end before returning control to the caller, thus making it truly multithreaded. Make sense? How often and how many times would this be called by the scripting language? Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups