Professional Web Applications Themes

Thread-safe array access in Carbon? - Mac Programming

I'm working on an application for doing USB I/O. The main problem I'm having right now is that there's an input buffer that's being written to asynchronously by a callback routine whenever USB input is being detected (ReadPipeAsync()). I'm also trying to read the contents of this input buffer and copy it into a string or other array for use after input is detected. I'm running into problems where the callback routine is trying to write to the array at the same time as another method is trying to read it causing an access fault. I know how to fix ...

  1. #1

    Default Thread-safe array access in Carbon?

    I'm working on an application for doing USB I/O.

    The main problem I'm having right now is that there's an input buffer
    that's being written to asynchronously by a callback routine whenever
    USB input is being detected (ReadPipeAsync()).

    I'm also trying to read the contents of this input buffer and copy it
    into a string or other array for use after input is detected.

    I'm running into problems where the callback routine is trying to write
    to the array at the same time as another method is trying to read it
    causing an access fault.

    I know how to fix this type of competition in Java by using the keyword
    "synchronized" on the methods which read and write the array, but I'm
    not sure how to make this type of thing work in Carbon, nor how to pause
    a thread and say "wait here until the other method is done".

    Can anyone either explain it to me or point me at a resource which
    explains it?

    Many thanks.

    CT
    Charles Guest

  2. #2

    Default Re: Thread-safe array access in Carbon?

    In article <doit.wisc.edu>,
    Charles Thomas <facstaff.wisc.edu> wrote:
     

    What you're looking for is called a mutex, for "mutual exclusion". It's
    just like Java's synchronized keyword; it allows only one thread to
    access a particular thing at a time. Carbon threading is handled by the
    somewhat strangely-named Multiprocessing Services, which provides
    semaphores but not mutexes. Semaphores are more general than mutexs, so
    that's no problem. MP Services also provides a thread-safe message queue
    which you could use to replace your array. You can get to the relevant
    docs here:

    http://developer.apple.com/doentation/Carbon/Reference/Multiprocessing
    _Services/index.html

    You could also skip Carbon and go straight to pthreads, using
    pthread_mutex_* functions.

    All this assumes you're running on OS X. If you're using OS 9, then your
    callback routine is most likely not running in a thread at all, but in
    an interrupt, and using a mutex for that would be disastrous. I'm not
    sure what the strategy is for that situation, but if you need it
    hopefully somebody else can chip in.
    Michael Guest

  3. #3

    Default Re: Thread-safe array access in Carbon?

    In article <mail-D04C39.11190522012004localhost>,
    Michael Ash <com> wrote:
     

    Thanks for the help, Michael. I'll take a look at the docs you
    recommend.

    This is only running on OSX, but is a Carbonized conversion from an
    older OS7-era application.

    But I'm converting it over to run fully with RunApplicationEventLoop(),
    so it will be exclusively an OSX application.

    CT
    Charles Guest

  4. #4

    Default Re: Thread-safe array access in Carbon?

    In article <mail-D04C39.11190522012004localhost>,
    Michael Ash <com> wrote:
     

    boost.org has interesting C++ support for thread synchronization, and
    there was an article in C/C++ Users' Journal on using the "volatile"
    keyword to get the compiler to automaticly generate waiting on mutexes
    only when it was needed.
    David Guest

  5. #5

    Default Another Carbon Thread Question

    If I'm using Carbon OSX and using RunApplicationEventLoop() and
    ReadPipeAsync(), what is the best way to pause the application to wait
    for input?

    What I'd like to do is (pseudocode):

    AskUSBDeviceForCurrentPosition();

    WaitAroundUntilUSBDeviceAnswers();

    ProcessUSBInputToGetCurrentPosition();


    How best would I implement the WaitUntilUSBDeviceAnswers() method in
    Carbon? I know that just sitting around is frowned upon, but wouldn't
    YieldToAnyThread() just lock things up so the USB device wouldn't be
    able to answer?

    CT
    Charles Guest

  6. #6

    Default Re: Another Carbon Thread Question

    In article <doit.wisc.edu>,
    Charles Thomas <facstaff.wisc.edu> wrote:
     

    Well, you don't generally pause your application to wait for input in
    Carbon. It doesn't really work that way. If you don't have any events
    to handle, your event handlers simply won't get called. If you want to
    ensure that there aren't any UI events to process, put up a modal dialog
    with an indeterminate progress bar or something like that.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

Similar Threads

  1. #40098 [NEW]: php_fopen_primary_script() not thread safe
    By wharmby at uk dot ibm dot com in forum PHP Bugs
    Replies: 7
    Last Post: January 12th, 02:35 PM
  2. Replies: 0
    Last Post: January 11th, 12:03 PM
  3. Thread-safe Code
    By Erik J Sawyer in forum ASP.NET Web Services
    Replies: 2
    Last Post: October 3rd, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139