Click to See Complete Forum and Search --> : Help with threads
JernauGurgeh
August 30th, 2005, 02:58 AM
I am writing a program that has a few very processor intensive functions and I want to be able to run these in a thread, so that the main program window is not blocked while the threads are underway, and also if possible to have a progress bar indicating the progress of the thread (I will probably do with a global variable) however most of the tutorials I can find on multithreading a rea bit vague for me. For example they simulate some computation by just having a couple of nested loops, which do nothing, but don't really explain how to do anything in the thread. For example, I have a class which constructs a grid of points, where each point has some properties. the properties for each point are produced by copying a template with the basic properties that all the points have, and then modifying it a bit. the function to do this looks like
CLayerProperties* intermediate = NULL;
bool buildGrid(theLayerProperties,theGridOfPoints)
{
for (int i=0;i<theGridOfPoints->getMaxX;i++)
{
//add code alter some variable to indicate progress
//here so it uses less processor time than in the j loop.
for (int j=0;j<theGridOfPoints->getMaxY;j++)
{
intermediate = theLayerProperties->replicate();
theGridOfPoints->insert(intermediate,i,j);
}
}
}
where theLayerProperties,theGridOfPoints are pointers to these objects. but I am not sure how to put that into a worker thread :/
even any good tutorials on the matter would be very helpful, thanks.
golanshahar
August 30th, 2005, 03:11 AM
this is how you make worker thread:
How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?t=312452)
and if you want to put the posted code in the thread so simply put that in the thread callback.
hope it helps
Cheers
JernauGurgeh
August 30th, 2005, 03:35 AM
that's just the problem, I don't really understand that :/
Ejaz
August 30th, 2005, 04:07 AM
[ Moved Thread ]
Ejaz
August 30th, 2005, 04:12 AM
Some tutorials from MSDN are as follows.
About Processes and Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/about_processes_and_threads.asp)
Multiple Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/multiple_threads.asp)
Using Processes and Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_processes_and_threads.asp)
Process and Thread Reference (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/process_and_thread_reference.asp)
JernauGurgeh
August 30th, 2005, 04:35 AM
thanks for moving this to the right forum, but I created this post because I just can't get into the various tutorials, since they don't really seem that clear to me.
ok, after a bit of work I think I might be getting somewhere. here is my original function:
bool BuildGrid::buildUniform(Layertemplate *theLayers)
{
Layertemplate* copyLayer;
for (int i=0;i<myXsize;i++)
{
for (int j=0;j<myYsize;j++)
{
copyLayer = theLayers->replicate();
EnterGridPoint(copyLayer,i);
}
}
return true;
}
and I want all that basically in a thread,
so from what I understand, I declare these in my BuildGrid class:
// thread for construction
CWinThread *m_pThread;
static UINT THREAD_buildGrid(LPVOID pvParam);
and make a function for creating the thread
bool createthread()
{
m_pThread = AfxBeginThread(THREAD_buildGrid,this);
if (!m_pThread)
{
CProgram_Log::getInstance()->report("Error Building Grid","Error Starting Grid Build Thread",__FILE__,__LINE__);
return false;
}
else
return true;
}
and then I have this:
UINT BuildGrid::THREAD_buildGrid(LPVOID pvParam)
{
Layertemplate* copyLayer;
BuildGrid* me = (BuildGrid*)pvParam;
/*PROBLEM*/ return me->buildUniform(theLayers);
}
which is basically the thread. but I still have the problem on the line indicated, since I don't know how to insert "theLayers" into the thread, like I did in the original function.
Ejaz
August 30th, 2005, 04:57 AM
One way of doing that is like, you create a structure, that contains 2 pointers (
theLayers & BuildGrid* ), create an object of it, fill them with proper address, pass it in the pvParam (where you are passing *this* right now).
Or, if your BuildGrid already has *theLayers*, then you can get that from me (BuildGrild* me).
Vanaj
August 30th, 2005, 11:18 AM
If you goto http://www.flounder.com/workerthreads.htm, a website by Dr. Joseph E. Newcomer MVP, you will find some very good info with sample code on worker threads along with other valuble info. Check out the "MVP Tips, Techniques, and Goodies"
Andreas Masur
August 30th, 2005, 02:59 PM
Take a look at the following FAQs (http://www.codeguru.com/forum/showthread.php?p=1201802#thread)...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.