Results 1 to 6 of 6

Thread: What is the race condition?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    2

    What is the race condition?

    I think it is related to multi-threading...


  2. #2
    Expert Member
    Join Date
    Oct 2005
    Answers
    383

    Re: What is the race condition?

    Race condition occurs when two threads acces same instance variable, at same time.i.e both threads may get wrong data.

    unlike local variables,Remeber instance variables are shared by threads

    in order to prevent race condition, you need to synchronize methods

    :)
    NEVER SAY DIE.

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Answers
    17

    Re: What is the race condition?

    Quote Originally Posted by ABSMITH View Post
    I think it is related to multi-threading...
    Race Conditions: When two threads (with same or different execution paths) race with each other to acquire the same resource (be it memory variable or any other programmable resource). An important fact to be kept in mind is that the Operating Systems schedules threads of equal priority arbitrarily. For example,

    Thread1 - As soon as writes data to MemVar1, OS puts it to sleep and schedules Thread2 for execution
    Thread2 - Does a few operations and Writes data to MemVar1. OS puts Thread2 to sleep and schedules/resumes Thread1 for execution again
    Thread1 resumes and uses data it previously wrote to MemVar1. However, since Thread2 overwrote Thread1's data in MemVar1, the data is now dirty for Thread1.

    This condition can be mitigated using synchronization objects for data manipulation. However, ineffective/improper use of synchronization objects result in other (and very painful to resolve) problems such as deadlocks and slowing down of program execution. Consider the following example.

    Thread1:
    Acquire SyncObject 1;
    Modify MemVar1;
    Acquire SyncObject2;
    Modify MemVar2;
    Verify MemVar1 and MemVar2;
    Release SyncObject1 and SyncObject2;
    Exit Thread;

    Thread2:
    Acquire SyncObject2;
    Modify MemVar2;
    Acquire SyncObject1;
    Modify MemVar1;
    Verify MemVar2 and MemVar1;
    Release SyncObject2 and SyncObject1;
    Exit Thread;
    ------------------
    In the above two scnerios, consider that after Thread1 acquiring SyncObject1 and modifying MemVar1, the OS puts it to sleep and schedules/resumes Thread2 for execution. Thread2 resumes, acquires SyncObject2, modifies MemVar2, and tries to Acquire SyncObject1. Since SyncObject1 is held by Thread1, Thread2 goes into wait mode until SyncObject1 is released by Thread1. OS puts the waiting thread to sleep and resumes Thread1. Thread1 now tries to acquire SyncObject2. Since SyncObject2 is held by Thread2, Thread1 goes into wait mode until Thread2 releases SyncObject2.

    Since Thread2 will not release SyncObject2 until Thread1 releases SyncObject1, and Thread1 does not release SyncObject1 until Thread2 releases SyncObject2, this results in a deadlock. Both threads lock up and will not execute any further resulting in the program hanging.

    Last edited by SomGollakota; 05-25-2008 at 12:53 PM.

  4. #4
    Junior Member
    Join Date
    May 2008
    Answers
    7

    Re: What is the race condition?

    Race condition occur when 2 or more thread try to access the same resource at same time.
    here the resources can be a local varialble,memory,file etc.


  5. #5
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    Re: What is the race condition?

    Race condition errors can occur when the outcome of a program dependant which of the two thread complete a process first.
    PLease let me know if you want more details .


  6. #6
    Contributing Member
    Join Date
    Oct 2008
    Answers
    72

    Re: What is the race condition?

    A race condition or race hazard is a flaw in a system or process whereby the output and/or result of the process is unexpectedly and critically dependent on the sequence or timing of other events. The term originates with the idea of two signals racing each other to influence the output first.

    Race conditions can occur in electronics systems, especially logic circuits, and in computer software, especially multithreaded or distributed programs.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact