Mutexs work with multiple threads or multiple processes. What you describe is not a problem if there are no other threads or processes that want access to the mutex at the same time. Add a Sleep() before the WaitForSingleObject() to allow time for other threads to gain access to the mutex and see if the wait function blocks until the other thread(s) have released the mutex.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Most probably the problems are not about security attributes but about you not using threads. Ancient Dragon made a good point above.
Think what would happen if the wait call would block when you are doing it from the same thread that created the mutex in the first place (and you are not having any other threads operating on the mutex).
I think you can forget the security attributes for now and try grasping the basic understanding how the mutexes work. You can use the mutexes also with NULL security attributes, so you don't have to wrestle with those attributes.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
hReaderMutex is not defined anywhere, hence the error. To get over this problem you need something like ...
// in MultiReader.cpp ...
const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
However, this is a bit bad construct, because when the CreateMutex() fails, you will not know the reason for the failure (GetLastError()).
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
Initialize outside the class i.e.
// outside your class ...
const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
You should (really) check the return value of WaitForSingleObject() and react accordingly. And also understand what it means to use zero as the time-out interval with WaitForSingleObject().
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
static methods can only access static class objects. So if that vector was not declared static then the threads can't access it. This has nothing to do with threads and everything to do with c++ classes.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
static data objects must also be declared something like global objects
// *.h file
#include <vector>
#include <string>
class foo
{
public:
static std::vector<std::string> lst;
// other stuff not shown
};
// *.cpp file
#include "foo.h"
std::vector<std::string> foo::lst;
int main()
{
return 0;
}
In meantime I found this:
http://homepages.tesco.net/J.deBoyne...functions.html
Onlystatic class methods can be used as thread functions and that link explains why. You already made the method static so don't worry about that part.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343