Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question Will no-lock crash the program?what about multi-core?

    let me put this simple:
    To a shared global ingeter variable i;


    there is only one thread write to it, like these:

    there are many other thread read it, like these:


    int temp = i + 100;


    //or any number of readonly sentances

    I DO NOT even use NO MUTEX in my program!


    will my program crash? in multi-core?

    In single-core, I figure it absolutely will not crash.
    but in multi-core, multiple CPU access one memory unit at the same moment? what happen?

    below I have a clearer case:
    if multiple threads(for example 1000 threads) are doing "i=100;" at the same moment?
    in multi-core CPU, will the program crash without LOCK!?

    I know that a program with many threads doing "i=100;" seams quite meaningless; and also I can write a program and run it for months for testing its not crashing, but without theoretic analysis on "why programs failed sooner of later without LOCK"(people told me that, I come here for WHY), I still feel bewildered when the test program crashed after a year.

    Who can tell me will this make a program(with atomic access of shared variable) crash ? If yes, why it crashes?

    I summarize my points:
    Is there any mechanism in OS avoiding multiple threads access a shared global variable at the same moment?
    Is there any mechanism is OS avoiding program crashing under the above situation?

    I presume that these access(read or write) is atomic, but does 'being atomic' mean that my program will definitely NOT crash or MAYBE crash? how about in multi-core situation?

    Also, I can figure an application where I can use this shared variable no-lock accessing:

    think about a application-layer routing program like this:
    one thread forwarding every incoming udp packet to 200 outgoing destination according to a 'route-table'(like a 200-elements-int-array standing for 200 different destination);
    another thread change the same 'route-table'

    I want the first forwarding thread use NO LOCK; it's just traversing the array and forwarding packets for performance reason. I can tolerate the indistinct of route-table at some moment. But just don't crash, please!? but I''m not sure whether OS allow me to do so?


    will MULTIPLE-THREADS-ATOMIC-ACCESS-SHARED-VARIABLE crash a program? and WHY? and how about in multi-core CPU?

    Is this possible or totally a silly idea? Please, give me help!?

  2. #2
    first of all please define : what do you mean by crash !!
    crash is happens when you access illegal memory, swapped page is not avaiable, code address space is modified by some other program and so on....

    accessing and writing global variable by multiple threads do not lead the program crash, but rather they lead to inconsistent read . so eventually it may crash because of inconsistency or may not, depends on your program logic.

    no matter it is multi core or single core, OS definately provides mechanism to avoid in consistent data write...
    giving locking mechanism b semaphore/mutex...
    so which makes the section atomic....
    atomic is a section where only one thread is allowed to do operation(s) exclusively.

  3. #3

    I understood

    Thank you.

  4. $spacer_open

Posting Permissions

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