LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2009, 09:52 AM   #1
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 0
any function for entering critical section?


hi
my question is:
if there are many vars shared between threads for condition checking
such as
if(shared_var1 > 0)
{...}
because other thread may do this at the same time
how to guarantee that threads exclusively access the var

i feel a bit troublesome to define respective mutex for every shared var that may be access by more than one thread. so, is there any function can do this.
 
Old 09-01-2009, 10:39 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ebd View Post
hi
my question is:
if there are many vars shared between threads for condition checking
such as
if(shared_var1 > 0)
{...}
because other thread may do this at the same time
how to guarantee that threads exclusively access the var

i feel a bit troublesome to define respective mutex for every shared var that may be access by more than one thread. so, is there any function can do this.
Don't double-post the same question twice.
 
Old 09-01-2009, 12:55 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
This was the original - I closed the dupe.
 
Old 09-01-2009, 03:15 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
i feel a bit troublesome to define respective mutex for every shared var that may be access by more than one thread. so, is there any function can do this.
1. If you just want a 'critical section' type functionality, then you only need to define a single mutex and use that for all your variables (even define a couple of functions to lock and unlock the mutex). You won't get quite as good a level of concurrency, but if the program doesn't spend much time inside critical sections you will hardly notice a difference using a single mutex vs separate ones.

2. If your problem is speed, then use a spin lock. But it would be rare that you should need these in a user space program.

3. If you are using a language with generic programming facilities like C++, you could make use of a templated type that creates the mutex in the constructor and makes lock/unlock calls for any of the accessor functions that you implement. This also means much neater code when the shared variable is accessed inside a conditional expression.
 
Old 09-01-2009, 10:56 PM   #5
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
if your shared vars are simple numbers you could also use atomic operations, gcc 4.1.x and above supports native atomic operations (not available on all platforms so not completely portable).

http://www.alexonlinux.com/multithre...omic-variables
http://gcc.gnu.org/onlinedocs/gcc-4....-Builtins.html
http://golubenco.org/blog/atomic-operations/
 
Old 09-02-2009, 02:22 AM   #6
shane_kerr
LQ Newbie
 
Registered: Oct 2005
Location: Amsterdam, Netherlands
Posts: 16

Rep: Reputation: 2
Mutexes work for multiple variables too

There is no reason you need to define a mutex for each variable. In fact, if you are working with related variables it is inefficient to do that.

At the very simplest, define one for the entire program.

A more common method is to include a mutex in each structure that you are manipulating. Often this is passed into each thread when it is created via pthread_create().

There may be gcc-specific methods to handle critical sections, but for portable code just use mutexes.
 
Old 09-02-2009, 07:41 AM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Without knowing the details of the OP's requirements I would personally say that using one mutex/synchronisation method for multiple variable is a bad idea ™. You really should only lock for something you need, without blocking anything else from using the other variable which you do not need. CAS could be used but I would only use it as an optimisation and not the default.

Quote:
Don't double-post the same question twice.
So is that four posts ?
 
Old 09-02-2009, 09:13 AM   #8
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Don't double-post the same question twice.
sorry, it was just by mistake
my explorer became busy state the first time i posted
so i stopped the explorer and posted it again, not aware of the post having been posted
 
Old 09-02-2009, 09:14 AM   #9
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
thank neonsignal a lot
your reply is helpful
 
Old 09-02-2009, 09:20 AM   #10
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dmail View Post
Without knowing the details of the OP's requirements I would personally say that using one mutex/synchronisation method for multiple variable is a bad idea ™. You really should only lock for something you need, without blocking anything else from using the other variable which you do not need. CAS could be used but I would only use it as an optimisation and not the default.


So is that four posts ?
what do you mean by CAS? I'm a newbie
 
Old 09-02-2009, 09:32 AM   #11
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by estabroo View Post
if your shared vars are simple numbers you could also use atomic operations, gcc 4.1.x and above supports native atomic operations (not available on all platforms so not completely portable).

http://www.alexonlinux.com/multithre...omic-variables
http://gcc.gnu.org/onlinedocs/gcc-4....-Builtins.html
http://golubenco.org/blog/atomic-operations/
your post is really professional, thanks
 
Old 09-02-2009, 10:20 AM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by ebd View Post
what do you mean by CAS? I'm a newbie
I have not read the links posted by estabroo (well not just now anyway) yet I would find it hard to believe they do not use the term CAS.
 
Old 09-03-2009, 12:08 AM   #13
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
dmail - They used compare and exchange instead of compare and swap. I'm guessing that the x86 asm op for it is cmpxchg.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to enter critical section in Linux Enviroment ebd Programming 2 09-01-2009 10:24 AM
Security of process’s data inside Critical Section(in locking) heroma Programming 1 06-12-2008 04:12 AM
Security of process’s data inside Critical Section(in locking) heroma General 1 06-11-2008 10:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration