Do I need a lock here?

Discussion in 'Python' started by jasiu85, Oct 27, 2008.

  1. jasiu85

    jasiu85 Guest

    Hey,

    Please take a look at the code of the two threads below:

    COMMON_DICT = {}

    def thread_1():
    global COMMON_DICT
    local_dict = prepare_dict()
    COMMON_DICT = local_dict

    def thread_2():
    global COMMON_DICT
    local_dict = COMMON_DICT
    use_dict(local_dict)

    Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
    operations are atomic and in each thread there's only one crucial
    bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
    second one. So I suspect that everything will work just fine. Am I
    right?

    Regards,

    Mike
     
    jasiu85, Oct 27, 2008
    #1
    1. Advertising

  2. jasiu85 schrieb:
    > Hey,
    >
    > Please take a look at the code of the two threads below:
    >
    > COMMON_DICT = {}
    >
    > def thread_1():
    > global COMMON_DICT
    > local_dict = prepare_dict()
    > COMMON_DICT = local_dict
    >
    > def thread_2():
    > global COMMON_DICT
    > local_dict = COMMON_DICT
    > use_dict(local_dict)
    >
    > Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
    > operations are atomic and in each thread there's only one crucial
    > bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
    > second one. So I suspect that everything will work just fine. Am I
    > right?


    Depending on what you mean by "right".

    The above is not enough to judge what is really happening. But depending
    on the execution order, thread_1 overwrites the reference in COMMON_DICT
    *after* thread_2 has altered it.

    But it won't crash or anything. If that's right for you.

    Diez
     
    Diez B. Roggisch, Oct 27, 2008
    #2
    1. Advertising

  3. jasiu85

    jasiu85 Guest

    On Oct 27, 10:12 am, "Diez B. Roggisch" <> wrote:
    > jasiu85 schrieb:
    >
    >
    >
    > > Hey,

    >
    > > Please take a look at the code of the two threads below:

    >
    > > COMMON_DICT = {}

    >
    > > def thread_1():
    > >     global COMMON_DICT
    > >     local_dict = prepare_dict()
    > >     COMMON_DICT = local_dict

    >
    > > def thread_2():
    > >     global COMMON_DICT
    > >     local_dict = COMMON_DICT
    > >     use_dict(local_dict)

    >
    > > Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
    > > operations are atomic and in each thread there's only one crucial
    > > bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
    > > second one. So I suspect that everything will work just fine. Am I
    > > right?

    >
    > Depending on what you mean by "right".
    >
    > The above is not enough to judge what is really happening. But depending
    > on the execution order, thread_1 overwrites the reference in COMMON_DICT
    > *after* thread_2 has altered it.
    >
    > But it won't crash or anything. If that's right for you.
    >
    > Diez


    The second thread doesn't alter the dictionary, it only reads it. Even
    if the first thread puts a reference to a new dictionary into the
    COMMON_DICT variable while the second thread is reading the old
    dictionary, such sematics is fine for me. I'm seeking for a kind of
    Producer/Consumer pattern. So the first thread produces some data in a
    form of a dictionary. The second thread reads it from time to time.
    It's sufficient for me if a dictionary is "lost" because the first
    thread overwrites the COMMON_DICT variable before the second thread
    tries to read it. It's also sufficient for me if the first thread
    updates COMMON_DICT variable while the second thread is reading the
    previous copy of the dictionary. So from my point of view the most
    critical parts are these two lines:

    COMMON_DICT = local_dict # in the first thread
    local_dict = COMMON_DICT # in the second thread

    Can these two instructions by any chance interfere with each other in
    a way that will crash either of the threads?

    I hope I made myself a little bit more clear :).

    Thanks!!

    Mike
     
    jasiu85, Oct 28, 2008
    #3
  4. jasiu85

    Aaron Brady Guest

    On Oct 28, 3:29 pm, jasiu85 <> wrote:
    > On Oct 27, 10:12 am, "Diez B. Roggisch" <> wrote:
    >
    >
    >
    > > jasiu85 schrieb:

    >
    > > > Hey,

    >
    > > > Please take a look at the code of the two threads below:

    >
    > > > COMMON_DICT = {}

    >
    > > > def thread_1():
    > > >     global COMMON_DICT
    > > >     local_dict = prepare_dict()
    > > >     COMMON_DICT = local_dict

    >
    > > > def thread_2():
    > > >     global COMMON_DICT
    > > >     local_dict = COMMON_DICT
    > > >     use_dict(local_dict)

    >
    > > > Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
    > > > operations are atomic and in each thread there's only one crucial
    > > > bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
    > > > second one. So I suspect that everything will work just fine. Am I
    > > > right?

    >
    > > Depending on what you mean by "right".

    >
    > > The above is not enough to judge what is really happening. But depending
    > > on the execution order, thread_1 overwrites the reference in COMMON_DICT
    > > *after* thread_2 has altered it.

    >
    > > But it won't crash or anything. If that's right for you.

    >
    > > Diez

    >
    > The second thread doesn't alter the dictionary, it only reads it. Even
    > if the first thread puts a reference to a new dictionary into the
    > COMMON_DICT variable while the second thread is reading the old
    > dictionary, such sematics is fine for me. I'm seeking for a kind of
    > Producer/Consumer pattern. So the first thread produces some data in a
    > form of a dictionary. The second thread reads it from time to time.
    > It's sufficient for me if a dictionary is "lost" because the first
    > thread overwrites the COMMON_DICT variable before the second thread
    > tries to read it. It's also sufficient for me if the first thread
    > updates COMMON_DICT variable while the second thread is reading the
    > previous copy of the dictionary. So from my point of view the most
    > critical parts are these two lines:
    >
    > COMMON_DICT = local_dict # in the first thread
    > local_dict = COMMON_DICT # in the second thread
    >
    > Can these two instructions by any chance interfere with each other in
    > a way that will crash either of the threads?
    >
    > I hope I made myself a little bit more clear :).
    >
    > Thanks!!
    >
    > Mike


    I vote for OK, in this /very/ /particular/ /case/, but what would you
    accept as evidence?

    These pointer values start out different and become the same, but both
    are dictionaries.

    10490912 10490912
    10490912 10490912
    10490912 10490624
    10490912 10490624
    10490624
    10490624 10490624
    10490624 10490624
    10490624 10490624
     
    Aaron Brady, Oct 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    3
    Views:
    532
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    520
    Robert Brewer
    Dec 5, 2003
  3. k3xji
    Replies:
    7
    Views:
    878
    Gabriel Genellina
    Dec 30, 2008
  4. roschler
    Replies:
    1
    Views:
    454
    Gabriel Genellina
    Jul 8, 2009
  5. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    316
    nano2k
    Aug 9, 2007
Loading...

Share This Page