ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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).
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.
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.
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
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.