-1

I am getting on error on Mac OS X 10.12 involving Clang, and it does not want compile my code. I want it to compile through the compiler, but it persists me with "error: member initializer 'pthread_mutex_init' does not name a non-static data member or base class" when calling pthread_mutex_init. I have tried adding and removing "static" in front of the pthread_mutex_t declaration and I have already included my pthread header file

EDIT: yes, I included file.h in my file.cpp file. EDIT #2: I have tried mutex_ = (mutex_pthread_t)PTHREAD_MUTEX_INITIALIZER and it gives some weird error telling me to insert "{" somewhere.

Here is my code:

Memory.h: 
#include <pthread.h>
class Memory {
    static pthread_mutex_t mutex_;
}

Memory.cpp:
#include <Memory.h>
#include <pthread.h>
Memory::Memory() : 
#ifdef __APPLE__
    pthread_mutex_init(&mutex_, NULL);
#endif
Big D.
  • 11
  • 1
  • 2
  • Hey kid. Instead of downvoting, can you please just tell me what I need to add or to fix. I'd let you downvote if you gave me the reason why, but being an a-hole like that won't help you in life. – Big D. Nov 05 '16 at 00:49
  • can you offer some more context? A minimal complete program will help the community to understand what you have missed. – Richard Hodges Nov 05 '16 at 01:02
  • Ok added more context. Please help me – Big D. Nov 05 '16 at 01:06
  • You seem to have a misunderstanding of the syntax of member definitions. I suggest you pick up a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and after you finish reading it keep it handy for reference. – Captain Obvlious Nov 05 '16 at 01:10
  • Any help with the pthread stuff? I can worry about the member defintions later. BTW I still get the same error if I defined mutex_ in the same Memory.cpp file – Big D. Nov 05 '16 at 01:11
  • 1
    _" I can worry about the member defintions later"_ - Yeah good luck with that. – Captain Obvlious Nov 05 '16 at 01:14
  • Can you please help me with errors in the member definitions in the code? – Big D. Nov 05 '16 at 01:21
  • On the off chance you didn't know, the clang chain accompanying Xcode has a fully functional C++14 standard-library implementation of [``](http://en.cppreference.com/w/cpp/thread/mutex) and all ther other thread-support goodies. If you can just go that route, it is a good consideration. Makes doing threading in C++ *sooo* much easier than pthreads. Best of luck, btw. – WhozCraig Nov 05 '16 at 04:42
  • @WhozCraig Thanks a lot for the help! I'll try the implementation as this pthread stuff is getting frustrating. – Big D. Nov 05 '16 at 05:14

1 Answers1

1

I am assuming you want there to be one mutex shared amongst all Memory objects?

Here are 2 (of many) ways, with subtly different side-effects:

#include <pthread.h>
class Memory {

  // I am assuming that you wanted the mutex to be initialised
  // at program start?

  static bool init_mutex();

  static pthread_mutex_t mutex_;
  static bool initialised;
};

bool Memory::initialised = init_mutex();

bool Memory::init_mutex()
{
  pthread_mutex_init(&mutex_, 0);
  return true;
}


// or what about upon first use of a Memory?

class Memory2
{
  struct impl {
    impl() {
      pthread_mutex_init(&mutex_, 0);
    }

    pthread_mutex_t mutex_;
  };

  static impl& get_impl()
  {
    static impl impl_;
    return impl_;
  }

  Memory2()
  {
    get_impl();
  }
};
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Many thanks Richard. I will try to implement this code and i'll report back if it works. Either way thanks for supplying me an answer. – Big D. Nov 05 '16 at 02:32
  • I tried the first solution and it still gives me the error "error: member initializer 'init_mutex()' does not name a non-static data member or base class" when I called the init_mutex() function from the Memory.cpp. I put the definition of the function and the variable declarations in the Memory.h, and it still gives me that annoying "non-static" error message. I think I'm just going to use the XCode implementation of pthreads tbh. As for the second solution, it isn't my main goal and it gave me even more errors. Still, I am thankful for your insightful answer. – Big D. Nov 05 '16 at 05:22