|
What happens if you remove the ::Sleep(0); call from your thread proc (don't let the threads give
up any of their time slice)?
You're still not going to get 4 times as fast since you have device contention at the display
device.
I wouldn't think it would be slower though
|
|
|
|
|
Hi,
Sleep(0) is supposed to free the thread's quantum allocated by the system.
I removed it but it is still slower as if it would run on 1 CPU which is paradoxal.
There is no spoon.
|
|
|
|
|
bouli wrote: Sleep(0) is supposed to free the thread's quantum allocated by the system.
Right - I was wondering what would happen if you let the threads use their entire time-slice;
I don't know what goes on with processor cache and bus and RAM contention but just at the
SetPixel() call alone, only one thread will be able to write a pixel at a time unless your video
device can parallel process the calls.
I don't think it's paradoxal. I think the fastest performance you could get would be to have one
thread that does the IO (output a pixel) and 3 threads calculating the random coordinates and
feeding them to the pixel-outputter-thread. Just a guess, I could be way off
When you run it what does performance monitor show for CPU usage?
|
|
|
|
|
Example fromm Google search ("multicore percent faster"):
From Performance Analysis and Multicore Processors[^]...
"One of the side effects of multicore technology is that CPU-bound applications can
potentially run twice as fast on dual-core processors, whereas memory-bound applications may only
run 50 percent faster. I/O-bound applications may not run any faster"
|
|
|
|
|
PROBLEM DESCRIPTION
I have a CFormView based SDI application. There is a button and a Editbox (m_Progress , a control category CEdit variable, indicating program progress) on the form. A lengthy task will be started by clicking the button. Usually the task will take several hours. While the process is running, users may use other applications, such as WORD, EXCEL,etc,. When users get back to the process to see its progress. The window cannot be maxmized. To solve this problem, I read some articles. A lot of articles suggest that thread should be used.
So, i decide to use a worker thread to solve this problem. To this end, I move the code in the original button-cick handler to a function (SubFunc())of a class (MyClass). In the new button-click handler I first creat an instance of MyClass. Then, pass the instance to AfxBeginThread() to create a worker thread. The above steps are implented as follows.
UINT MyThreadProc(LPVOID pParam)
{
MyClass* pObject = (MyClass*)pParam;
if (pObject == NULL)
{
return -1;
}
else
int test = pObject->SubFunc();
return 0;
}
}
void CHhhhView::OnButton1()
{
MyClass *objMyClass = new MyClass;
AfxBeginThread(MyThreadProc, objMyClass);
}
In SubFunc(), I use following statements to send the string "9999" to m_Progress. Specially, ((CMDIFrameWnd*)AfxGetMainWnd()->GetActiveWindow())->GetActiveDocument() is used.
CDocument* pp = ((CMDIFrameWnd*)AfxGetMainWnd()->GetActiveWindow())->GetActiveDocument();
POSITION vv = pp->GetFirstViewPosition();
POSITION pos = pp->GetFirstViewPosition();
if (pos != NULL)
{
CHhhhView* pView = (CHhhhView*)pp->GetNextView(pos);
if(pView!=NULL)
{
pView->m_Progress.SetWindowText("9999");
pView->UpdateWindow();
}
}
However, ((CMDIFrameWnd*)AfxGetMainWnd()->GetActiveWindow())->GetActiveDocument() works
only when objMyClass is NOT called
in a worker thread.
That is, objMyClass is called in the following way,
void CHhhhView::OnButton1()
{
MyClass mc;
mc.SubAdd();
int itmp = mc.m_itmp;
}
I noticed an artical
http://www.codecomments.com/archive372-2006-4-890607.html[^], which highly associated with my problem. However, I still could not figure out the solution, as I am a newbie to MFC.
* Am I in the right direction to solve the problem correctly caused by lengthy task by using a worker thread?
* What is a good way to wire the CEdit control and the varible updated in the SubFunc() running in a worker thread.
Please help me. This problem has stumped me for serveral days .
-- modified at 17:25 Saturday 4th November, 2006
|
|
|
|
|
GetActiveWindow() is thread-specific. You have no active windows in your worker thread.
Why not add a CHhhhView* member to your MyClass class (pass it in in the constructor):
class MyClass
{
CHhhhView *pView;
public:
MyClass(CHhhhView *view) {pView = view;}
};
void CHhhhView::OnButton1()
{
MyClass *objMyClass = new MyClass(this);
AfxBeginThread(MyThreadProc, objMyClass);
}
Now you can use pView in MyClass instead of ((CMDIFrameWnd*)AfxGetMainWnd()->GetActiveWindow()).
|
|
|
|
|
:-DThank you Mark very much for your reply. It works for me.
|
|
|
|
|
One more question about this topic.
Where and how should the pointer 'objMyClass' be deleted to prevent memory leak. I think this should be done at the end of SubFunc(). However, I am not very sure.
|
|
|
|
|
cy163@hotmail.com wrote: Where and how should the pointer 'objMyClass' be deleted to prevent memory leak.
Any time after the pObject->SubFunc() call (obviously)
There's alot of people who object to deleting objects in a different thread than the thread the
object was created in. If you are using new/delete this is perfectly legal to do, so IMO it
comes down to preference.
If it's clear in the code that you create an object (using new) and send it off to a worker
thread to be processed, and that the worker thread will delete it when it's finished, then delete
it in the thread proc when you're finished with it.
If you want to delete it in the same thread it was created in (the UI thread in your example)
then you could use PostThreadMessage() from the worker thread to send the pointer back to the UI
thread for deletion when it's finished with it. The worker thread would need to know which thread
it came from to know where to post the message to.
cy163@hotmail.com wrote: I think this should be done at the end of SubFunc(). However, I am not very sure.
I personally wouldn't do it IN the SubFunc() function. That would be, IMO, too buried to be clear
in the code what's going on. Right after the pObject->SubFunc() call a "delete pObject;" would
be fine.
I would just make sure to document it well in the code with comments so when you possibly enhance
the code later you know exactly where objects are bing deleted, something like this:
void CHhhhView::OnButton1()
{
MyClass *objMyClass = new MyClass;
AfxBeginThread(MyThreadProc, objMyClass);
}
Whatever it takes to make sure you always know that you shouldn't use objMyClass later because
at any time the worker thread will delete it
|
|
|
|
|
Thank you Mark for your clear explanation.
One more further question, also about mem leak. i have some dynamically allocated variables (using malloc(), new) in SunFunc(). I wonder if those allocated mem can be deallocated automatically after the thread finished successfully. Or, i have to take care of the memory releasing things myself.
Moreover, could you please talk about how to terminate the thread before SubFunc() reaches its end in my example. I refer to some articles. They suggested to use CEvent and WaitForSingleObject(), as well as AfxEndThread().
I do not how to combine them to terminates a thread from another thread (main thread, in my case).
Thank you for your valuable time
-- modified at 6:10 Monday 6th November, 2006
|
|
|
|
|
Hi,
I want to create a child window with menu like in a SDI application from a dialog based application. How to create it?
Cyber Friend
|
|
|
|
|
Is it possible to reallocate using operator new?
// C
some_ptr = malloc( sizeof( some_type ) * n );
... // change "n"
some_ptr = realloc( sizeof( some_type ) * n );
// C++
some_ptr = new some_type[n];
... // change "n"
// Now what?
Best regards
hint_54
|
|
|
|
|
The short answer is no. Either use a combination of malloc and realloc as in your example, or use the keyword new and copy the contents over to a larger buffer.
|
|
|
|
|
|
Use an STL collection such as vector , and you won't have to worry about the memory allocation.
|
|
|
|
|
Hi
I need a program that convert date to shamsi
thank you
|
|
|
|
|
|
is it possible to get file size if it is opened with fopen_s function?
|
|
|
|
|
You may use fseek() to go the end of the file, and use ftell() to get file current position, which is now also the file size.
|
|
|
|
|
Once you have opened your file:
long size;
fseek(file,0,SEEK_END);
size = ftell(file);
rewind(file);
|
|
|
|
|
Hi,
In my application I created a wmv movie form 3 images. It also have the audio for 15 seconds. Audio per frame is variable means that we can have audiofor first frame for 2 sec , for second frame 10 sec and for 3rd frame 3 sec.
How can I achive this?
one solution can be variable frame rate for each frame. CAn we do this in wmv ? How can we do this? Is there any other alternative available to achive this
All help and guidelines are welcome
Thanks in advance
Regards,
Hemant.
|
|
|
|
|
Can't you keep a constant frame rate?
In your case one frame-per-second would work. So when you write the video stream you'd write
2 frames of the first image, 10 frames of the second image, and 3 of the third image.
Are you using a Video Image stream or a regular video stream for your wmv file?
|
|
|
|
|
What is difference between "Video Image stream" and "regular video stream"?
|
|
|
|
|
A video image stream is made up of still images. The stream is written differently than
regular "live" video when using the windows media sdk to write wmv files.
|
|
|
|
|
Hi,
I am creating a movie from some images it is not a real time video.
My important question is How I can write a metatdata >64 to wmv file?
Regards,
Hemant
|
|
|
|