Can someone Explain Mutex and how it is used?

前端 未结 4 1212
天涯浪人
天涯浪人 2021-01-12 10:01

I read a few documents about Mutex and still the only Idea I have got is that it helps preventing threads from accessing a resource that is already being used by another res

4条回答
  •  没有蜡笔的小新
    2021-01-12 10:47

    This link in msdn provides a similar example as yours with threads made in the main() function. But again the shared resource, which is supposed to be a database is not included.
    Anyway, a shared resource is whatever that needs to be accessed from multiple threads: settingsfiles, drivers, database,...

    Mind you that the counter in the example is written while protected by the mutex, while it is been read while not being protected. While in this case, there is probably no problem, it is a bit sloppy.

提交回复
热议问题