LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2006, 04:55 PM   #1
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Rep: Reputation: 30
GCC errors using Pthreads


Hi everyone. I am writing a simple wrapper class for POSIX threads and mutexes but in my mutex class, I get an error saying

Code:
error: expected primary-expression before '{' token
error: expected ';' before '{' token
when trying to initialize the pthread_mutex_t in the mutex class's constructor.

Code:
Mutex::Mutex(void)
{
	mutex = PTHREAD_MUTEX_INITIALIZER;
}
However, that assignment does not cause any trouble when made outside of the Mutex() declaration, like in main() for example. I looked up the declaration of PTHREAD_MUTEX_INITIALIZER and it is #defined as

Code:
{_PTHREAD_MUTEX_SIG_init, {0}}
Could those brackets be causing the trouble when used inside of method declarations? If so, how can it be fixed? Any help would be appreciated.

-Brandon R
 
Old 07-16-2006, 08:09 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
First, doing (void) as a parameter list is bad C++ form. Use just () instead.
Second, always use initializer lists, and this could be the source of the problem.
Third, IMO tabs are evil, since the default is 8 spaces and you can't nest effectively.
If my first suggestion doesn't work, try initializing it with the declaration.
Code:
Mutex::Mutex()
 : mutex(PTHREAD_MUTEX_INITIALIZER)
{
}
 
Old 07-16-2006, 09:36 PM   #3
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
I would initialize the pthread_mutex_t as part of the declaration but, since it is a data member of the Mutex class I am writing, it can't be initialized as part of the declaration. I tried initializing the pthread_mutex_t in the initializer list but still get one of the two errors:

Code:
error: expected primary-expression before '{' token
One is better than two but it still needs some work. I still think it has to do with the fact that PTHREAD_MUTEX_INITIALIZER is a preprocessor definition. When processed,

Code:
Mutex::Mutex()
 : mutex(PTHREAD_MUTEX_INITIALIZER)
{
}
would work out to be

Code:
Mutex::Mutex()
 : mutex({_PTHREAD_MUTEX_SIG_init, {0}})
{
}
I'm not familiar with expressions like that. Should that compile? If so, is there any reason why it wouldn't?

As a more immediate solution, I've decided to use pthread_mutexattr_init() instead but I would still be interested in hearing an explanation of PTHREAD_MUTEX_INITIALIZER - how it works and why it doesn't here.

Last edited by spaaarky21; 07-16-2006 at 11:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc errors? rlprofessional Linux - Newbie 1 06-13-2006 12:33 PM
gcc errors using redhat 9 kevinmm Linux - Newbie 6 01-03-2004 08:03 AM
gcc compile errors ? qwijibow Linux - Newbie 2 07-21-2003 07:20 AM
gcc compiler errors externalgreenfu Linux - Software 0 07-16-2003 03:40 PM
gcc errors?? or are they? Rito Linux From Scratch 1 01-06-2003 04:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration