LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 09-17-2015, 01:18 PM   #1
YehudaSinger
Member
 
Registered: Jun 2013
Posts: 30

Rep: Reputation: Disabled
Mutex handling


Dear sir/Mrs.
I have a program with 27 Mutexes.
all mutexes are created with the same sequence:
1. Setting an atribute using pthread_mutexattr_setpshared funtion with the PTHREAD_MUTEX_RECURSIVE_NP.
2. Initialization of the Mutex using the pthread_mutex_init.

Except one mutex, 26 times, the process is OK.
for one mutex initialization, I get the error code 95.
Of course, later, any LOCK/UNLOCK operation fails with the error code 130 and 131 respectively.

We use the Red-Hat OS.

Thanks in advance.
Yehuda
 
Old 09-18-2015, 09:22 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I see a big design-problem in the idea of "27 mutexes." (Could you simply be running out of them?)

Even if your application really does have 27 distinct flows-of-control that really can be used in parallel, pragmatically speaking you don't need to have a distinct mutex for each one. You can "group" them as you see fit. That is to say, "in order to do this, or, that, or that, you need to hold 'mutex #1.'" (You make this decision "by agreement," and follow it carefully as you write the code.)

Mutexes are sometimes also arranged in a hierarchy (again, "by agreement"), so that you must never try to grab "this" mutex unless you are holding "that" one.

Consider, as an example, the locking strategy that is used within the Linux kernel. Although "the kernel, of course, is different from user-land," and must deal with a more complex situation overall, the design ideas still generally hold.

Finally, I find that the actual number of mutexes needed is quite small ... usually one, maybe two or three. The more elaborate you try to get in your mutual-exclusion design, the (much) greater becomes the risk of deadlock, race conditions, inefficiency, and so on. It's probably better to allow a thread to have to wait a few microseconds longer, if it keeps your overall design simple. The number "27" tells me: change your design.

Last edited by sundialsvcs; 09-18-2015 at 09:25 AM.
 
1 members found this post helpful.
  


Reply

Tags
kernel 2.6.35



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
Mutex Unlock pratham29 Linux - Newbie 1 11-19-2013 01:46 PM
Priority in mutex manohar Programming 2 02-04-2011 03:21 PM
locks , mutex sha_neb Programming 2 02-02-2011 10:20 AM
Why does pthread_cond_wait need a mutex? fuzzyBuzz Programming 4 06-01-2009 02:16 PM
Mutex new2lunix Programming 1 12-02-2008 08:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:07 PM.

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