Thread: mutex necessity

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    3) They can call the same mutex. I believe it is guaranteed that only one will get the lock.

    2) Reading a variable doesn't need syncrhonization. If two threads require the same variable to read, then the bus controller will serialize the "require" and give the values to each thread. The problem is with writing.
    A classic example
    Thr1: r = 2;
    Thr2: r = 1;

    Both threads read r. It doesn't matter who reads first. It matters who writes last. That will determine r value. If you have:

    Thr1: r+= 2;
    Thr2: r = 1;

    Then the write (for the same variable) makes it necessary for synchronizing the oprerations.

    1) So you can read Struct[0].field with Thr1 and Struct[0].field2 with Thr2 and change their value safely. Since the data is in different memory location.
    In general, you can safely read Struct[0], since its value cannot be changed, and safely read/write its struct fields since they aren't shared, but each thread reads different memory locations.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    3) They can call the same mutex. I believe it is guaranteed that only one will get the lock.
    Of course it's guaranteed. Otherwise there would be no use for mutexes.

    2) Reading a variable doesn't need syncrhonization. If two threads require the same variable to read, then the bus controller will serialize the "require" and give the values to each thread.
    I don't think that's a given. I believe (some) architectures can indeed process multiple read requests at the same time. Thus they need not be serialized.
    Regardless, if threads only reads, the data will never change and regardless who reads first and who reads last or if one thread reads half and then the second thread starts to read, it doesn't matter, because the value will always be the same.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. mutex problem
    By radeberger in forum C++ Programming
    Replies: 16
    Last Post: 03-20-2009, 04:24 PM
  3. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  4. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  5. Mutex - location of lock/unlock
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2003, 12:28 PM