question with multi-threaded programming

This is a discussion on question with multi-threaded programming within the C++ Programming forums, part of the General Programming Boards category; will it be dangerous that more than one threads are sharing one variable? what would happened if two or more ...

  1. #1
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149

    question with multi-threaded programming

    will it be dangerous that more than one threads are sharing one variable? what would happened if two or more threads are trying to operate one variable at the same time? any help would be greatly appreciated~~

    blow me ... ...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > will it be dangerous that more than one threads are sharing one variable?
    Variables which can be modified in a single instruction are OK - say an integer
    Code:
    int shared = 0;
    Two threads can read and write that as much as they want, and nothing unusual is going to happen.

    Code:
    char *shared_buff = NULL;
    One thread doing
    shared_buff = new char[10];
    and another thread doing
    delete [] shared_buff;
    is on the other hand a recipe for big disasters.

    Look up critical sections / semaphores / mutex for info on how to guard concurrent access to shared data.

  3. #3
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by Salem
    > will it be dangerous that more than one threads are sharing one variable?
    Variables which can be modified in a single instruction are OK - say an integer
    Code:
    int shared = 0;
    Two threads can read and write that as much as they want, and nothing unusual is going to happen.
    thank you,Salem
    if one thread are doing "shared++;" when another thread are doing "shared=10;", two are trying to operate shared at the same time, then what is the value of shared?

    blow me ... ...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    ++ isn't an atomic operation

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    ho.......what if doing "shared = 10;" and "shared = 1;" at the same time?

    blow me ... ...

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I have a simple Windows MT tutorial which covers basic synchronisation starting here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by adrianxw
    I have a simple Windows MT tutorial which covers basic synchronisation starting here.
    i will read it carefully. thanks a lot ~~~~~~

    blow me ... ...

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    ho.......what if doing "shared = 10;" and "shared = 1;" at the same time?
    One of them will be executed a little later, that one will prevail.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> what if doing "shared = 10;" and "shared = 1;" at the same time?

    On a single processor system, only one thread will be running, whichever thread runs second will set the result. On a multi processor system, both are likely to set the value in their own local cache, whichever cache flushes last will set the value.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Quote Originally Posted by Salem
    > will it be dangerous that more than one threads are sharing one variable?
    Variables which can be modified in a single instruction are OK - say an integer
    Code:
    int shared = 0;
    Two threads can read and write that as much as they want, and nothing unusual is going to happen.
    ...
    I have to disagree.
    All multithreaded access to variables should be protected by synchronization primitives - here's why.

    There are a couple of problems that can arise when using this construct. The compiler is free to optimize (what appears to be) read-only access to memory by placing the memory contents into a CPU register. While one thread uses the value within the register, another thread can change the value within memory which the first thread may never see.
    This can be overcome on single x86 processor machines by using the volatile keyword. This tells the compiler to not optimize access to an object (reads and writes go directly to memory).

    It is true that reads and writes to aligned, CPU-sized data, on an x86 is an atomic operation. (I've also read that this is only true for PII's or higher.) This is not necessarily true on other CPUs, in which case using volatile doesn't help since a read or write operation could be pre-empted.

    On SMP machines, volatile doesn't help the situation either since each processor has it's own memory cache. The processors within an SMP machine are only required to synchronize their caches on a memory barrier. Memory barriers occur when certain instructions are executed.

    Using a synchronization primitive/function will typically execute one of these instructions and cause a memory barrier. This is necessary so that any cached modifications to an object on another processor are seen by the current processor.

    So in summary, synchronization primitives/functions (which cause memory barriers) should be used in order to implement correct synchronization in all environments.

    Synchronization and Multiprocessor Issues
    Interlocked Variable Access

    gg

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662

  12. #12
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    synchronization, i will conquer it this weekend.

    blow me ... ...

  13. #13
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Hermitsky
    synchronization, i will conquer it this weekend.
    Unlikely. It will suck up the weekend and still leave you with tons of things uncovered :-)
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21