thread lock object.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajikoe@gmail.com

    #1

    thread lock object.

    Hello,

    I have multi thread program running together and each will increment
    int i.

    How can we make only one thread at a time be able to access and
    increment i ?

    Thanks in advance,

    Sincerely Yours,
    pujo

  • Irmen de Jong

    #2
    Re: thread lock object.

    ajikoe@gmail.co m wrote:[color=blue]
    > Hello,
    >
    > I have multi thread program running together and each will increment
    > int i.
    >
    > How can we make only one thread at a time be able to access and
    > increment i ?
    >
    > Thanks in advance,
    >
    > Sincerely Yours,
    > pujo
    >[/color]

    Use a synchronization primitive such as a lock
    (threading.Lock , threading.RLock )

    But for simply incrementing a number (i+=1) this is not needed
    because that operation cannot be interrupted by another thread,
    as far as I know.

    --Irmen

    Comment

    • ajikoe@gmail.com

      #3
      Re: thread lock object.

      thanks.

      Pujo

      Comment

      • Peter Hansen

        #4
        Re: thread lock object.

        Irmen de Jong wrote:[color=blue]
        > ajikoe@gmail.co m wrote:[color=green]
        >> How can we make only one thread at a time be able to access and
        >> increment i ?[/color]
        >
        > Use a synchronization primitive such as a lock
        > (threading.Lock , threading.RLock )
        >
        > But for simply incrementing a number (i+=1) this is not needed
        > because that operation cannot be interrupted by another thread,
        > as far as I know.[/color]

        Most assuredly it can:
        [color=blue][color=green][color=darkred]
        >>> def f():[/color][/color][/color]
        .... global x
        .... x += 1
        ....[color=blue][color=green][color=darkred]
        >>> dis.dis(f)[/color][/color][/color]
        3 0 LOAD_GLOBAL 0 (x)
        3 LOAD_CONST 1 (1)
        6 INPLACE_ADD
        ....a context-change here can lead to incorrect results...
        7 STORE_GLOBAL 0 (x)
        10 LOAD_CONST 0 (None)
        13 RETURN_VALUE

        -Peter

        Comment

        Working...