[LC++]Why doesn't PTHREAD_MUTEX_INITIALIZER work my constructor?
David Filion
filiond at videotron.ca
Sat Nov 16 21:32:01 UTC 2002
Hello,
I have created a class that contains a phtread mutex that I want
to initialize in the class constructor.
If I use:
mutex = PTHREAD_MUTEX_INITIALIZER;
in the class contructor I get a compilation error. If I use:
pthread_mutex_init (&mutex, NULL);
it works.
Example:
========== Thread.h: =============
#ifndef TRY_H
#define TRY_H
#include <pthread.h>
class Threads {
private:
pthread_mutex_t mutex;
long somevar;
public:
Threads ();
int lock ();
int unlock ();
};
#endif
========== End Thread.h ===========
========== Start Thread.cc =========
#include "Threads.h"
Threads::Threads ()
{
// This return a compilation error of:
// Threads.cc: In method `Threads::Threads()':
// Threads.cc:6: parse error before `{'
//
//mutex = PTHREAD_MUTEX_INITIALIZER;
// This works.
pthread_mutex_init (&mutex, NULL);
}
int Threads::lock ()
{
return (pthread_mutex_lock (&mutex));
}
int Threads::unlock ()
{
return (pthread_mutex_unlock (&mutex));
}
========== End Thread.cc =========
Why is that? Does it have to do with the fact that
the mutex is being initialized at runtime instead
of at the time of compilation? If that is the case
why am I getting a syntax error and some kind of
linking error?
System:
Slackware 8.1
g++ 2.95.3
glibc 2.2.5
Thanks
David Filion
More information about the tuxCPProgramming
mailing list