Board index » Visual Studio » cmutex ?

cmutex ?

Visual Studio60
Hello,

I'd like to use this below code to protect some code in IE toolbar, but not

sure if this is best choice, since IE is a window app:

CSingleLock mlock(&m_Mutex);

mlock.Lock()

do some file writing here...

mlock.Unlock()



Do I risk something here? (m_Mutex is CMutex)

regards


-
 

Re:cmutex ?

buzz wrote:

Quote
Hello,

I'd like to use this below code to protect some code in IE toolbar, but not

sure if this is best choice, since IE is a window app:

CSingleLock mlock(&m_Mutex);

mlock.Lock()

do some file writing here...

mlock.Unlock()



Do I risk something here? (m_Mutex is CMutex)

regards







Protect from what? A mutex provides synchronization between two

threads. It only works if both threads cooperate and use the same

mutex. Used alone, the code above accomplishes nothing. It is half of

a solution to some problems.



--

Scott McPhillips [VC++ MVP]



-

Re:cmutex ?

That mutex is the same for alll instances of toolbar.

its declared as m_Mutex(FALSE, _T("xyz")) in contructor.

I want to protect place where the file is being written ofcourse...





"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>wrote in message

Quote
buzz wrote:

>Hello,

>I'd like to use this below code to protect some code in IE toolbar, but

>not

>sure if this is best choice, since IE is a window app:

>CSingleLock mlock(&m_Mutex);

>mlock.Lock()

>do some file writing here...

>mlock.Unlock()

>

>Do I risk something here? (m_Mutex is CMutex)

>regards

>

>



Protect from what? A mutex provides synchronization between two threads.

It only works if both threads cooperate and use the same mutex. Used

alone, the code above accomplishes nothing. It is half of a solution to

some problems.



--

Scott McPhillips [VC++ MVP]







-

Re:cmutex ?

bobbi wrote:

Quote
That mutex is the same for alll instances of toolbar.

its declared as m_Mutex(FALSE, _T("xyz")) in contructor.

I want to protect place where the file is being written ofcourse...



You still have not mentioned multiple threads or multiple processes. If

you are not dealing with one of those situations then the mutex

accomplishes nothing. Multiple instances of a toolbar, all within an

application's main thread, will never execute concurrently so they would

never need mutex protection.



If you don't understand, you could try to explain what you are trying to

prevent, and why you think it might happen. So far, you have not

mentioned a problem that needs a mutex solution.



--

Scott McPhillips [VC++ MVP]



-

Re:cmutex ?

Of course I've mentioned, first: this is IE adds on, so you can have more

then one thread or

more then one process, thats why I used mutex. That part I know, I didn't

ask

if I choose mutex right. I'm mot sure if I shouldn't use

MsgWaitForMultipleObjects()

instead of CSinleLock().

Second: My concern is that same file can be written by different IE threads

or process.





"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>wrote in message

Quote
bobbi wrote:

>That mutex is the same for alll instances of toolbar.

>its declared as m_Mutex(FALSE, _T("xyz")) in contructor.

>I want to protect place where the file is being written ofcourse...



You still have not mentioned multiple threads or multiple processes. If

you are not dealing with one of those situations then the mutex

accomplishes nothing. Multiple instances of a toolbar, all within an

application's main thread, will never execute concurrently so they would

never need mutex protection.



If you don't understand, you could try to explain what you are trying to

prevent, and why you think it might happen. So far, you have not

mentioned a problem that needs a mutex solution.



--

Scott McPhillips [VC++ MVP]







-

Re:cmutex ?

bobbi wrote:



Quote
Of course I've mentioned, first: this is IE adds on, so you can have more

then one thread or

more then one process, thats why I used mutex. That part I know, I didn't

ask

if I choose mutex right. I'm mot sure if I shouldn't use

MsgWaitForMultipleObjects()

instead of CSingleLock().



I think MsgWaitForMultipleObjects() would make the the process that

needs to write to the file wait for all the others to have to write the

file and make them write simultaneously?



Give the mutex a more unique name. I wonder how many other amateur

programs use xyz or qwerty as the mutex name. e.g. use

"APPNAME_YOURNAME_DATE".



Remember the MSDN warning:

Security Note After creating the CMutex object, use GetLastError to

ensure that the mutex did not already exist. If the mutex did exist

unexpectedly, it may indicate a rogue process is squatting and may be

intending to use the mutex maliciously. In this case, the recommended

security-conscious procedure is to close the handle and continue as if

there was a failure in creating the object.



-