« first day (1012 days earlier)      last day (1841 days later) » 

11:42 AM
I am confused. Does thread-safe mean I don't need to protect the data/variable I pass in?
 
@Rick it depends?
 
For example, ssize_t write(int fd, const void *buf, size_t count);, if I have 2 threads writing to the same file descriptor, do i have to surround write with mutex lock and unlock?
 
@Rick yes, reentrant does not mean data race free
 
> It depends on the function, and how you use it.

Take for example memcpy, it is generally thread safe, if you copy data where both source and destination is private to a single thread. If you write to data that can be read from/written to by another thread, it's no longer thread safe and you have to protect the access.
 
pretty much
write is thread safe too in the sense it can be called by more than one thread
but if you're writing to the same dest things get interesting
 
11:49 AM
So I think thread-safe may only indicate that the internal data structure that a function use is protected among threads.
It does not protect anything from outside.
Can I understand it in this way?
 
@Rick or is designed not to have shared state
usually the terms used are "Reentrant" e.g. what you said and 'Thread-safe' a stronger guarantee that is often mis-used
 
Ah. I see books have different interpretations as for "Reentrant".
> A reentrant function
achieves thread safety without the use of mutexes
 
yeah a lack of ubiquitous language creates issues
that's not actually different, but mutexes aren't always required
 
Skip this.. Let's go back to the previous question.
Hmm
@Mgetz If thead-safe does not mean data race free as you said
what about printf
 
@Rick actually factually thread safe
it uses a lock around stdio
 
11:59 AM
printf is thead safe and it's writing to standard ouput, a standard I/O stream.
So do I need to surround printf with a mutex?
No? Because internally printf maintains a lock itself to protect standard output?
 
@Rick no
@Rick technically fwrite does
the standard requires streams to be data-race free in the sense that two calls two fwrite at the same time will result in no undefined behavior, but rather undefined output
 
What if i use fprintf with standard output stream instead of printf? Do I need to surround fprintf with a mutex?
 
@Rick no because fprintf is a wrapper around fwrite which is where the mutex actually lives
 
But I think the mutex lives outside fwrites, in printf and fprintf..
 
@Rick nope
formatting is thread safe
 
12:07 PM
Why write need to use with mutex while fwrite does not?
I can't see the difference.
T-T
The question should be:
Why some thread safe functions need to be surrounded with mutex while some dont ?
 
@Rick for what it's worth none of this is required by the standard per se
@Rick so I would still surround fwrite with a mutex. As aforementioned, threadsafe internally does not mean the actual write will make sense
 
QAQ
Let me check the source code see if I can find something.
 
 
2 hours later…
2:00 PM
good afternoon!
anyone feel like answering a C question? (shameless ad for a question I've asked in the C room)
 
2:28 PM
@iksemyonov have you already asked it on the site? We reserve the right to answer it in C++
 
@Mgetz you are right pal. I think I should always surround a mutex for shared data in a thread, no matter how the data is internally constructed, no matter the shared variable is user defined or pre-defined like stdout.
 
@Rick well saying that fprintf is thread safe is one thing, but if you actually try it you'll get garbage because internally it may use fputc as the thread safe level.
as the developer it's up to you to guard your own resources
 
Well as the developer, I have no idea on which level the mutex is involed. A non-primitive data type can carry a mutex itself.
And that's not the thing I should worry and find out. Just lock it :D.
 
pretty much, their safety may be more about UB prevention rather than design behavior
 
but make sure that you use the correct mutex for the shared data
 
2:40 PM
side note: this is actually why I like apple's approach (and now MS' ) to concurrency. You just schedule a callback to deal with it on the owning thread
tbf you could do this before in most systems it was just a bigger pain in the butt to pass context
 
Qt does the same thing with its signal/slots
 
For example, the `FILE` structure itself could be designed to carry a mutex/lock. Then every library function that receive a `FILE` object can be designed to :
retrieve the mutex inside the object and lock it, do sth, then unlock.

Then it will not be a problem of "on which level the mutex is invovled", instead it becomes "as long as a `FILE` object is involved, it ensures it get synchronized".
 
@Rick FILE does actually carry a mutex on most implementations btw
 
struct _IO_FILE {
  int _flags;		/* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;	/* Current read pointer */
  char* _IO_read_end;	/* End of get area. */
  char* _IO_read_base;	/* Start of putback+get area. */
  char* _IO_write_base;	/* Start of put area. */
  char* _IO_write_ptr;	/* Current put pointer. */
I am just guessing :). I found this line ` _IO_lock_t *_lock;` in its definition.
Don't know if it's used like I described.
 
@Rick that's actually no longer accurate it just exists because people were doing crap they shouldn't have
the actual structure IIRC is actually different
they do book keeping to keep that up to date for people doing bad
 
2:48 PM
the actual structure depends on the C runtime you are running
 
well it's supposed to be opaque
but MSVC's wasn't for the longest time so people did baaaaad things
 
as a side note, deleting that struct is impossible to do thread safely using only the embedded mutex
 
Anyway, I should not worry on which level it's locked.
But think back and reconsider the concept of thread safe and thread safe function, I suddenly feel that it's not a big deal. It does not solve big problem. I mean, one still need to know the mechanism of mutex synchronization and use mutex with thread safe function. It does not create an entire abstraction layer to ease the pain.
 
@Rick well it's more important to realize that 'thread-safe' does not really mean 'data-race' free in a complete sense
there can still be dataraces from a macro level
they just won't be UB
 
Yes, I was stupid and thought thread safe == no mutex involved in a thread.
Writing your own thread safe function isn't really a difficult thing I Think.
 
2:57 PM
@Rick nope, it just means that there are no micro level data races
 
Yes
 
@Rick most methods that don't deal with shared state are thread safe and reentrant
 
Making the library function thread safe, that might be a big deal, since many things are involved.
 
Depends, memcpy is quite simple
 
😁. Thanks @Mgetz anyway, I feel eased after pesuading myself.
Can go for a meal now 😁.
 
4:00 PM
@Mgetz Is it worth asking on the site? I havent' so far, no.
 
4:18 PM
@iksemyonov kinda hard to tell when we don't know the question
 
 
2 hours later…
5:48 PM
@iksemyonov That's kinda the entire point of the site
 

« first day (1012 days earlier)      last day (1841 days later) »