Results 1 to 4 of 4

Thread: Incrementing value from multiple threads

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9

    Incrementing value from multiple threads

    Hey all, I was wondering about incrementing a value (an int) from different threads and its potential race conditions. More specifically, do I need to use atomic functions to ensure that no two threads attempt to increment this counter at the same time or do I even need to be worried about this?
    Thanks!
    Chris

  2. #2
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    Re: Incrementing value from multiple threads

    Atomics will make sure that threads that simultaneously increment the value don't step on each other. (e.g., thread one will not read 13 at the same time thread 2 reads 13 and they both write back 14, instead of 15 which is what you get with two increments.) Atomics will not make sure they don't simultaneously increment the value. You do need to worry about that if your algorithm depends on the threads doing different things to different pieces of data.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3

    Re: Incrementing value from multiple threads

    As a follow-up, does anybody know roughly how expensive these atomic operations are in the underlying hardware? Are they relatively cheap (comparable to a regular add/increment/etc.), or expensive enough to only be used when absolutely necessary?

  4. #4
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    Re: Incrementing value from multiple threads

    The cost depends on the hardware, unfortunately. My understanding is that in Nvidia's Fermi they are "a lot cheaper" than what they were before, which was incredibly expensive. In general they should be several times slower than a non-atomic operation. You should be able to measure this quite easily, but my guess would be between 5 and 50 times slower.

Similar Threads

  1. How can we use same VGPath in multiple threads
    By srastogi in forum OpenVG and VGU
    Replies: 3
    Last Post: 09-17-2009, 08:48 AM
  2. OpenGL ES calls across multiple threads
    By hbar in forum OpenGL ES general technical discussions
    Replies: 6
    Last Post: 01-15-2009, 04:54 AM

Posting Permissions

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