I've created an mfc application which at the moment listen to a specific port using csocket and output the data into a text file. This process is run in a separate thread (a worker thread I belive). The program works fine.
To upgrade the program I thought about adding a text field with a cstring object attached to it. My question is then, how can I update the textfield with the data that my listen-thread receive ? Can I post\send the data or a pointer from one thread to another ? What about synchronising ?
Links to related articles or tutorials would be much appreciated.
'GetSafeHwnd()' returns a 'HWND' and not a pointer. You treat the returned handle as a pointer to a handle (by casting it). While dereferencing it in the thread function you of course call for trouble...
Change your structure to accept a HWND...
Code:
struct threadData
{
Driver *d;
HWND hwnd;
};
Then assign it accordingly...
Code:
obj->hwnd = GetSafeHwnd();
The thread function needs to delete the passed data structure before it terminates otherwise you will have memory leaks...
Code:
UINT StartThread( LPVOID pParam )
{
threadData * structData = (threadData*) pParam;
Driver *obj = structData->d;
HWND hwnd = structData->hwnd;
if (obj == 0)
return -1;
int status = obj->start(hwnd);
// Delete passed data
delete structData;
return 0;
}
The thread function needs to delete the passed data structure before it terminates otherwise you will have memory leaks...
that´s a bad policy - better leave the memory management for the threadData completely to the dialog object:
add a threadData member
initialize it inside the onInitDialog() function
pass its address to the thread (no new/delete needed)
otherwise if you happen to pass the same pointer to more than one thread, your solution leads to multiple deletes.
next, to pass the data back to the dialog, you need a common buffer and a synchronization object to serialize access to it (say, 2 more members for the threadData struct, a char[] and a mutex, and have them passed forward to the Driver::start() function as well)
Originally posted by abc_coder
that´s a bad policy - better leave the memory management for the threadData completely to the dialog object:
add a threadData member
initialize it inside the onInitDialog() function
pass its address to the thread (no new/delete needed)
otherwise if you happen to pass the same pointer to more than one thread, your solution leads to multiple deletes.
Well...I do not want to start a big discussion about how to do these things right. Nevertheless your solution lacks from some of the mentioned points as well and requires more handling then you have described.
Since the thread data are a member of your class, you need to make sure that the thread will be terminated before your class will be destroyed. Otherwise your thread will deal with a dangling pointer.
Another point is the number of threads. As long as each thread gets the same data you can simply use one structure. However, as soon as the data needs to vary for each thread you will simply fill up your class with n member variables.
Originally posted by abc_coder
next, to pass the data back to the dialog, you need a common buffer and a synchronization object to serialize access to it (say, 2 more members for the threadData struct, a char[] and a mutex, and have them passed forward to the Driver::start() function as well)
This might be a different approach while dealing with one thread, nevertheless, Windows is an event-driven operating system...so, why not make use of it?
Furthermore, this approach is not practible while dealing with several threads...first of all the processing time would decrease since every thread has to wait until the previous result would have been processed, and adding several kernel objects adds another overhead which is not necessary...
Andreas Masur, thanks for your reply. I've made the updates you adviced. The code now both compiles\links and runs just fine without any error messages.
The problem however is that OnUpdateControl() doesnt seem to be executed when :ostmessage is executed.
Just for testing purpose my OnUpdateControl lools like this :
Did you define all necessary entries in the message map? Maybe you can post the definition of the message and the mapping of the message to the function in the message map.
Other than that, I do not see any reason why it should not work...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.