Can anyone offer any advice on CMutex (M.F.C.).

Hi,

I've been trying to implement the CMutex class in my own code. I have a method that refreshes a CListCtrl object. This is done as a thread as not to elongate the OnTimer() event.

However this results in a CInvalidArgException being thrown. I want to implement a CMutex object to guard against multiple access to my Refresher routine.

As with most things Window's Programming related, I can't quite grasp the syntax and I was hoping someone could clear it up for me.

So far I have declared the CMutex object pointer as a member of the class who's OnTimer event is being used to invoke the Refresher routine. I attempt to instantiate the CMutex on the first call of the Refresher routine (I'm not sure if this is correct). I'm still figuring out the rest.

Thanks for any help!
First of all, a mutex seems overkill because all you want is thread synchronization between threads of the same process. This can be done with a semaphore or a critical section which are faster than mutexes.

Second, you should post minimal code that illustrates your situation. Just remember that the new thread requires access to the mutex object, so it is a good idea to have the object created before entering the new thread. If OnTimer() is the one triggering the thread, OnTimer() should create the synchronization object BEFORE starting the new thread.
webJose wrote:
Second, you should post minimal code that illustrates your situation.
I only really added 3 lines of code to various different files I have so I thought it would be clearer if I explained the situation, but I'll bare this in mind for next time.

Thanks for the reply, I'll look into semaphores.
Out of interest, is your list control costly to refresh because it takes a lot of effort to calculate/obtain a few results, or because you're dealing with a load of results?
Last edited on
Topic archived. No new replies allowed.