Re: undivideable action?



Use a mutex or a CRITICAL_SECTION.

What you mean is "I want an undivideable action" (MFC is irrelevant to the question) "that
guarantees that any attempt to work on a data structure before I complete my action will
be blocked".

Note that EVERY access to the data structure MUST be protected by the same synchronization
primitive.

For example, to add or delete elements to a list, I would move all this code into a class


class CListManager {
public:
CListManager() { InitializeCriticalSection(&lock); }
virtual ~CListManager() { DeleteCriticalSection(&lock); }
void AddElement(Thing *) {
EnterCriticalSection(&lock);
...add it to list...
LeaveCriticalSection(&lock);
}
void RemoveElement(Thing *) {
EnterCriticalSection(&lock);
...remove it from list..
LeaveCriticalSection(&lock);
}
protected:
CRITICAL_SECTION lock;
...list variable of bunch of Thing * objects...;
}

Note that because your list is protected, the ONLY way to access it is with a method of
this class, and EVERY method of the class will lock and unlock the lock.
joe

On Fri, 10 Aug 2007 02:48:56 -0700, RAN <nijenhuis@xxxxxxx> wrote:

Hi,

How can i create an undivideable action in MFC ?
I mean i want to execute a certain function and this function (method)
may not be interrupted by anything else (if this is possible all
together).

The situation is this.

I have multiple clients that connect to my server using CAsyncSocket.
I store this accepted clientsockets in a standard C double linked
list.
If one of the clients closes down the OnClose is called at the server
and i want to remove (delete) the clientsocket record from the double
linked list. But there are more action taken on this double linked
list such as storing a record when a client connects. More stuff is
done also. I want to be sure that the double linked list is not
corrupt when adding a new client record, while deleting etc.
How do i make sure that the records in the list are always properly
deleted, can i make sure that the DeleteClientSocket() method which
deletes records from the list is always completly executed so i have
an intact double linked list ?!
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.