keep a thread alive in a dll?
Hi,
I am developing a dll in C and I have one problem that I don't know how to solve...
An external application invokes my dll by calling the function:
int calculateParameters(int, int)
I need to return to the application the value "OK" indicating that the passed parameters are correct as soon as possible, and then do the needed calculations and pass the result of the calculations to the external application using a callback function.
How can I do so that after the function returns "OK", the previously created thread could continue alive and do the appropiate calculations and invoke the callback funtion?
Here is the simplified code of this dll:
// Function invoked from the external application
int calculateParameters(int a, int b)
{
_beginthread(sumParameters, 0, valorTmp); //Create a thread to calculate
if (a > b) return OK; //As soon as this function returns, the thread previously created is stopped and no calculation is done
}
//Function needed to perform the internal calculations
void sumParameters(void *valorTmp)
{
z = a + b; //Example of calculations
Callback(y); //Pass the result of the calculations to the external application
}
Thank you very much for your help
[1320 byte] By [
djodra] at [2007-11-19 1:36:50]

# 1 Re: keep a thread alive in a dll?
Here is the simplified code of this dll:
// Function invoked from the external application
int calculateParameters(int a, int b)
{
_beginthread(sumParameters, 0, valorTmp); //Create a thread to calculate
if (a > b) return OK; //As soon as this function returns, the thread previously created is stopped and no calculation is done
}
How do you know that thread is is stopped? Are you sure it starts?
Probably, the thread starts and finishes before OS gives new time slice to the thread where your DLL function is running.
Did you try to set a breakpoint somewhere in sumParameters function to be sure that thread starts?
I am just trying to tell that the code shown here has some synchronization problems.
If you need this thread only for making calculations and return result via callback I would re-write this code in this way (I show only the idea):
1. Define in DLL a function Init that will create the thread.
2. Thread function will have a message loop like this:
while (GetMessage (...) > 0)
{
if msg.message == MAKE_CALCULATION)
{
z = a + b; //Example of calculations
Callback(y);
}
}
3. int calculateParameters(int a, int b)
{
if (a > b)
{
PostThreadMessage (threadID, MAKE_CALCULATION,...) ;
return OK; //As soon as this function returns, the thread previously created
is stopped and no calculation is done
}
else return ERROR;
}
Thus, you won't need to start thread each time you need to make calculations.
sbubis at 2007-11-9 13:56:43 >
