// JP opened flex table

Click to See Complete Forum and Search --> : visual c++, threads


ecastil
April 14th, 2004, 05:09 AM
Hello guys,

I have created a thread in which i try to call another function. In the thread i have to wait that the user enter the function. I do this through a semaphore but, when i try to change my private function (that is NULL meanwhile) by the user function, a runtime error happens. It is necessary for me that the thread is created before. here is some of the code:

threadFunction(){
s.wait() ;// wait until the user enter the function
function(parameter); //execution error
...
}

I realize the problem is that is hard to change some inner attribute of the class that defines my thread function and that i use inside the threadFunction, e.g

threadFunction(){
bool1 = true; //private boolean data of my class
s.wait() ;// wait until the user enter the function
bool1=false;
}

The variable boo1 is never changed, why can't i do this?. It is necessary for my that the thread is befere created and change the class attributes inside of my threadFunction.

Andreas Masur
April 14th, 2004, 07:02 AM
[Moved thread]

Andreas Masur
April 14th, 2004, 07:05 AM
Well...sorry...but I honestly do not understand your problem(s). You would need to post some more code and try to elaborate a little bit further. How does the thread function look like? What are you trying to access? How can be the function NULL? What are you passing to the thread?

ecastil
April 14th, 2004, 07:32 AM
this is some code,

WHilo::WHilo(){//the constructor of my class

s = new Semaforo();
hManija = CreateThread(NULL,
0,
funcionHilo, //static function
this,
0,
(LPDWORD) &hId);

WaitForSingleObject(hManija, 2000);

}

this is funcionHilo,

DWORD WINAPI WHilo::funcionHilo(LPVOID parametro){
try{
(reinterpret_cast<WHilo*>(parametro))->proxy();
}catch(...){}
return 0;
}

and, this is proxy

void WHilo::proxy(){

(*s).wait();
Sleep(2000);
funcion(parametro); //here is the runtime error
hCodigoSalida = (DWORD)-1;
}

when the user put the funcion, it is received in other method of this class in which :

funcion /*private function of the class*/ = function; //user function
and i too receive the user function parameter

parametro/*private data of this class*/ = parameter; //user parameter

so, when s (the semaphore) is signaled, it is because the user put your function and proxy can contine your execution, but it does not work. what do i have to do?

Andreas Masur
April 14th, 2004, 09:19 AM
Well...that is still confusing...a couple of points though:

What is the purpose of the 'WaitForSingleObject()' call on the thread handle within the destructor?
What is 's' inside the 'Proxy()' function?
What is 'funcion'?
What is 'parametro' passed to 'funcion()' inside 'Proxy()'?
When does these 'parametro' gets set? What datatype is it?

Please provide the class declaration and definition (you probably can attach them if is bigger). Other than that, please enclose your code with the [code] tags to make it more readable...

ecastil
April 14th, 2004, 12:34 PM
the WaitForSingleObject in the constructor is to give a slice time of the execution to the thread (hManija).

s is a semaphore, when it is signaled it means that the user already entered your function. s is initialize in the constructor and implemented in other file.

funcion is a private attribute of my class. it is a pointer to a function. Before the user enter your function (pointer to a function), the private attribute "funcion" has garbage.

parametro is void*. funcion and parametro are set in a method named activarHilo that looks like

bool WHilo::activarHilo(void* (*funcion) (void*), void* argumento, int prioridad){

this->funcion = funcion;
this->parametro = argumento;
this->prioridadHilo = prioridad;
Sleep(1000); //do nothing
/*signal the semaphore s in order to proxy() continue with its code
because funcion is already set*/
(*s).senalar();
return false;
}

thanks,

CJ1
April 20th, 2004, 04:35 PM
If I read this correctly, the function is static and you have not created an instance of the class.

When executing a static function of a class, you can not call or access any non-static members.

You may need to rethink your approach.

//JP added flex table