Click here to Skip to main content
15,890,609 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
JokeRe: please guide I program for fax modem, thanks Pin
CPallini7-Apr-09 0:38
mveCPallini7-Apr-09 0:38 
GeneralRe: please guide I program for fax modem, thanks Pin
Rajesh R Subramanian7-Apr-09 0:52
professionalRajesh R Subramanian7-Apr-09 0:52 
AnswerRe: please guide I program for fax modem, thanks Pin
David Crow7-Apr-09 3:08
David Crow7-Apr-09 3:08 
Questionprevious posts and replies.... Pin
p_19606-Apr-09 23:17
p_19606-Apr-09 23:17 
AnswerRe: previous posts and replies.... Pin
Niklas L6-Apr-09 23:24
Niklas L6-Apr-09 23:24 
AnswerRe: previous posts and replies.... Pin
CPallini6-Apr-09 23:26
mveCPallini6-Apr-09 23:26 
QuestionHow to display a diagram charted by visio, such as topological diagram, iwith mfc? Pin
liuxg20086-Apr-09 22:27
liuxg20086-Apr-09 22:27 
QuestionSynchronization problem. (Hiding pointers addresses behind access keys) Pin
Ahmed Charfeddine6-Apr-09 21:54
Ahmed Charfeddine6-Apr-09 21:54 
Hi there.

I have an application ofwhich threads concurrently act on multiple instances of a certain class called CChannel.

CChannel has a method called ::Send which must be protected. For that end I put in a place a Critical section as a data member of CChannel then I call EnterCriticalSection and LeaveCriticalSecion within ::Send. I avoided to use a global critical
section for performance reasons : indeed I do'nt want to have a blocking scenario when thwo threads call ::Send on two different instances.

At some point of time, I need to delete a Channel object. Since threads hold references to the pointer addresses, the application can easily crash if one thread tries to use a certain address pointing to an object that has been deleted by another thread.

To overcome this, instead of communicating addresses, rather I communicate access numbers. these number serv as a key for a map that stores context structures containing the actual addresses and also the per-instance CriticalSection. So the CriticalSection which used to be a data member of Channel is now out of the class (I will be able to delete a Channel object, but still use its acorresponding CriticalSection)


Here my solution, I am not confident, I think it works, but anyway I will be happy to be helped with a much more solid solution.

<pre>//This is the wrapper structure. it contains the criticalsection, the pointer to CChanel
typedef struct ChannelContext
{
CChannel* pChannel;
CCriticalSection cs;
} ChannelContext;
typedef std::map&lt;UINT,ChannelContext*&gt; channelMapT;
channelMapT channelMap;
CCriticalSection csMap; //(used to protect the map).
//The following two function can be called without problem by any thread :

//This function is called by a thread when it needs to act on a certain CChannel.
//it is meant to provide safe access to the CChanell. the calling thread only needs to check if the retunred
//address is not null, then consume pContext-&gt;pChannel, then when it finishes its job with the Channel is calls
//pContext-&gt;LeaveCriticalSection(&amp;pContext-&gt;cs).
ChannelContext* requireChannelByKey(UINT accessKey)
{
EnterCriticalSection(&amp;csMap);

channelMapT::iterator myIt=channelMap.find(accessKey);

if(myIt!=channelMap.end())
{
ChannelContext* pContext=myIt-&gt;second;
EnterCriticalSection(&amp;pContext-&gt;cs);

LeaveCriticalSection(&amp;csMap);
return pContext;
}

LeaveCriticalSection(&amp;csMap);
return NULL;
}

//Function to delete an object.
void deleteChannel(UINT accessKey)
{
EnterCriticalSection(&amp;csMap);
channelMapT::iterator myIt=channelMap.find(accessKey);

if(myIt!=channelMap.end())
{
ChannelContext* pContext=myIt-&gt;second;
EnterCriticalSection(&amp;pContext-&gt;cs);


//Delete the entry.
channelMap.erase(myIt);
delete pContext-&gt;pChannel;
LeaveCriticalSection(&amp;pContext-&gt;cs);
delete pContext;
}

LeaveCriticalSection(&amp;csMap);
}</pre>


Thank you in advance. Note, if someone have ideas how Windows manages its internal structure then please enlignten me.
I think that HANDLE just act like the accessKey : it hides the pointer addresss. But what about synchrnoization ?

Easy Profiler : a compile-time profiler for C++
www.potatosoftware.com

modified on Tuesday, April 7, 2009 4:07 AM

AnswerRe: Synchrnoization problem. Pin
Sarath C6-Apr-09 22:04
Sarath C6-Apr-09 22:04 
GeneralRe: Synchrnoization problem. Pin
Ahmed Charfeddine6-Apr-09 22:22
Ahmed Charfeddine6-Apr-09 22:22 
QuestionMainFrame Maximization problem Pin
prithaa6-Apr-09 20:50
prithaa6-Apr-09 20:50 
AnswerRe: MainFrame Maximization problem Pin
«_Superman_»6-Apr-09 21:04
professional«_Superman_»6-Apr-09 21:04 
GeneralRe: MainFrame Maximization problem Pin
prithaa7-Apr-09 19:38
prithaa7-Apr-09 19:38 
GeneralRe: MainFrame Maximization problem Pin
prithaa10-Apr-09 5:53
prithaa10-Apr-09 5:53 
QuestionShow selected rows in ListCtrl Pin
Pryabu6-Apr-09 19:14
Pryabu6-Apr-09 19:14 
AnswerRe: Show selected rows in ListCtrl Pin
«_Superman_»6-Apr-09 19:24
professional«_Superman_»6-Apr-09 19:24 
GeneralRe: Show selected rows in ListCtrl Pin
Pryabu6-Apr-09 19:58
Pryabu6-Apr-09 19:58 
QuestionRe: Show selected rows in ListCtrl Pin
«_Superman_»6-Apr-09 20:13
professional«_Superman_»6-Apr-09 20:13 
AnswerRe: Show selected rows in ListCtrl Pin
Pryabu6-Apr-09 20:19
Pryabu6-Apr-09 20:19 
GeneralRe: Show selected rows in ListCtrl Pin
Pryabu6-Apr-09 20:25
Pryabu6-Apr-09 20:25 
QuestionRe: Show selected rows in ListCtrl Pin
David Crow7-Apr-09 3:12
David Crow7-Apr-09 3:12 
AnswerRe: Show selected rows in ListCtrl Pin
CDrone6-Apr-09 20:00
CDrone6-Apr-09 20:00 
GeneralRe: Show selected rows in ListCtrl Pin
Pryabu6-Apr-09 20:10
Pryabu6-Apr-09 20:10 
GeneralRe: Show selected rows in ListCtrl Pin
CDrone6-Apr-09 20:22
CDrone6-Apr-09 20:22 
AnswerRe: Show selected rows in ListCtrl Pin
Sophiya Chen6-Apr-09 22:32
Sophiya Chen6-Apr-09 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.