-
Again on multithread...
Hello, I'm looking to solve this problem.
I'm programming a simple beginning application to learn multithreading under WinXP and using VC++ and MFCs.
My single-threaded app does a timed access to a slow data source, say 1 access every 30 secs. Every time I start the "download" it takes more than 5 seconds to be completed, and in this time the GUI is blocked.
I was thinking to do this work by using a thread for download while the main window continues with its message pump, but I have a problem: the classes/objects that I use to do the download work are created before calling the thread, and not created by the thread, so I can't use those objects from the thread...
How can I do? I read about syncronization mechanisms but I don't know if this is the case: how can I syncronize the access to entire objects? The thread needs to use also the thread calling object...
Thank you for all the hints,
BrownB
-
threads inside process same memory address
synchronization mechanisms
Kuphryn
-
You might consider using AfxBeginThread() and setting a global bool flag which lets you know when you can and cannot touch certain resources. This has worked for me, but perhaps is not the best.
-
The problem is that is not easy understand which kind of resources/data I should check. Actually, all I need to do is to use from the thread some methods of the calling object: I'm passing to the thread the reference to the calling object (the "this" pointer) but from the thread the membres and the methods thet I see are "wrong"...
Note that the thread main function is a static member of the calling object.
-
pointers not references
post couple lines api create thread and callback function
Kuphryn
-
I'm not sure what you mean. The thread is going to be static, yes, but when pass the "this" pointer, you can access the current class that called the thread.
Also, when you say "wrong" I am not sure what you mean... Generally, unless this is for learning, if you are unsure what is going to be accessed or changed, you need to know that before you have a thread running.
-
Actually I found some documentaion that says that a static method of a class can access only to static members of that class, and I tried to access to other non-static methods and members from a static method which was the thread function....
Now, I implemented a static function as thread, and declared it as friend of the class that I want to use.