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?
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.
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:
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
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)
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)
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)
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)
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)
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)
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)
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)
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)