Modifier and Accessor Synchornization

Hi,
This is not what you may think -- it is not about getter and setters, allow me to elaborate. I have a data structure that requires a few modifications to the underlying data and many accesses (M threads add/remove, N threads access).
To achieve concurrency, I am adding two minimalistic locks to synchronize between two thread types. One for when the data structure goes under modification phase (modifyingLock) and one for the count of threads currently accessing the data structure (threadCount).
The premise is allow modifying threads to make modification to data when there is no thread accessing (reading) it. Therefore, it must wait until all the accessing threads are done by verifying whether threadCount is zero or not. Ergo, Access threads can be in two states, either in process of accessing the data or just about to begin searching. In case of former, the accessing thread should be allowed to finish it search and at the end decrement threadCount. If the thread of type N haven't begun accessing the data and see modifyingLock set, they should wait until modifications are perform to data and get notified upon completion. The process should work as following:
Modifying threads (M),
- Before making any modification, M must check to see whether modifyingLock as been set which is an indication another M thread is (OR about to) making modification to the data structure and also check whether any thread of type N is accessing it.
- If either condition is not met, threads of type M must wait until either another thread of type M finishes its task of modification and/or accessing threads of type N are flushed and done with their search (threadCount should be zero).
Accessing threads (N),
- If modifyingLock is not set, they can began accessing the data structure safely knowing thread of type M would not begin its task of modification until all the accessing threads are done.
- And any further new access would be blocked and have to wait until thread M's are done with their task.
Sounds logical but when it comes to the actual implementation, I can clearly see that there are many possible thread locks which I cannot seem to figure how to remedy:
// Partial code of add() method
// modifyingLock is a byte[] of size one
// threadAccessCount is an int[] of size one
    synchronized (modifyingLock) {
        while (modifyingLock[0] != 0 &&
                threadAccessCount[0] != 0) {
          try {
            modifyingLock.wait();
          } catch (InterruptedException ie) {         
        } // end of while loop
        modifyingLock[0] = (byte) 1;
        // Actual modification to the data structure
        modifyingLock[0] = (byte) 0;
        modifyingLock.notifyAll();
    } // end of synchronized block
// Partial implementation of accessing method
    synchronized (modifyingLock) {
      while (modifyingLock[0] != 0) {
        try {
          modifyingLock.wait();
        } catch (InterruptedException ie) {         
      } // end of while loop  
    } // end of synchronized block
     synchronized (threadAccessCount) {
       threadAccessCount[0]++;
    // Actual search or read task
    synchronized (threadAccessCount) {
      threadAccessCount[0]--;
      if (threadAccessCount[0] == 0) {
        synchronized (modifyingLock) {
          modifyingLock.notifyAll();
    }Now my question, can you come up with any scenario where a deadlock or race condition can occur?
Thank You

Maxim_Karvonen,
Thank you for the keen eyes; I really didn't except anyone to read through the whole thing. I can swear I had made changes on my last reply that should reflect many of the issues you pointed out. I don't know why my last post does not reflect these changes. In any case, let me recap what the requirement for my problem is as it'll help explain the situation become more pristine.
We have writer and reader threads which both work on the same set of data structure. Since there is a mutual underlying data involved, I was hoping to come up with a mechanism to basically flush out the "reader" threads as soon as a "writer" thread appears for a modification to such data structure. And by flush, I am not saying to terminate the search in the middle of work but rather allow the "reader" threads to finish their task but any "new" coming "reader" AND "writer" thread must block and wait for the first "writer" thread to perform its duty.
The reason reader and writer threads may go head to head is because the modification methods (i.e. add() and remove()) both take advantage of the accessing method (i.e. contain()) to make sure an entry/node is present in the data structure before proceeding to the actual job. So as you can see, both reader and writer threads call upon contain() method at some point. Therefore, in the design, I have to make sure that all of its content only be available to a single "writer" thread.
Since my last reply, I managed (hopefully) to accomplish this to some extend. But soon after, I realized that my design would continuously only allow the "writer" threads to obtain the locks IF there is a writer thread present in the queue at any given time.
For instance, if I have 50 reader threads in the middle of contain() and 5 writer threads, waiting to be processed, all 50 reader threads must finish their task and exit. Meanwhile any incoming reader (or/and writer thread) must be blocked. Once all 50 reader threads egressed, one of the 5 (or + any incoming writer thread) writer thread is selected and given a way to perform its task.
Now, we can have two scenarios, one being to continue blocking all the incoming reader threads (which can add up to dozens by the time a writer thread finishes) due to the fact that we still have other writer threads. Secondly, allow a contention among reader and writer threads and see who wins. BUT... Due to my design, if a reader wins, then all other readers must be processed and their counter decremented to zero so the writer threads can be given a green flag for operation. And since number of reader threads pretty much dominates throughout the life cycle of the application, it will be a long epoch until any of the writer threads come to life.
So to combat this, I introduced a reader/writer ratio. Every time a writer performs its task, right before it exits, it checks to see whether a threshold has been reached. If not, it gives priority to another writer thread to continue executing until number of reader to writer threads passes such ratio. In that case, the ball would be in reader threads' court. So this is what I came up with so far:
private byte[] modifyingLock; // An indication that a writer thread "wanting" to do some work
private int[] accessThreadCount; // Number of "reader" threads
private short modificationThreadCount; // Number of "writer" threads
private double accessToModifyingThreadRatio; // The ratio of reader to writer (usually 10 to 1)
// add() method
synchorinzed (modifyingLock) {
  modificationThreadCount++;
  // An indication to all "reader" threads that a "writer" or "writers"
  // thread(S) has joined a party and everyone needs to clear out a way
  modifyingLock[0] = 1; /* (1.1) */
  while (accessThreadCount[0] != 0) {
    try {
      modifyingLock.wait();
    } catch (InterruptedException ie) {         
  } // end of while loop
  // We have to reset the lock's value because add() needs to call on
  // contain() and if we leave the lock's value on line "1.1", then
  // a writer thread that calls contain() would also go on a block
  // and create a deadlock
  modifyingLock[0] = 0; /* (1.2) */
  // If the item is already in the data structure, just return. But before
  // doing so, a writer thread needs to do some cleaning and synchronization
  if (/* contain() */) {
    // As we discussed, if the "current" writing thread leave this lock's value
    // unset, this would give the "reader" threads a way to execute without
    // giving a chance to "writer" threads.
    // This is due to the fact that the only way reader threads "know" whether
    // a writer thread is about to do some work is to have this lock's value
    // set which lets the incoming reader threads know that they should block
    // ... for now.
    // If ratio of reader threads to writer thread exceeds a certain number,
    // then the lock's value is left unset so now, "reader" threads can execute
    // their task. Otherwise, we let the next possible "writer" thread to take on.
    // This is more like a writer thread vouching for another writer thread
    // comrade.
    if (/* threshold is NOT reached */) {
      modifyingLock[0] = 1;
    modificationThreadCount--;
    modifyingLock.notifyAll();
    return;
  /****** Do the actual insertion *******/
  // This is the end of the line for the writer thread so it has to perform
  // the same set of procedures as I just described above. I could always
  // modularize it to avoid code duplication but we leave it like that for now.
  if (/* threshold is NOT reached */) {
    modifyingLock[0] = 1;
  modificationThreadCount--;
  modifyingLock.notifyAll();
// contain() method
synchronized (accessThreadCount) {
  accessThreadCount[0]++;
// If the modifyingLock is set, this is an indication that a writer
// thread is about to make a modification and all the incoming reader
// threads (those which have not begun accessing the underlying data
// structure) would have to block
// A writer thread that gets to this point has already acquired a lock
// so it follows through and also has the modifyingLock reset at line
// 1.2.
synchronized (modifyingLock) { /* (2.1) */
  while (modifyingLock[0] == 1) {
    try {
      modifyingLock.wait(); /* (2.x) */
    } catch (InterruptedException ie) {
  } // end of while loop
} end of synchronized block
/******* Do the actual search *******/
// If this is a reader thread, try to acquire a lock on the object,
// decrement the "reader" thread counter. If the counter has reached
// zero, automatically call upon any waiting thread on the lock.
// The notification is only good under two conditions:
//   - Only "reader" threads been performing and a writer(s) is on
//     a waiting list.
//   - Other reader threads that have been blocked on line 2.1
synchronized (modifyingLock) {
  synchronized (accessThreadCount) {
    accessThreadCount[0]--;
    if (accessThreadCount[0] == 0) { /* (2.4) */
      modifyingLock.notifyAll();
  } // end of synchronized (accessThreadCount)
} // end of synchronized (modifyingLock)
return /* base on the outcome of search method above (2.2) */
///////////////////////////////////////////////////////////////////////////Ok, so this might not be the most crisp design for such requirements but I think it can hold its own. One concern that I have is that during the work flow of a "writer" thread, when it is about to exit contain(), it "might" call on notifyAll() once here, and another time at the bottom of add(). Would that be a problem? Note that the lock on the modifyingLock is still held by the writer thread up to the end of add().
Another tiny problem that I can think of is when contain() method returns at line (2.2). This is a clear case of unsafe "compound" actions which can result in some reader getting all the way down to line (2.2) and a context switch occurs. As a result, a writer thread begins working (and ultimately exits). In that case, a return value of true may well be "false!" due to alteration done to the data structure. This could leave a client of reader thread very unhappy. Although, this scenario can be very unlikely, it could happen and I do not know how to resolve it.
I still have to think about a few pointers. Maxim_Karvonen, I would like to extend my gratitude for your participation in this discussion. If it wasn't for your reply, I wouldn't have cared to post this latest response. I know this might not be the end so I keep digging.
Thank You

Similar Messages

  • What does this message mean? "Warning: SUID file "System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent" has been modified and will not be repaired"

    What does this message mean? "Warning: SUID file "System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent" has been modified and will not be repaired"  I get this when I try to repair permissions in disk utility.

    It's an innocuous error you can ignore. It was introduced with previous updates for ARD and/or Java before Lion. These are messages, not errors. They will cause no harm, and they will be repeated each time you repair permissions.

  • I have been transfering info to an external hard drive and when I try and trash contents off my computor i get the message it will not continue with the message SUID file syst/library/coreS has been modified and will not be repaired. Hence I cannot free u

    I have been transfering photos and movies to an external hard drive and when I try and secure trash contents off my computor i get the message it will not continue In utilities  the message is: SUID file syst/library/coreS has been modified and will not be repaired. Hence I cannot free up space on my computor. What can i do?

    Do not worry. It is probably a permission you should ignore, but you should check that the permission appears in this site > http://support.apple.com/kb/TS1448?viewlocale=en_US&locale=en_US

  • Permissions repair: SUID file system/library/coreservices/remotemanagement/ARDAgent/contents/MacOS/ARDAgent has been modified and will not be repaired

    I have a mid 2007 iMac running OS Mavericks and the hard drive is failing. When doing a disk permissions verify it shows  SUID file system/library/coreservices/remotemanagement/ARDAgent/contents/MacOS/ARDAgent has been modified and will not be repaired. I do not have a backup of my system due to my external drive I use for backing up my system has stopped working. Can I repair this myself without having to reinstall the OS so I don't loose my any of my files.

    You can safely ignore that.
    Disk Utility's Repair Disk Permissions messages you can safely ignore

  • Moving photos, some modified and some original to external hard drive

    I have about 3000 photos that I would like to move either to my external hard drive or DVD to free up some space. My dilemma is that I would like only to have one of each picture. What I mean by this is that if I have modified a photo it seems that both the modified and the original are copied. I don't need duplicates. But if I haven't modified the picture I would need the orignal to copy. Thoughts? Thanks.

    Hi, bows4474. If you select pictures in your library and Export them to a folder on your desktop or elsewhere on your hard drive — being sure to select Original Size in the Export dialog box — then that folder will contain the modified versions of pictures you've modified and the original versions of pictures you haven't modified. It won't contain the originals of pictures you've modified. It also won't be an iPhoto Library database folder, so it won't contain any thumbnails, albums, film rolls, books, slideshows, etc. that were created in iPhoto — it will simply be a folder full of image files. You can then burn it to CD or DVD, and you won't need iPhoto to access and open the files on any Mac or PC. But they won't be organized as you organized them in iPhoto, and if any of them have the same names, you won't be able to put same-named images in the same folder without one of them replacing the other(s).
    To get all the pictures back into iPhoto at any later time, you'd probably want to create a brand new iPhoto Library and import them into it. Importing them back into the same library you exported them from would duplicate every one there, creating real havoc.

  • Cases and Accessories for 5G iPods

    Ok, so ever since I purchased my Black 60GB iPod I started reading about how easily it scratched and realized I needed to get a case for it immediately. I was also surprised to discover that my old iTrip and other top docking accessories no longer worked with the 5G iPods. So I began to research my options for cases and accessories, and here's what I came across:
    This is a silicone case from zCover that seems to be the only 5G specific case that is available and shipping now.
    http://www.zcover.com/zCoverProduct_iPod_G5Original.htm
    Here are 6 different options for cases offered by Speck sometime in mid-November.
    http://www.speckproducts.com/for-video.html
    Tunewear has several different options available for the 5G's. The silicone case (ICEWEAR 5G) can be found here: http://www.tunewear.com/english/product/icewear_5g/index.html
    The wallet-sized leather case (PRIE TUNEWALLET 5G) can be found here: http://www.tunewear.com/english/product/tunewallet_5g/
    And finally the leather case with belt clip and hook (PRIE Ambassador) can be found here: http://www.tunewear.com/english/product/ambassadoripod5g/index.html
    All of these cases are shipping sometime in November and all come included with Tunewear's TUNEFILM 5G which can also be purchased separately, found here: http://www.tunewear.com/english/product/tunefilm_5g/index.html
    Another leather case option is being offered by difusi. Here is their VideoValet case, stated to be shipping sometime this week: http://www.difusi.tv/hardware/casemini/videovalet.html
    Since most of these cases haven't been released yet, I decided to fashion my own case for the time being. This option will probably only work for those of you who purchased the 60 GB iPods, because the 30GB 5G's are significantly thinner than their 20GB and 30GB 3G and 4G counterparts. But anyway, I purchased a cheap silicone case for a 20GB iPod from Best Buy (you can probably find one anywhere that sells iPod accessories for around $10-$15) and a pack of PDA screen protectors. Since the height and width of the new iPods are exactly the same (the thickness or depth of the new 60GB is close enough to the 20GB and 30GB 4G's) and the click wheel and dock connector seem to be in the same place, it was only the screen size that needed to be adjusted. So I cut the case to make the entire screen visible, used a small amount of heat with a lighter to melt the cut edges firm, and then cut a screen protector to fit. This is a crude option, but it has worked for me so far and I don't have a scratch on it yet.
    As for other accessories available, allabouttheaccessories.com has nearly all accessories that are currently available and compatible with the 5G video capable iPods listed here: http://www.allabouttheaccessories.com/index.asp?PageAction=VIEWCATS&Category=193
    As most of you know, the current incarnations of Griffin's t fit in the older case. Obviously the edges of the iTrip, perhaps the most popular FM transmitter for the iPod, are not compatible with the new 5G iPods. For some reason, Apple did away with the external power port on top of the new iPods, and therefore it is unlikely that there will be any accessories, including iTrip, iTalk, remotes, etc. that will dock on top of the iPod. Instead, most will use the dock connector port on the bottom of the iPod, which isn't all bad and actually offers some benefits. Griffin's new iTrip that will use this port can be found here: http://www.griffintechnology.com/products/itrip30/techspecs.php And with a simple mini USB to standard USB cable, you can charge your iPod using a number of car chargers that use the USB cable (including Griffin's PowerJolt of course).
    Anyway, hope this post helps those of you searching for new stuff for your 5G iPods. Despite the criticism I've read, this device is a huge step for iPods and has a ton of potential. Sure there are a few things that need to get worked out, but the bottom line is they've added all t

    I did a quick search on Best Buy and came up with the following...
    http://www.bestbuy.com/site/searchpage.jsp?_dyncharset=ISO-8859-1&_dynSessConf=&id=pcat17071&type=pa...
    I'm sure you can do the same on Google with the rest of the web.

  • Modify and add new field in standard Purchase Order script

    Hi All,
           I have some problem with Modify and add some new fields in Standard Purchase Order script. From ME22n transaction code it display PO detail. In <b>item detail</b> with <b>condition</b> tab all data will display in Currently PO script output.
           But I want to display ME22n->Item Detail->Invoice->Taxes Data. how to display this data in standard PO script. All data are fetch from <b>Structure</b> like ( KOMV,KOMVD..etc) then How it can be Display?
    Waiting for Replay.
    Himanshu Patel.

    Work with an Abaper.Tell your requirement [ addition of a field] and ask him to include this field by using the functionality " Field Exit".

  • VROPs - Can you create a report to show when a VM was modified and what was changed?

    I am having trouble trying to craft a report/view that can show me what I am looking for.  I figured since this is capacity related vROPs may keep track.  Is there a way to show when a VM was modified and what was modified?  Looking mainly for vdisk, vCPU, or vRAM.
    Thanks for the help or suggestions.

    Hi newbski1,
    There are few things you can try to help with this.
    1. The events view does show you resource changes, it isn't terribly detailed but it will help
    2. Depending on your version you can also look at Configuration Manager which is part of the vRO suite that will show you configurations changes.
    Cheers
    @iiToby

  • MODIFY and SAVE_TRANSACTION not working for ZONE BO

    Hi
    I am trying to add/delete location in transportation zone through MODIFY and SAVE_TRANSACTION but the update is not happening in database. I have done deletions and additions in other business objects but for transportation zone it is not working properly. Is something wrong with BO configuration ?
    Procedure that I am following for deletion of zone:
    1. Read Zone with querybyelements method
    2. Read locations for this zone using retrieve_by_association method in edit mode
    3. Fill modification table with deletion indicator and appropriate values for location
    4. Call method Modify and then save_transaction.
    I followed similar procedure for adding/deleting region in transportation zone but this also doesn't work.
    Thanks
    Anuraag

    Hi Anuraag,
    In TM you should open the transaction /SCMTMS/ZONE. Here you select the zone you would like to modify and you execute the changes you would like to make.
    Regards,
    Nico.

  • When I run an Utilities check on the HD. I always get a prompt-SUID System/Library/CoreS has been modified and will not be repaired. how can i access this to be repaired. Most files are corrupted by messages that they are one thing but should be another

    When I  run a system utilities check and select HD. i always get a prompt the the SUID System/Library/CoreS has been modified and will not be repaired. How can I access this file so that I may repair it? Left unrepaired . There are a lot of files that do not have the correct code. They are one thing but should be another

    Hello there, toestothenose.
    It seems that you may not have gotten the entire message into your thread here, but the following Knowledge Base article offers a good resource on which messages from Disk Utility's Repair Disk Permissions can be safely ignored:
    Mac OS X: Disk Utility's Repair Disk Permissions messages that you can safely ignore
    http://support.apple.com/kb/ts1448
    Thanks for reaching out to Apple Support Communities.
    Cheers,
    Pedro.

  • Since installing Yosemite all my documents say created, modified, and last opened at 7 08

    Since installing Yosemite (10.10.3 on a 2009 iMac) the dates of creation, modification, etc. of documents are shown correctly, but every one was Created, Modified, and Last opened at 7 08. Is my clock going bad? The time on the desktop menu gives the correct time.

    Date/ Time Incorrect in Applications

  • Hi There! I have a question on Kernel Task-Threat. How can I clean my computer? This is the message --Warning: SUID file "System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent" has been modified and will not be repaired.

    Hi There! I have a question on Kernel Task-Threat. How can I clean my computer? This is the message that persists when cleaning disc--Warning: SUID file “System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent” has been modified and will not be repaired.

    That's a routine message and it means nothing. Don't waste your time repairing permissions unless you know you have a problem that can be solved that way, which you probably never will.

  • HT203172 Warning: SUID file "System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent" has been modified and will not be repaired. When I repaired "disk permission", this message was what came up. What does this mean, and how can

    Warning: SUID file “System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/MacOS/ARDAg ent” has been modified and will not be repaired. When I repaired "disk permission", this message was what came up. What does this mean, and how can I fix it?
    The opperating system I am using is 10.7.4, and updating to the latest version currently. Computer is running fine, except that when I did some utility repairs, I got the warning above, and not really sure what do, in regards to repairing it correctly, not just getting the message off of my computer.
    Thank you for your help, and please only contribute if you know about this issue please.

    That's a status message which can be safely ignored.
    (70090)

  • "SUID file modified and will not be repaired"  !?

    I've never seen this before:
    2008-12-26 13:23:19 -0800: Reading permissions database.
    2008-12-26 13:23:19 -0800: Reading the permissions database can take several minutes.
    2008-12-26 13:24:22 -0800: Warning: SUID file "System/Library/Filesystems/AppleShare/afpLoad" has been modified and will not be repaired.
    2008-12-26 13:24:23 -0800: Warning: SUID file "usr/bin/setregion" has been modified and will not be repaired.
    2008-12-26 13:24:30 -0800: Warning: SUID file "System/Library/PrivateFrameworks/Install.framework/Versions/A/Resources/runner " has been modified and will not be repaired.
    2008-12-26 13:24:30 -0800: Warning: SUID file "System/Library/Printers/IOMs/LPRIOM.plugin/Contents/MacOS/LPRIOMHelper" has been modified and will not be repaired.
    2008-12-26 13:24:34 -0800:
    2008-12-26 13:24:34 -0800: Permissions repair complete
    Please advise.

    It's always best to search before posting:
    http://discussions.apple.com/search.jspa?search=Go&q=suid+

  • Read, Modify and Apply Report XML using Java Script

    Hi Guys,
    Is there any way that we can Pragmatically Read, Modify and Apply Report XML using Java Script or some other way.
    Thanks
    Kaushik
    Edited by: Kaushik K on Jun 20, 2012 8:36 PM

    Requirement ::
    Users should be able to add Column to the Report Dynamically at Runtime.
    (There are around 1000+ Users, Answers Approach is not acceptable)
    So we are planning to provide a Multi Select / Shuttle Box Option for Users to add Columns dynamically. (Only for Table View)
    What we planned to DO ::
    Create a Presentation Variable Prompt, Which reads the Metadata Table (Presentation Table.Column Name, populated using the Metadata Dictionary)
    And Create a report with One Column and the Column Fx like @{var_dynamic_columns}{'"Time"."Year","Time"."Month"'}
    With this, OBIEE is rewriting the Logical SQL Currently as "Select "Time"."Year","Time"."Month" from "A - Sample Sales" "
    But getting an error
    The number of columns returned in retrieving column metadata was invalid (expected: 1; received: 2)
    So we want to see, if we can rewrite the Advanced XML of the Report to have dynamic columns based on the Values from the Presentation Variable.
    Please help me if this is a viable solution or any other better solution.

Maybe you are looking for

  • Can someone help me - OS reinstall problems.

    Alright guys, I'm feeling pretty stressed about my situation right now. Here is what happened.... I have a 13' MBP pro that I bought at the start of 2012. It was new then. Months ago I attempted to install Windows via Bootcamp, but it failed. Since t

  • SAP XI 2.0 to 3.0 / 7.0 delta

    Hi XI Gurus I wanted to know the delta between SAP XI 3.0 and 2.0 and delta between SAP XI 7.0 and 3.0. Regards Madhan Doraikannan

  • Oracle 8i/9i ODBC driver for ARM-Linux

    I may need an ODBC driver for Oracle for the arm-linux platform in the near future. Does anyone know if such a thing exists and where to get it? Michael

  • Post Installation Steps in RAC --Urgent

    Hi DBA's Database Version 11.2.0.1 Operating System : Solaris 10 This is first time i am implementing 2 node RAC. I Installed Grid and Database successfully, and my RAC instance working fine. My Concern is, I want to test thoroughly my RAC instance,

  • Title Bar Drop downs

    Has anyone noticed that the title bar drop down menus highlight with an Windows XP blue color that is different than all the blue used in the apps drop downs. The graphite is the same but there is a new (ugly) blue on the drop downs. Or is it just me