|
I hear that the memory pool can improve the performance. It means that allocate a block of memory and then new the class object in this memory block, but how to make the thread safe and proceeding safe?
modified on Wednesday, September 22, 2010 11:10 AM
|
|
|
|
|
A memory pool (implemented as a custom allocator or overiding new/delete for a class) can improve speed a lot. HOWEVER before you do make sure that your app is actually eating up loads of execution time in the ordinary allocator before doing anything about it. I've seen plenty of people mess about and spend ages optimising allocators and get nothing concrete out of it.
If you decide to use a custom allocator then you make it thread safe the way you do any other object - use a synchronisation object and lock it when doing any operations on the object. Incidentally I've seen the cost of synchronisation wipe out any benefit a custom allocator gives you, so again make sure you're just not replacing a runtime library bottleneck with a carefully crafted homemade one.
There are ways you can reduce locking bottlenecks from an allocator but you start needing to use multiple arenas to allocate from and switch between them on each allocation. This can then start messing up locality of reference (unless you manage interleaved arenas) and again slow everything down again.
Anyway, the point I'm really making here is measure how long your app spends in the allocator. If it's taking an appreciable length of time consider using a third party custom allocator or implementation of new and delete and only write your own if it's still not fast enough.
Another idea would be to use a garbage collector - if you google "Hans" "Boehm" and "garbage collection" you can find a decent one (if you like that sort of thing) that's written by one of the luminaries of the C++ standards committee. Garbage collection has the advantage that when you delete objects the memory is not immediately freed and is recovered in one fell swoop later by the garbage collector reducing contention for the allocator when releasing objects.
Cheers,
Ash
modified on Monday, September 20, 2010 7:05 AM
|
|
|
|
|
good answer.
I've bumped (5-ved) the question also.
Watched code never compiles.
|
|
|
|
|
Thanks for the reply.
I've given the questioner a five as well - normally I wouldn't but in this case his question didn't deserve the one someone else gave him so I'm attempting to be karmic.
Cheers,
Ash
|
|
|
|
|
Override the new/delete operators and use "placement new" syntax to alloc in a predetermined location, managed by you.
onwards and upwards...
|
|
|
|
|
If you take the memory from your own Win32 Heap, then it is already thread-safe. See HeapCreate.
|
|
|
|
|
I find that I could not distinguish the synchronization from the block, and asynchronism from no-block.
when use the synchronization to socket with the server, it must be block model. How to achieve it with the non-block model?
|
|
|
|
|
I don't understand your question. You probably mean synchronisation in the context of multi threading, with non-blocking sockets (asynchronous networking) you typically have a single threaded application.
/M
|
|
|
|
|
Thanks for your reply. It not multi-thread, it is using socket. I do not know the different between synchronization and block.
|
|
|
|
|
CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007Luna_Blue);
this code is set my program's color blue.
but I want red or another.
How to create a style by myself?
How to change the style that vs2008 feature pack?
thanks!
|
|
|
|
|
Hi everyone ! Do anybody know ( because I took from here many useful links ) an auto complete control implementation of CEdit or CComboBx control ? I found HEREsomething interesting , but not working on SDI/MDI application , so is in vain , and autocomplete implementation is based on CList , not on CListCtrl ... any hint or link will be very appreciated ! Thanks in advance !
|
|
|
|
|
|
Thanks , first sample is allready implemented , second is not quite what I need ... I keep search , or , if I can , I buld myself ... thanks again !
|
|
|
|
|
i am a newbie and creating a project in c language on windows platform.
i am trying to develop a small application using winAPI 32. I am taking online tutorial from winprog.org ,in my tutorial i am creating a window from a given code but when i run the code ,my window name appearsll to be in different language .The code i used is given below. I am not able to understand why this sia happening . please help me .. I am using vc++ express edition 2008 on window 7.
i'll be very thankful to you.
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
|
|
|
|
|
Always use code block and paste ur code in betweenthat block. code block is right on the text field where you write your question. Number of people answering you question, will dramatically change. TRUST ME.
|
|
|
|
|
right.
|
|
|
|
|
You might find the properly formatted code cross posted in the QA...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
See, that is a problem with editable posts: the enquirer did not format properly in Q&A, someone cleaned it up, and the enquirer probably didn't learn a thing; he may get an answer and never figure how to use PRE tags.
|
|
|
|
|
Yes, no HTML , but see the positive side: he might go on and probably learn a bit of software development.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
gateway23 wrote: ,my window name appearsll to be in different language
What do you mean, exactly?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
He asks why the window title says "Finestra di base"...
|
|
|
|
|
Luc Pattyn wrote: He asks why the window title says "Finestra di base"...
Well, I see NOTHING wrong in that.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
sorry for the inconvenience , i want to say ..when i run the code ,the windows title appears comes in different language ,it must come in english but it appears in different language.
|
|
|
|
|
hello guys...I added ListBox control on the form, so it accessible in MyProjDlg.cpp only. Now I want to add something to this ListBox from some other class "MyClass.cpp". So I made a function setter() in MyProjDlg.h like this....
#pragma once
#include "afxwin.h"
class MyProjDlg: public CDialog{
public:
.....
protected:
.....
public:
afx_msg void OnAdd();
afx_msg void OnDelete();
afx_msg void OnShow();
void setter(LPSTR str);
CListBox ListVar;
};
and gave its its implementation in MyProjDlg.cpp
void setter(LPSTR str) {
MessageBox(NULL,str,"",NULL);
}
and used it in MyClass.cpp like this..
LPSTR str = "Rumble in the Bronx"
for (int i=0; i<5; i++) {
setter(str);
}
problem is,setter()it is not accessible in the MyClass.cpp....why
|
|
|
|
|
setter() is a member of the MyProjDlg class so can only be called on an object of that class.
It's time for a new signature.
|
|
|
|