Threads and Memory Allocation
-
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
-
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
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<
for the<
symbol and>
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 -
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
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:
-
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:
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
-
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
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:
-
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:
-
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
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;
}
-
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;
}
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:
-
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:
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
-
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
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: