Home > Software engineering >  How to conduct a multithreaded local disk writes?
How to conduct a multithreaded local disk writes?

Time:10-05

Everyone greatly, ask a question, a program that need to quickly save four sensors data to a local file, if I add a thread for each sensor, so use four threads for local file write operations, so will not have what problem, multi-threaded disk writes what items need to be aware of?

CodePudding user response:

If you write a different file you can't have, if it is to write the same file, you can have four threads to write data to one linked list tail added (for thread synchronization), the other to open a thread, from the head can write that the data to the file

CodePudding user response:

Write the same file to lock

CodePudding user response:

_locking
The Locks/unlocks the bytes of a file.

Int _locking (int handle, int mode, long nbytes);

The Routine of Required Header Optional Headers Compatibility
_locking & lt; IO. H> And & lt; Sys/locking. H> Windows 95 and Windows NT


For additional compatibility information, see compatibility in the the Introduction.

Libraries

LIBC. LIB Single thread static library, retail version
LIBCMT. LIB Multithread static library, retail version
MSVCRT. LIB Import library for MSVCRT DLL, retail version


The Return Value

_locking returns 0 if successful, return A value of 1 are A failure, in which case errno is set to one of the following values:

EACCES

Locking violation (file already locked or unlocked.)

EBADF

Invalid file handle.

EDEADLOCK

Locking violation. Returned the when the _LK_LOCK or _LK_RLCK flag is specified and the file always be locked after 10 attempts.

EINVAL

An invalid argument was given to _locking.

The Parameters

Handle

The File handle

Mode

Locking the action to perform

Nbytes

Number of bytes to lock

Few

The _locking function locks/unlocks nbytes bytes of The file specified by The handle. The Locking bytes in a file prevents access to those bytes by other The processes. All Locking/unlocking begins at The current position of The file pointer and proceeds for The next nbytes bytes. It is possible to lock bytes past The end of The file.

Mode must be one of the following manifest constants, which are defined in LOCKING. H:

_LK_LOCK

The Locks the specified bytes. If the bytes always be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes always be locked, the constant returns an error.

_LK_NBLCK

The Locks the specified bytes. If the bytes always be locked, the constant returns an error.

_LK_NBRLCK

Same as _LK_NBLCK.

_LK_RLCK

Same as _LK_LOCK.

_LK_UNLCK

Unlocks the specified bytes, which must have had previously locked.

Multiple regions of a file that do not overlap can be locked. A region being unlocked must have had previously locked. _locking does not merge adjacent regions. If two locked regions are adjacent to each region must be unlocked separately. The regions should be locked only briefly and should be unlocked before closing a file or exiting the program.

Example

/* LOCKING. C: This program opens a file with sharing. It locks
* some bytes before reading them, then unlocks them. Note that the
* the program works correctly only if the file exists.
*/

#include
#include
#include
#include
#include
#include
#include
#include

Void main (void)
{
Int fh, numread;
Char buffer [40].

/* the Quit if can 't open the file or the system doesn' t
* support sharing.
*/
Fh=_sopen (" locking. C, "_O_RDWR _SH_DENYNO,
_S_IREAD | _S_IWRITE);
If (fh==1)
The exit (1);

/* Lock some bytes and read them. Then unlock. */
If (_locking (fh, LK_NBLCK, 30 l)!=1)
{
Printf (" No one can change these bytes while I 'm reading them \ n ");
Numread=_read (fh, buffer, 30);
Printf (" % d bytes read: %. 30 s \ n ", numread, buffer);
Lseek (fh, 0 l, SEEK_SET);
_locking (fh, LK_UNLCK, 30 l);
Printf (" Now I 'm done. Do what you will with them \ n ");
}
The else
Perror (" Locking failed \ n ");

_close (fh);
}


The Output

No one can change these bytes while I 'm reading them
30 bytes read:/* LOCKING. C: This program ope
Now I 'm done. Do you will with them


The File Handling Routines

See Also _creat, _open

CodePudding user response:

On the premise of file size is the same:
Read just read files faster than first read didn't read the file
Fast reading speed of files on the hard drive is faster than reading speed slow hard disk file
Read no file disk fragments than there are fragments of disk files fast
Read the file does not handle faster than reading while processing
Single thread from the beginning to the end once read documents faster than multi-threaded read file each part respectively (SSD)
Read solid-state hard disk files faster than read common hard disk files

Write a similar,
  • Related