Thread safety via function refactoring

As we saw in the preceding example, we need the mutex lock because the gbuf global buffer was being used by all application threads as their I/O buffer. So, think on this: what if we can allocate an I/O buffer that's local to each thread? That would indeed solve the issue! How exactly, will be shown with the following code.

But first, now that you are familiar with the previous example (where we used the mutex lock), study the output of the refactored program:

$ ./mt_iobuf_rfct 10000./mt_iobuf_rfct: using default stdio IO RW buffers of size 4096 bytes; # IOs=10000mt_iobuf_rfct.c:testit_mt_refactored:51: [Thread #0]: numio=10000   total rdwr=5120000   expected # rw syscalls=1250 iobuf = 0x7f283c000b20mt_iobuf_rfct.c:testit_mt_refactored:51: ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.