design query: where to lock mutex for function accessing global value?


 
Thread Tools Search this Thread
Top Forums Programming design query: where to lock mutex for function accessing global value?
# 1  
Old 03-19-2012
design query: where to lock mutex for function accessing global value?

Hi,
Suppose I have a function that accesses and increments a global variable . This function is run as part of thread. One locks mutex in the function and unlocks it after the processing is done. Is there any alternative way? Thanks in advance.
# 2  
Old 03-19-2012
Depends how you use the variable. If you have one writer and many readers, you could try a reader-writer lock; readers will be able to share it simultaneously, but writers will get an exclusive lock first.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error during Accessing Global variable inside function

emailid=myemail@xyz.com taskName="DB-Backup" starttime=`date` email() { subject="$taskName" ": " $* " at `date` " mutt -s "$subject" $emailid < /dev/null } email "Starting" #do my stuff email "Finished" The above code gives following error ./dbbackup.sh: line 6: :... (5 Replies)
Discussion started by: nitiraj.rathore
5 Replies

2. Solaris

Accessing global-zone installed application

Hi, Is it possible to access application installed on global-zone from a non-global zone? Is there any configuration to achieve the above requirement? Tried looking up information but unable to find. Thanks in advance. Eugene (3 Replies)
Discussion started by: srage
3 Replies

3. Shell Programming and Scripting

Function ignoring global variables

Hi there. I'm writing a function to which I want to pass a global variable. For some reason, it's ignoring the variable. #!/bin/bash ##################################### #Variable Declaration ##################################### CURPATH=`dirname $0` DEEP=$CURPATH/depth.txt export... (4 Replies)
Discussion started by: mikesimone
4 Replies

4. Shell Programming and Scripting

Accessing variables outside the function

I have the below code to find yesterdays date, In this I want to make MONTH, DAY and YEAR as global variableand use it outside the {}, but I am unable to do so , please assist: #!/usr/bin/ksh date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - $1` case "$DAY" in 0) ... (1 Reply)
Discussion started by: mohsin.quazi
1 Replies

5. Programming

Mutex lock question

Hi all, I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation. Below is the summary of my code : MUTEX LOCK performTask(); MUTEX UNLOCK. In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies

6. UNIX for Dummies Questions & Answers

Accessing Global Env Variable

Greetings: I need to remove 'RUBYOPT' env variable to install MacRuby. I see it via $env (tchrc). I checked my (local) .tcshrc, .login, .profile files: not defined there. Apparently, it's not set locally. I know this RUBYOPT is global, since I can see it in another account on my... (4 Replies)
Discussion started by: UncleRic
4 Replies

7. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

8. Shell Programming and Scripting

accessing a function in ksh from perl

is it possible? Because I know we could use open(A, `abc.ksh`); to access a ksh, but is it possible to access just one (or more) function from the ksh script? (2 Replies)
Discussion started by: ahtat99
2 Replies

9. Programming

How to handle mutex lock?

Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively. Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time. ie. two Reads can happen simultaneously, but Write can't happen at Read is going on. ... (3 Replies)
Discussion started by: satheeshalle
3 Replies
Login or Register to Ask a Question