I have two files as below.
ppnfs_mutex.h
...
struct ppnfs_mutex_t
{
pthread_mutex_t mutex;
pthread_t owner;
const char* context;
};
...
ppnfs_mutex.cc
#define PPNFS_MUTEX_INITIALIZER \
{ .mutex = PTHREAD_MUTEX_INITIALIZER, .owner = 0, .context = NULL, }
struct ppnfs_mutex_t ppnfs_metadata_mutex = PPNFS_MUTEX_INITIALIZER; // line 6
I use g++ to compile this file and there is an error message as:
ppnfs_mutex.cc:6:47: error: expected primary-expression before ‘.’ token
What is the problem with the code?
That style of initialisation is allowed in C, but not in C++.
In C++, either initialise without naming the fields, and hope that nobody reorders them:
or provide a constructor or factory function, or (if you’re using C++11) use the standard thread library rather than posix threads.