Click to See Complete Forum and Search --> : Some Thread Question


ideru
February 21st, 2006, 07:59 PM
hello , this post of mine is related to my post (http://www.codeguru.com/forum/showthread.php?t=376738) here
But the next question is related to thread, so am continuing the question here :o

I have a dialog application, OnInitDialog I started my thread, in this thread processing I have a call back function that would change a value of the variable "myVar". After the thread starter function was called I check the value of "myVar" and it was already change. But when the function went back to OnInitDialog to execute the next lines of code, I check the value of "myVar" again, but it was still on the original value.

How can I synchronize the value of "myVar" in between my thread and the GUI main thread?? :confused:

// this is making me daft:(

kirants
February 21st, 2006, 08:49 PM
Please post code for launching the thread, checking value etc..

ideru
February 21st, 2006, 09:00 PM
this is how I created the thread


OnitIniDialog
{
// remove other lines
InitConnect()
}

InitConnect()
{
mySocketClass.InitConnection()
}


On mySocketClass Class

InitConnection()
{
// do some socket initialization

//create Listener Thread
ListnerParam* pLp = new ListnerParam;
pLp->funcCallBack = fCallBack;
pLp->hListener = s;

m_hListener = CreateThread(
NULL,
0,
&ListnerProc,
(LPVOID)pLp,
0,
&m_dwListnerThreadId);
if( m_hListener == NULL )
return false;
}


the ListnerProc function, basically waits for any connection and use the callback function if accept connection.

Arjay
February 21st, 2006, 10:12 PM
An immediate problem is that when you declare


ListnerParam* pLp = new ListnerParam;


the pLp param is going to go out of scope which means that you won't be able to access any of its params after the InitConnection function returns. You are also going to have a memory leak.

This might not be your trouble per se, but without more code, it's hard to diagnose.

Can you post a more complete code example?

Arjay

ideru
February 21st, 2006, 11:11 PM
Hello Arjay, basically thats the whole code as it is
I just removed the socket initialization part

ListenerParam is a structure by the way declare this way


struct ListnerParam
{
SOCKET hListener;
void (WINAPI *funcCallBack)(SOCKET hAccept);
};


Can you please tell me how does pLp goes out of scope?? :confused:

leojose
February 22nd, 2006, 12:30 AM
Can you please tell me how does pLp goes out of scope?? :confused:
The object pLp that you created will only be valid as long as your function does not return. When it does, pLp goes out of scope and you lose all memory references or values stored in the member funtions...
But in your case, you are creating a thread which on the other hand does not terminate even after your function has returned. so, to make sure that your variables (like myVar) does not lose its value, you may have to define it as a global variable.

ideru
February 22nd, 2006, 12:56 AM
myVar is a member of my class in Application "B". :o


the flow goes like this

1. OnInitDialog -> Initial Connection
2. InitialConnection -> Start Thread
3. Thread Started -> wait for incoming connection
---
4. Connection accepted -> call CallBack function
5. CallBackFunction -> ::SendMessage
6. OnMessge -> do some checking
7. Checking OK -> myVar++

4-7 works fine if "B" is run first or if run from "A" under normal condition, actually on this time, there is no need to check the value of myVar . But I think the behavior of the normal and abnormal with concern to myVar variable is the same.

if under abnormal condition, I need to check the value of myVar which was not yet updated on the UI Thread, but already updated on my callBack function.

I tried using the PostMessage as suggested here (http://www.codeguru.com/forum/showthread.php?t=375357)but the result is still the same :confused:

kirants
February 22nd, 2006, 12:28 PM
An immediate problem is that when you declare


ListnerParam* pLp = new ListnerParam;


the pLp param is going to go out of scope which means that you won't be able to access any of its params after the InitConnection function returns.
That is a problem only if it is required for the caller of the InitConnection to know what the ListenerParam was passed. If it is not a requirement, I don't see a problem with scope here.

kirants
February 22nd, 2006, 12:31 PM
The object pLp that you created will only be valid as long as your function does not return.
That is not the case here since pLP is a heap object and not a stack object.

To ideru,
it seems that everyone here is finding it hard to understand the problem. Initiallly, it seemed to be a problem with threads within a process. But now, it seems that there is another process involved.. but again, your flow steps doesn't make any mention of what process context those individual steps occur in..

Arjay
February 22nd, 2006, 01:09 PM
That is a problem only if it is required for the caller of the InitConnection to know what the ListenerParam was passed. If it is not a requirement, I don't see a problem with scope here.It's a problem because you can't free up the memory allocated with new.

Arjay

Arjay
February 22nd, 2006, 01:12 PM
Hello Arjay, basically thats the whole code as it is
I just removed the socket initialization part

We are still missing some code. You mention the variable (myVar) but didn't include the code that accesses it. Is this variable global? Are you synchronizing its access?

Arjay

kirants
February 22nd, 2006, 01:15 PM
It's a problem because you can't free up the memory allocated with new.
Arjay
The thread procedure can free it, by dynamic_downcast-ing it, since that is what is being passed as param to the thread

Arjay
February 22nd, 2006, 04:09 PM
The thread procedure can free it, by dynamic_downcast-ing it, since that is what is being passed as param to the threadYeah, you are right. Of course, it's not always the best practice to allocate in one thread and delete in another. If op would change to storing the struct as a member of the socket class and pass the this pointer to the thread in the InitConnection method, memory allocation/deletion wouldn't ever be an issue.

Arjay

MikeAThon
February 22nd, 2006, 08:11 PM
I also do not see a scope problem with pLp, although its memory indeed needs to be deleted (whihc you might do in the thread, but you need to show us thread code in ListenerProc).

But your original problem related to the value of variable myVar. And you just stated the following:
myVar is a member of my class in Application "B".
Is the variable in a different process, or is it in a different thread of one single process? Memory is shared by different threads for the same process, but since each individual process gets its own 4 gig virtual memory, distinct from all other processes, memory is not shared between different processes.

Tell us more about your architecture. Please show us the code for the thread function, as well as the code (in OnInitDialog, I think??) where you think that myVar should be changing but it's not.

Mike

ideru
February 22nd, 2006, 10:48 PM
sorry about the vagueness, and thanks for the replies

here is the code


//variables

int myVar;
MyNetSocket mySocket;

OnInitDialog
{
myVar = 0;

InitConnect(); // initialize socket and start thread for accepting connection, see my previous post

CheckConnection(); // here the myVar Value is
}

void myDialog::InitConnect()
{
mySocket.InitConnection(&fCallBack, port);
}

void myDialog::CheckConnection()
{
// do some processing here

if( myVar > 0 )
{
// do this
}
else if( myVar == 0 )
{
//process here <- enters here
}
}

void WINAPI MyDialog::fCallBack(parameter)
{
// SendMessage Here ID: WM_USER+10
}

//this is the MessageMapping for WM_USER+10
LRESULT myDialog::processConnect(parameter)
{
// check if parameter is valid
if ( valid )
myVar++;
}

----------------------------
void MyNetSocket::InitConnection(void (WINAPI *fCallBack)(SOCKET hAccept),
int port);
{
// do some initialize here

// create Listener thread
ListnerParam* pLp = new ListnerParam;
pLp->funcCallBack = fCallBack;
pLp->hListener = s;


m_hListener = CreateThread(
NULL,
0,
&ListnerProc,
(LPVOID)pLp,
0,
&m_dwListnerThreadId);
if( m_hListener == NULL )
return false;

return true;
}

ListnerProc(LPVOID lpParameter)
{
ListnerParam* pLp = ( ListnerParam * )lpParameter;
while( 1 )
{
// get accept here

pLp->fCallBack(socket);
}
}



sorry about the code, that's basically the flow of it, where/how myVar is incremented and where it is check

kirants
February 23rd, 2006, 12:42 PM
After the thread starter function was called I check the value of "myVar"Are you referring to the CheckConnection function call in OnInitDialog() ?

and it was already change.
I see myVar being set to 0 in OnInitDialog. So, when you say it already change, what do you mean ? It changed from 0 to something else ?
Also, from the code, only place myVar is set is in OnInitDialog and processConnect. So, only way it can change is if the callback came in. Question is, did you get the callback before the CheckConnection was called ?

But when the function went back to OnInitDialog to execute the next lines of code, I check the value of "myVar" again, but it was still on the original value.
What original value ? 0 ?

ideru
February 24th, 2006, 04:48 AM
Are you referring to the CheckConnection function call in OnInitDialog() ?


yes, the myVar value is check there


I see myVar being set to 0 in OnInitDialog. So, when you say it already change, what do you mean ? It changed from 0 to something else ?
Also, from the code, only place myVar is set is in OnInitDialog and processConnect. So, only way it can change is if the callback came in. Question is, did you get the callback before the CheckConnection was called ?


that is what am wonderin' how are the message process at kind of situation. And is the thread already started at this time even though OnInitDialog is not yet finished? what is the timing if a connection arrives then callback function is called but OnInitDialog is not yet finished?



What original value ? 0 ?
yes :o

actually what I did was ( because I can't seem to understand this situation), I checked for the client Mutex name :o