[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Logging and Threads
[Thread Prev] | [Thread Next]
- Subject: Re: Logging and Threads
- From: Marian Beermann <public@xxxxxxxxx>
- Reply-to: libssh@xxxxxxxxxx
- Date: Tue, 4 Mar 2025 22:26:21 +0100
- To: libssh@xxxxxxxxxx, g4-lisz@xxxxxxxxxxxx
I'm not sure what you mean by blocking here, but I'm pretty sure it's not an issue either way.
These are all blocking file I/O, though normally the page cache will absorb any writes. Note that fflush(3) just flushes the FILE* buffers to the file descriptor. It doesn't fsync(2) (which may block for I/O for some time).
There's also a lock in FILE* which is taken by the buffered I/O functions. That's what makes these thread-safe. Naturally, any in-progress fprintf which has taken the lock on a given FILE will mutually exclude any other fprintf from starting progress on the same FILE.
Cheers, Marian On 3/4/25 21:25, g4-lisz@xxxxxxxxxxxx wrote:
Hi there!I never thought about this... until now: I'm using libssh in a multithreaded application and I'm logging from within the threads, using the lib's logging functions. I'm quite sure that this isn't an issue as long as the output goes to stderr.But I added a callback (ssh_set_log_callback) which writes the messages to a file handle, like:logging_cb( ... ) {fprintf(log_fd, "[%s, %d] %s - %s\n", date, verbosity, (char *)userdata, buffer);fflush(log_fd); }fprintf() is thread-safe. But can it block? In other words: Could one thread temporarily block another because of the logging?So far I couldn't observe any blocking. But to be honest, I'm not sure why it works fine.Maybe the OS layer write buffer is simply big enough to "take it all"? Cheers Till
| Logging and Threads | g4-lisz@xxxxxxxxxxxx |