Sponsored Content
Top Forums Programming Understanding Condition variables Post 302573539 by jim mcnamara on Monday 14th of November 2011 10:41:49 PM
Old 11-14-2011
I do not understand your confusion very well. Here is a stab at an answer that may help.

pthread_cond_t variables MUST BE USED with separate mutexes. Mutexes do not take on values like 42. Think of cond_t variables as special add-on features for mutexes that take on a value.

See this page, scroll down to condition variables.

Linux Tutorial: POSIX Threads
 

10 More Discussions You Might Find Interesting

1. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

2. UNIX for Dummies Questions & Answers

pthread condition variables and mutexes?

I am trying to understand the exact difference between condition variables and mutexes in thread synchronization ?. I know mutex will control the thread access to shared data and condition variables will be useful for waiting for certain event or condition to occur . But I couldn't understand why... (2 Replies)
Discussion started by: siddaonline
2 Replies

3. Programming

Condition variables

Hi, I am reading through the pthreads tutorial and had a question on the example they have given for condition variables. Here is the code snippet: This is what the thread waiting on the condition variables doing: pthread_mutex_lock(&count_mutex); while (count<COUNT_LIMIT) { <---... (10 Replies)
Discussion started by: the_learner
10 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

7. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

8. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

9. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

10. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies
PTHREAD_COND(3) 					   BSD Library Functions Manual 					   PTHREAD_COND(3)

NAME
pthread_cond -- condition variable interface LIBRARY
POSIX Threads Library (libpthread, -lpthread) SYNOPSIS
#include <pthread.h> int pthread_cond_init(pthread_cond_t * restrict cond, const pthread_condattr_t * restrict attr); int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_wait(pthread_cond_t * restrict cond, pthread_mutex_t * restrict mutex); int pthread_cond_timedwait(pthread_cond_t * restrict cond, pthread_mutex_t * restrict mutex, const struct timespec * restrict abstime); pthread_cond_t cond = PTHREAD_COND_INITIALIZER; DESCRIPTION
Condition variables are intended to be used to communicate changes in the state of data shared between threads. Condition variables are always associated with a mutex to provide synchronized access to the shared data. A single predicate should always be associated with a con- dition variable. The predicate should identify a state of the shared data that must be true before the thread proceeds. The pthread_cond_init() function creates a new condition variable, with attributes specified with attr. If attr is NULL the default attributes are used. The pthread_cond_destroy() function frees the resources allocated by the condition variable cond. The macro PTHREAD_COND_INITIALIZER can be used to initialize a condition variable when it can be statically allocated and the default attributes are appropriate. The effect is similar to calling pthread_cond_init() with attr specified as NULL, except that no error checking is done. The difference between pthread_cond_broadcast() and pthread_cond_signal() is that the former unblocks all threads waiting for the condition variable, whereas the latter blocks only one waiting thread. If no threads are waiting on cond, neither function has any effect. If more than one thread is blocked on a condition variable, the used scheduling policy determines the order in which threads are unblocked. The same mutex used for waiting must be held while calling either function. Although neither function strictly enforces this requirement, undefined behavior may follow if the mutex is not held. The pthread_cond_wait() function atomically blocks the current thread waiting on the condition variable specified by cond, and unlocks the mutex specified by mutex. The pthread_cond_timedwait() function behaves similarly, but unblocks also if the system time reaches the time specified in abstime, represented as struct timespec (see timespec(3)). With both functions the waiting thread unblocks after another thread calls pthread_cond_signal() or pthread_cond_broadcast() with the same condition variable and by holding the same mutex that was associated with cond by either one of the blocking functions. The current thread holds the lock on mutex upon return from either function. Note that a call to pthread_cond_wait() or pthread_cond_timedwait() may wake up spontaneously, without a call to pthread_cond_signal() or pthread_cond_broadcast(). The caller should prepare for this by invoking either function within a predicate loop that tests whether the thread should proceed. As noted, when calling either function that waits on a condition variable, a temporary binding is established between the condition variable cond and the mutex mutex. During this time, the effect of an attempt by any thread to wait on that condition variable using a different mutex is undefined. The same mutex must be held while broadcasting or signaling on cond. Additionally, the same mutex must be used for con- current calls to pthread_cond_wait() and pthread_cond_timedwait(). Only when a condition variable is known to be quiescent may an applica- tion change the mutex associated with it. In this implementation, none of the functions enforce this requirement, but if the mutex is not held or independent mutexes are used the resulting behaviour is undefined. RETURN VALUES
If successful, all functions return zero. Otherwise, an error number will be returned to indicate the error. ERRORS
The pthread_cond_init() function may fail if: [EINVAL] The value specified by attr is invalid. The pthread_cond_destroy() function may fail if: [EBUSY] The variable cond is locked by another thread. [EINVAL] The value specified by cond is invalid. Both pthread_cond_broadcast() and pthread_cond_signal() may fail if: [EINVAL] The value specified by cond is invalid. Both pthread_cond_wait() and pthread_cond_timedwait() may fail if: [EINVAL] The value specified by cond or the value specified by mutex is invalid. [EPERM] The value specified by mutex was not locked in the condition wait. The pthread_cond_timedwait() function may additionally fail if: [ETIMEDOUT] The system time has reached or exceeded the time specified in abstime. SEE ALSO
pthread(3), pthread_barrier(3), pthread_condattr(3), pthread_mutex(3), pthread_rwlock(3), pthread_spin(3) STANDARDS
These functions conform to IEEE Std 1003.1-2001 (``POSIX.1''). BSD
July 8, 2010 BSD
All times are GMT -4. The time now is 09:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy