Condition variables


 
Thread Tools Search this Thread
Top Forums Programming Condition variables
# 8  
Old 12-08-2010
I don't get it. How can you possibly use a cond variable if you have no way of knowing if what you did ever worked?
# 9  
Old 12-08-2010
Quote:
Why is the check for count in the while loop and not an if ?

From what I see, the only advantage of it being in the while loop is if by any chance the signaling thread incorrectly signaled the waiting thread (i.e. it sent a signal even though the value of count was still < COUNT_LIMIT). But in that case, I would expect the code to be something like this:
AFAICS, at the time where the watch_count() function in thread T0 is executed, count might already be > COUNT_LIMIT. Right?
# 10  
Old 12-08-2010
Quote:
I don't get it. How can you possibly use a cond variable if you have no way of knowing if what you did ever worked?
signals-based Pthread implementations work, you just have to always handle the spurious signal case.

As an aside, Pthreads implementations on Microsoft Windows suffer less from this particular issue because they are based on the Windows eventing model and have the WaitForSingleObject and WaitForMultipleObject APIs.
# 11  
Old 12-09-2010
Hi the_learner,

Quote:
From what I see, the only advantage of it being in the while loop is if by any chance the signaling thread incorrectly signaled the waiting thread (i.e. it sent a signal even though the value of count was still < COUNT_LIMIT). But in that case, I would expect the code to be something like this:

Code:
    
pthread_mutex_lock(&count_mutex);
while (count<COUNT_LIMIT) { 
    pthread_cond_wait(&count_threshold_cv, &count_mutex);
 }
count += 125; <--- I moved this outside of 'while'

pthread_mutex_unlock(&count_mutex);
Im probably missing something.
You are right: the construct is strange *if* you want to protect again spurious wake-up. Indeed, after waking up (falsely), It would add 125 to count and consequently leave the while() loop. In the spurious case, you usually want to pthread_cond_wait() again.

From the code and comment, we have no chance to know what the intent of the author's code was... Perhaps he wanted for teaching purpose to print the value after waking from the condition; increments it to show that only the thread is active (because it holds the mutex) and terminate the program regardless the count value... if so this should be clearly indicated. From a pedagogical standpoint, this code isn't IMHO good.

Hope you got enough information, to answer your initial question. If you're lost, we can write you a short summary Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. Programming

Understanding Condition variables

I am trying the understand the conditional variable concept. I went through the following site: https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables I understand that condition variables allow threads to synchronize based upon the VALUE of data. When the data acheives a particular... (2 Replies)
Discussion started by: rupeshkp728
2 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question