Thread: ISO C++ forbids declaration of pthread_mutex_t with no type

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    ISO C++ forbids declaration of pthread_mutex_t with no type

    I'm getting weird errors for a semaphore class i'm writing. I don't know
    what's causing this and have searched all over google for the answer with no luck.
    Here's the code. please help! thanks!
    Code:
    // semaphore.hpp
    #include <pthread.h>
    #include <stdio.h>
    #include <cstdlib>
    #include <unistd.h>
    
    #ifndef __SEMAPHORE_HPP_
    #define __SEMAPHORE_HPP_
    
    namespace Threading {
      class Semaphore {
      public:
        Semaphore(unsigned int resource=0);
        /* The initial value for the semaphore can be specified */
        ~Semaphore() { /* nothing to do */ }
        int getCount() const { return sCount; }
        /* Get the current value of a semaphore. */
        void post();
        /* Posting to a semaphore increments its current value 
         * and releases the first thread waiting for the semaphore 
         * if it is currently at 0. */
        bool tryWait();
        /* TryWait is a non-blocking variant of Wait. */
        void wait();
        /* Wait is used to keep a thread held until the semaphore 
         * counter is greater than 0. */
    
      private:
        int sCount;
        /* Count of semaphore */
        pthread_mutex_t mMutex;
        /* Mutex to protect count */
        pthread_mutex_t cMutex;
        /* Mutex to protect condition value*/
        pthread_cond_t condition;
        /* Condition of availability of resource */
        bool cValue;
        /* Condition value */
      };
    };
    #endif //__SEMAPHORE_HPP_
    Code:
    // semaphore.cpp
    #include "semaphore.hpp"
    
    using namespace Threading;
    
    Semaphore::Semaphore(unsigned int resource) {
      sCount = resource;
      mMutex = PTHREAD_MUTEX_INITIALIZER;
      cMutex = PTHREAD_MUTEX_INITIALIZER;
      condition = PTHREAD_COND_INITIALIZER;
    }
    
    void Semaphore::post() {
      pthread_mutex_lock(&mMutex);
      sCount += 1;
      if(sCount <= 0) {
        // condition signal
        pthread_mutex_lock(&cMutex);
        cValue = false;
        pthread_cond_signal(&condition);
        pthread_mutex_unlock(&cMutex);
      }
      pthread_mutex_unlock(&mMutex);
    }
    
    bool Semaphore::tryWait() {
      pthread_mutex_lock(&mMutex);
      if(sCount <= 0) {
        pthread_mutex_unlock(&mMutex);
        return false;
      }
    
      sCount -= 1;
    
      if(sCount < 0) {
        pthread_mutex_unlock(&mMutex);
        // condition wait
        pthread_mutex_lock(&cMutex);
        while(!cValue) {
          pthread_cond_wait(&condition, &cMutex);
        }
        pthread_mutex_unlock(&cMutex);
        // condition reset
        pthread_mutex_lock(&cMutex);
        cValue = false;
        pthread_mutex_unlock(&cMutex);
      }
      else pthread_mutex_unlock(&mMutex);
    
      return true;
    }
    
    void Semaphore::wait() {
      if(sCount < 0) {
        pthread_mutex_unlock(&mMutex);
        // condition wait
        pthread_mutex_lock(&cMutex);
        while(!cValue) {
          pthread_cond_wait(&condition, &cMutex);
        }
        pthread_mutex_unlock(&cMutex);
        // condition reset
        pthread_mutex_lock(&cMutex);
        cValue = false;
        pthread_mutex_unlock(&cMutex);
      }
      else pthread_mutex_unlock(&mMutex);
    }
    Code:
    semaphore.cpp:7: parse error before `{'
    semaphore.cpp: At top level:
    semaphore.cpp:8: ISO C++ forbids declaration of `cMutex' with no type
    semaphore.cpp:8: initializer for scalar variable requires one element
    semaphore.cpp:9: ISO C++ forbids declaration of `condition' with no
    type
    semaphore.cpp:9: initializer for scalar variable requires one element
    semaphore.cpp:10: parse error before `}'
    make: *** [semaphore.o] Error 1
    make: Target `proj3B' not remade because of errors.
                                                                                                                                                                
    Compilation exited abnormally with code 2 at Thu Nov  6 19:11:07

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    The PTHREAD_MUTEX_INITIALIZER macro initializes the pthread_mutex_t structure when you first declare it.

    You cannot assign the values to a struct using the curly-brace syntax (which is what that macro does) after you've declared it. As you probably guessed, you get a parse error

    From what I can see you'll need to initialize mMutex, cMutex, etc with PTHREAD_MUTEX_INITIALIZER at their declaration instead. I think.

    IANAE (I Am Not An Expert)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  4. Forbids Declaration with No Type
    By ProgrammingDlux in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2002, 07:39 PM
  5. help, templates and hashing, and yucky stuff like that
    By DarkDays in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2001, 06:01 AM