Thread: Threads + Context Switching

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    13

    Threads + Context Switching

    Hi,
    I'm looking for race conditions in a small piece of code, and there is one particular thing I'm looking at.

    If I have a line like this:

    Code:
    list[n++] = item;
    this translates to

    Code:
    list[n] = item;
    n++;
    So I am I right in thinking that this assignment to list[n] could occur, then a bunch of code from another code is executed before n++?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you would actually have to look at the assembler code generated by the compiler to determine that - it would depend on the actual processor being used exactly what instructions you get and whether there is a mouse-hole or one that you can drive a bus through.

    But indeed, there is no guarantee that n++ is executed as part of the actual line above. Nor that the compiler generates instructions that in themselves don't violate some threading rules. C is not, in and of itself, a threadsafe language. You have to MAKE such things sure in your design of the software.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    13
    Ok, make sense. I suppose there is no harm in assuming that it will be executed separately.

    Thanks.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I'm looking for race conditions...
    If you don't see any thread synchronization 'primitives' like pthread_mutex_lock() or EnterCriticalSection() proctecting shared memory access, then it's safe to assume that there are race conditions - until proven otherwise.

    gg

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    13
    Oh I know there are race conditions, I have to find them. This is for an assignment, I just wasn't sure about that one.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, here's the general rule:
    If any threads access a shared resource, such as the same variable, you must use thread synchronization, such as critical sections.
    If you need a thread to do something before any other, then events might do the trick.
    If one thread mustn't be disturbed while doing some important work that is also important to the other thread, use critical sections or the like.

    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most OS's also have constructs such as "LockedIncrement()", "LockedDecrement()", "CompareAndSwapLocked()" which is guaranteed to be thread/context safe (although the latter may do "nothing" and return a result to indicate that "nothing was done", depending on the circumstances in which it is used).

    Note that even a simple statement such as "n++" is not guaranteed to be one instruction on all platforms - for example RISC processors would do this as a "read, modify, write". On processors that support complex instructions, the compiler may well BE ABLE to use this construct, but choose not to do so because n is used in such a way that the compiler thinks it's better to use a register, e.g. the above "list[n++] = item" can become:
    Code:
        mov  eax, n
        mov  edx, list
        mov  [edx+eax*4],item
        inc  eax
        mov n, eax
    This is "simplified syntax x86 assembler, so may not be 'compiler friendly'... ".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  3. how to get context switching between threads
    By v3dant in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 11:04 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM