Thread: Thread-safe locking: is incoming data ignored during lock?

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    Thread-safe locking: is incoming data ignored during lock?

    I understand the concept of thread safety is to prevent data races and other undefined behavior. However, when using mutexes and thread locks (say C++'s std::mutex and std::scoped_lock for example), say I have a thread-safe network message queue and need to lock it in order to pop a message. If another message arrives during that lock period, is it waiting in line so to speak, or is it utterly ignored and thus the queue cannot accept incoming data during a lock period? I'm very concerned about possible data loss.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,148
    I think if your underlying protocol is TCP then you should receive all your messages, in the correct order.
    The errors of great men are venerable because they are more fruitful than the truths of little men. - Nietzsche

  3. #3
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    Quote Originally Posted by john.c View Post
    I think if your underlying protocol is TCP then you should receive all your messages, in the correct order.
    That's a relief! Is UDP also the same? Because I'm not sure yet but I think I may use that for some operations.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,148
    No, UDP is "unreliable". It's used for higher performance. It will not necessarily get the packets to you and when packets do arrive they may be out of order.
    The errors of great men are venerable because they are more fruitful than the truths of little men. - Nietzsche

  5. #5
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    I see... Oh well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this thread safe?
    By theoobe in forum C# Programming
    Replies: 2
    Last Post: 09-22-2012, 02:27 AM
  2. how to show incoming binary data as hex values
    By khalid10 in forum C# Programming
    Replies: 8
    Last Post: 05-10-2010, 11:55 PM
  3. Implementing shared/exclusive locking (readers/writer) lock
    By ruj.sabya in forum Linux Programming
    Replies: 0
    Last Post: 05-08-2008, 12:06 AM
  4. Is this thread safe
    By Bru5 in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2006, 05:34 PM
  5. Cannot read incoming data from socket
    By fnoyan in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2006, 02:42 AM

Tags for this Thread