|
Presumably the manager classes are created before the worker threads are active, so putting synchronization at that level seems appropriate. It is necessary then to prevent one thread from writing changes to these objects while others are also trying to read or write to them.
This article presents one technique for accomplishing such synchronization:
http://www.codeproject.com/threads/ReaderWriterLock.asp [^]
|
|
|
|
|
>>But to be honest I really haven't come up with the perfect way to solve this. It's a lot of
>>objects that can be accessed by both the GUI, the Webserver and the Worker Threads, so
>>honestly, wrapping every get/set accessor up in Mutexes isn't a very pretty solution.
It's not always pretty
I use a little helper class to make it a bit prettier:
<br />
class CMyCriticalSection<br />
{<br />
private:<br />
CRITICAL_SECTION CritSec;<br />
public:<br />
CMyCriticalSection() {::InitializeCriticalSection(&CritSec);}<br />
virtual ~CMyCriticalSection() {::DeleteCriticalSection(&CritSec);}<br />
<br />
void Enter() {::EnterCriticalSection(&CritSec);}<br />
void Leave() {::LeaveCriticalSection(&CritSec);}<br />
};<br />
So, for member variables that need thread access protection I add a CMyCriticalSection object:
<br />
class CMyClass<br />
{<br />
protected:<br />
CString Str;<br />
CMyCriticalSection StrCritSec;<br />
public:<br />
void ThreadSafeSetStr(const CString &);<br />
};<br />
<br />
void CMyClass::ThreadSafeSetStr(const CString &newstr)<br />
{<br />
StrCritSec.Enter();<br />
Str = newstr;<br />
StrCritSec.Leave();<br />
}<br />
Mark
|
|
|
|
|
You do not necessarily need to use a multi threaded approach with your server.
If locking and inter-thread-communication becomes a burden try a software architecture where socket connections are handled in one single thread context. When all sockets are running in the same thread context there is no need for locking shared data structures anymore. There are a couple of Unix web servers which follow this architecture (e.g. thttpd, mathopd), for Windows try the MFC class CAsyncSocket.
Hope this helps, feel free to brainstorm more...
|
|
|
|
|
first TrackPopupMenu,then how to response right button click on the menu?
thank you here
|
|
|
|
|
make one more menu IDR_MENU2
add handler
oncontext menu write code
void CChildView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
int h,w;
h=rect.Height();
w=rect.Width ();
CMenu conmenu;
conmenu.LoadMenu(IDR_MENU2);
CMenu *p=conmenu.GetSubMenu(0);
p->TrackPopupMenu(TPM_CENTERALIGN|TPM_RIGHTBUTTON,point.x ,point.y ,AfxGetMainWnd() );
CWnd::OnContextMenu(pWnd,point);
}
and on RButtonDOWn u write ur code
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
Thank you.
but I mean this:
I used trackpopupmenu showed a popup menu,then I want to do something when I right click on the menu,pay attention, I mean right click on the menu not right click to show the menu.
so,the point is how to handle right click on a popup menu message.
|
|
|
|
|
there r msg handlers for right click event also like WM_RBUTTONDOWM and so on
....u may use that event .....that code was for pop up menu
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
3141592653 wrote: so,the point is how to handle right click on a popup menu message.
you have to map the Menu ID with the It's respective Handller!
|
|
|
|
|
use english to ask your question, otherwise, people here won't understand what you're trying to achieve...
|
|
|
|
|
Hi All
How to implement printing without using Print Dialog ?
How to set default values (Paper size, Orientation etc) to Print Dialog ?
KK
|
|
|
|
|
|
|
This is all done via OpenPrinter() .
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
kk wrote: How to set default values (Paper size, Orientation etc) to Print Dialog ?
CreateDC for creating Printer DC and TextOut for printing on the papar!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I am having problems using OleLoadPicture in multithreaded application.
Is this function really thread safe and does not pose any threats?
Does it have any disadvantages? - Like I've heard that it creates temporary files to cache the images we load.
How to tackle the multithreaded scenario as my application is creating more than 100 threads, each of which uses this function, and then one of the threads crashes while calling OleLoadPicture .
Thanks in advance,
Amit.
|
|
|
|
|
Can you explain why you make 100 threads that use OleLadPicture ?
|
|
|
|
|
The use of this function has been made in a dll.
This dll is going to be used from a webservice. Right now I am trying do load testing through a C# appliaction and make the dll threadsafe.
If I am using it for single thread then it works fine, but for multiple threads some threads finish their working successfully, and one of them crashes the application.
There is no problem with image.
|
|
|
|
|
Are you sure this error is of thread not OleLadPicture?
|
|
|
|
|
The problem is not with the thread.
The thing is that, some threads will finish successfully (They execute OleLoadPicture sucessfully) and suddenly in a random thread the function OleLoadPicture will not work, and then rest all threads which have yet to call this OleLoadPicture function, will not be able to execute it.
|
|
|
|
|
How to run your threads immediately?
|
|
|
|
|
WhiteSky wrote: How to run your threads immediately?
I am not getting your question. Are you asking me post the code?
Well, the threads are created from C# code and the call to function OleLoadPicture is in a C++ dll. This dll contains both managed and unmanaged code.
|
|
|
|
|
oh it seems that I asked of you if you dont have prolem with OleLoadPicture and all things is ok and also threads working without problem except a thread I think your problem is on memory why?because you said you got this error in random case so I think you need to free memory (I had a problem like this with threads but my program sudden terminated and I was wodering when I trace my prgroam line to line it was working but if I run it didnt work...my problem was I needed to close threads and free memory) Now my guess is this,Did you check for free memory?
|
|
|
|
|
As you said there was this problem too that the threads were not getting terminated. Then one of my friends suggested using mutex object. So I tried to use mutex whereever memory allocation is needed and it worked. Also the threads are getting terminated properly.
But still there is a problem and that is of the memory not getting freed. Anyway, I will look into it.
Thank you,
Amit.
|
|
|
|
|
If you want to use CreateMutex you need to call RleaseMutex for each mutext(why its not free)
|
|
|
|