Home > pthread mutex initializer compiler > pthread_mutex_initializer compiler error

Pthread_mutex_initializer Compiler Error

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about pthread_mutex_init example Stack Overflow the company Business Learn more about hiring developers or posting ads with pthread_mutex_t us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is pthread_mutex_destroy a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Checking for error when using the PTHREAD_MUTEX_INITIALIZER macro up vote 1 down vote

Pthread_mutex_lock

favorite As a rule, I was told that any system call (or alike) should be tested for errors on return. When initializing a pthread mutex using: pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER; We don't check wether the operation was successful or not. But using this macro instead of dynamic initialization seems to be a common practice. Is there any good reason an error check isn't needed in this case? c++ pthreads share|improve this question asked Apr 29 '14 at 8:45 Paz 436215 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted The reason error checking isn't necessary is because static initialization cannot fail. In the most common implementations, PTHREAD_MUTEX_INITIALIZER will be something along the lines of 0 or {} (depending on how pthread_mutex_t is defined), to ensure zero initialization, and the the various system calls which use the pthread_mutex_t are designed to treat a zero initialized pthread_mutex_t as if it had been initialized to all default values, either because the type was designed explicitly this way, or because the routines use lazy initialization. (Note that PTHREAD_MUTEX_INITIALIZER cannot be anything along the lines of __special_mutex_initializer(), since it can be used to initialize file scope static variables in C, and C requires compile time constant expressions for the initialization of static variables.) EDIT: For more information, you might want to read the rationale section of http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html. This describes several different possible implementations, and some of the trade-offs involved. share|improve this answer edited Apr 29 '14 at 9:16 answered Apr 29 '14 at 9:03 James Kanze 114k794220 add a comment| up vote 0 down vote From IBM Note: Mutex initialization using the PTHREAD_MUTEX_INITIALIZER does not immediately

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PTHREAD_MUTEX_INITIALIZER inside C++ member function cannot compile? up vote 6 down vote favorite 1 class A { public: A(); private: pthread_mutex_t mu; }; A::A() { mu = PTHREAD_MUTEX_INITIALIZER; //cannot compile } Can't I initialize pthread_mutex_t http://stackoverflow.com/questions/23359776/checking-for-error-when-using-the-pthread-mutex-initializer-macro inside a class member function? c++ pthreads share|improve this question asked Jul 26 '12 at 9:50 Alcott 4,7781263126 1 What you show is an assignment not an initialization. –Flexo♦ Jul 26 '12 at 9:54 @Flexo, OOPS, sorry. –Alcott Jul 26 '12 at 9:56 No need to apologise! –Flexo♦ Jul 26 '12 at 9:58 add a comment| 3 Answers 3 active oldest votes up vote 8 down vote Instead of this: A::A() { mu = PTHREAD_MUTEX_INITIALIZER; http://stackoverflow.com/questions/11666718/pthread-mutex-initializer-inside-c-member-function-cannot-compile //cannot compile } Try this: A::A() { pthread_mutex_init( &(mu), NULL); } The PTHREAD_MUTEX_INITIALIZER is a macro,a C struct initializer for something like {0,0,0,0,0{0}} and can only be used at the point of definition. share|improve this answer answered Jul 26 '12 at 10:03 askmish 4,4811136 add a comment| up vote 7 down vote Use pthread_mutex_init in this case, as the constant is for compile-time initialization. share|improve this answer edited Jul 26 '12 at 10:03 answered Jul 26 '12 at 9:53 Diego Sevilla 20.9k33661 Only for static pthread_mutex_t? –Alcott Jul 26 '12 at 9:57 Then I cannot put pthread_mutex_t data member in the initializtion list, can I? –Alcott Jul 26 '12 at 9:58 Sorry, I meant compile-time initialization, not static initialization. And yes, you cannot put it either (at least if not in C++11) in initialization lists. –Diego Sevilla Jul 26 '12 at 10:03 add a comment| up vote 2 down vote Even if we change this to use an initializer list in the constructor it still fails: #include struct foo { pthread_mutex_t test; foo() : test(PTHREAD_MUTEX_INITIALIZER) {} }; int main() { foo f; } We can see why it fails and an only be used for initialisation in a few contexts by looking at the output from the pre-processsor: struct foo { pthread_mutex_t test; foo() : test({ { 0, 0, 0, 0, 0, { 0 } } }) {} }; It's not legal t

Sign in Pricing Blog Support Search GitHub https://github.com/SnarkyClark/luafcgid/issues/8 This repository Watch 7 Star 48 Fork 18 SnarkyClark/luafcgid Code Issues 11 Pull requests 2 Projects 0 Wiki Pulse Graphs New issue gcc compile error with PTHREAD_MUTEX_INITIALIZER #8 Closed Neopallium opened this Issue Mar 20, 2011 · 2 comments Projects None yet pthread_mutex_initializer compiler Labels None yet Milestone No milestone Assignees No one assigned 2 participants Neopallium commented Mar 20, 2011 gcc is giving a compile are on line 15 of pool.c: pool->mutex = PTHREAD_MUTEX_INITIALIZER; if you change it to: pool->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; it will pthread_mutex_initializer compiler error compile. This maybe an issue with newer versions of gcc. I am using gcc 4.4.5 on linux. Neopallium commented Mar 20, 2011 Another person had this same issue with: gcc version 4.3.2 (Debian 4.3.2-1.1) Owner SnarkyClark commented Mar 29, 2011 This appeared to have changed somewhere between GCC 4.1.2 and 4.3.2. Fix commited. SnarkyClark closed this Mar 29, 2011 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

 

Related content

No related pages.