Home > pthread mutex initializer syntax > pthread_mutex_initializer syntax error

Pthread_mutex_initializer Syntax 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 pthread_mutex_init example site About Us Learn more about Stack Overflow the company Business Learn more

Pthread_mutex_init Attributes

about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x pthread_mutex_init recursive 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 C pthread pthread_mutex_init man mutex: Expected expression before `{' up vote 2 down vote favorite I am using pthread library to create two threads. I am using two queues to communicate the data between the two threads (producer-consumer thing) and hence want to have a mutex to sync the push-pops in the queue by the threads. The problem I am facing is, I get a compile error as

Pthread_cond_init

follows: $ gcc simple-tun.c simple-tun -lpthread simple-tun.c: In function ‘new_queue’: simple-tun.c:920:13: error: expected expression before ‘{’ token The the function where I get the error is: 908 struct queue * new_queue () { 909 910 struct queue * q; 911 q = (struct queue *) malloc (sizeof(struct queue)); 912 913 if (q == NULL) 914 return NULL; 915 916 917 q->head = NULL; 918 q->tail = NULL; 919 q->is_empty = 1; 920 q->mutex = PTHREAD_MUTEX_INITIALIZER; 921 922 return q; 923 } structure queue is: struct queue { 80 struct node * head; 81 struct node * tail; 82 int is_empty; 83 pthread_mutex_t mutex; 84 }; If I comment out the line 920, the linker starts giving 'multiple declaration errors' $ gcc simple-tun.c simple-tun -lpthread simple-tun: In function `settun': (.text+0x2b7): multiple definition of `settun' /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb): first defined here simple-tun: In function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here simple-tun: In function `mktun': (.text+0x1e2): multiple definition of `mktun' /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6): first defined here simple-tun: In function `net_connect': (.text+0xe27): multiple definition of `net_connect' /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115): first defined here simple-tun: In function `data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here simple-t

the interface may not be implemented on Linux. Name pthread_mutex_destroy, pthread_mutex_init - destroy and initialize a mutex Synopsis #include https://linux.die.net/man/3/pthread_mutex_init int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html *restrict attr); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Description The pthread_mutex_destroy() function shall destroy the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialized. An implementation may cause pthread_mutex_destroy() to set the object referenced by mutex to an invalid value. A destroyed pthread_mutex_initializer syntax mutex object can be reinitialized using pthread_mutex_init(); the results of otherwise referencing the object after it has been destroyed are undefined. It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behavior. The pthread_mutex_init() function shall initialize the mutex referenced by mutex with pthread_mutex_initializer syntax error attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect shall be the same as passing the address of a default mutex attributes object. Upon successful initialization, the state of the mutex becomes initialized and unlocked. Only mutex itself may be used for performing synchronization. The result of referring to copies of mutex in calls to pthread_mutex_lock(), pthread_mutex_trylock(), pthread_mutex_unlock(), and pthread_mutex_destroy() is undefined. Attempting to initialize an already initialized mutex results in undefined behavior. In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed. Return Value If successful, the pthread_mutex_destroy() and pthread_mutex_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error. The [EBUSY] and [EINVAL] error checks, if implemented, act

const pthread_mutexattr_t *attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; DESCRIPTION The pthread_mutex_init() function initialises the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect is the same as passing the address of a default mutex attributes object. Upon successful initialisation, the state of the mutex becomes initialised and unlocked. Attempting to initialise an already initialised mutex results in undefined behaviour. The pthread_mutex_destroy() function destroys the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialised. An implementation may cause pthread_mutex_destroy() to set the object referenced by mutex to an invalid value. A destroyed mutex object can be re-initialised using pthread_mutex_init(); the results of otherwise referencing the object after it has been destroyed are undefined. It is safe to destroy an initialised mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behaviour. In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialise mutexes that are statically allocated. The effect is equivalent to dynamic initialisation by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed. RETURN VALUE If successful, the pthread_mutex_init() and pthread_mutex_destroy() functions return zero. Otherwise, an error number is returned to indicate the error. The [EBUSY] and [EINVAL] error checks, if implemented, act as if they were performed immediately at the beginning of processing for the function and cause an error return prior to modifying the state of the mutex sp

 

Related content

No related pages.