Home > Software engineering >  Fibers and threads inside what is the difference between the function by the program?
Fibers and threads inside what is the difference between the function by the program?

Time:10-24

 # include & lt; Windows. H> 
# include & lt; Flags. H>
# include & lt; Tchar. H>


# define PRIMARY_FIBER 0
# define WRITE_FIBER 1
# define READ_FIBER 2
# define FIBER_COUNT 3
# define COPY_LENGTH 512

VOID CALLBACK ReadFiber (LPVOID lpParam);
VOID CALLBACK WriteFiber (LPVOID lpParam);

Typedef struct _tagFIBER_STRUCT
{
DWORD dwFiberHandle;
HANDLE hFile;
LPVOID lpParam;
} FIBER_STRUCT, * LPFIBER_STRUCT;

Char * g_lpBuffer=NULL;
LPVOID g_lpFiber [FIBER_COUNT]={};
Void GetApp (LPTSTR lpPath, int nBufLen)
{
TCHAR szBuf [MAX_PATH]=_T (" ");
GetModuleFileName (NULL, szBuf, MAX_PATH);

Int nLen=_tcslen (szBuf);
For (int I=nLen; I & gt; 0; I -)
{
If (szBuf [I]=='\ \')
{
SzBuf [I + 1)=_T (' \ 0 ');
break;
}
}

NLen=_tcslen (szBuf) + 1;
Int nCopyLen=min (nLen nBufLen);
StringCchCopy (lpPath nCopyLen, szBuf);
}

Int _tmain (int arg c, _TCHAR * argv [])
{
G_lpBuffer=(char *) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, COPY_LENGTH);
FIBER_STRUCT fs/FIBER_COUNT={0};

TCHAR szDestPath [MAX_PATH]=_T (" ");
TCHAR szSrcPath [MAX_PATH]=_T (" ");
GetApp (szDestPath MAX_PATH);
GetApp (szSrcPath MAX_PATH);
StringCchCat (szSrcPath MAX_PATH, _T (" 2. JPG "));
StringCchCat (szDestPath MAX_PATH, _T (" 2 _cpy. JPG "));

HANDLE hSrcFile=CreateFile (szSrcPath GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
HANDLE hDestFile=CreateFile (szDestPath GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);

Fs [PRIMARY_FIBER] hFile=INVALID_HANDLE_VALUE;
Fs [PRIMARY_FIBER] lpParam=NULL;
Fs [PRIMARY_FIBER] dwFiberHandle=0 x00001234;

Fs [WRITE_FIBER] hFile=hDestFile;
Fs [WRITE_FIBER] lpParam=NULL;
Fs [WRITE_FIBER] dwFiberHandle=0 x12345678;

Fs [READ_FIBER] hFile=hSrcFile;
Fs [READ_FIBER] dwFiberHandle=0 x78563412;
Fs [READ_FIBER] lpParam=NULL;

G_lpFiber [PRIMARY_FIBER]=ConvertThreadToFiber (& amp; The fs/PRIMARY_FIBER);
G_lpFiber [READ_FIBER]=CreateFiber (0, (LPFIBER_START_ROUTINE) ReadFiber, & amp; The fs/READ_FIBER);
G_lpFiber [WRITE_FIBER]=CreateFiber (0, (LPFIBER_START_ROUTINE) WriteFiber, & amp; The fs/WRITE_FIBER);

//switch to read fibers
SwitchToFiber (g_lpFiber [READ_FIBER]);

//remove fibers
DeleteFiber (g_lpFiber [WRITE_FIBER]);
DeleteFiber (g_lpFiber [READ_FIBER]);

The CloseHandle (fs) [r]. READ_FIBER hFile);
The CloseHandle (fs) [r]. WRITE_FIBER hFile);

//back to thread
ConvertFiberToThread ();
return 0;
}

VOID CALLBACK ReadFiber (LPVOID lpParam)
{
//copy files
While (TRUE)
{
LPFIBER_STRUCT pFS=(LPFIBER_STRUCT lpParam);
Printf (" switch to 8 x [%] fibers \ n ", pFS - & gt; DwFiberHandle);
DWORD dwReadLen=0;
ZeroMemory (g_lpBuffer COPY_LENGTH);
ReadFile (pFS - & gt; HFile, g_lpBuffer COPY_LENGTH, & amp; DwReadLen, NULL);
SwitchToFiber (g_lpFiber [WRITE_FIBER]);
If (dwReadLen & lt; COPY_LENGTH)
{
break;
}
}

SwitchToFiber (g_lpFiber [PRIMARY_FIBER]);
}

VOID CALLBACK WriteFiber (LPVOID lpParam)
{
While (TRUE)
{
LPFIBER_STRUCT pFS=(LPFIBER_STRUCT lpParam);
Printf (" switch to 8 x [%] fibers \ n ", pFS - & gt; DwFiberHandle);
DWORD dwWriteLen=0;
WriteFile (pFS - & gt; HFile, g_lpBuffer COPY_LENGTH, & amp; DwWriteLen, NULL);
SwitchToFiber (g_lpFiber [READ_FIBER]);
If (dwWriteLen & lt; COPY_LENGTH)
{
break;
}
}

SwitchToFiber (g_lpFiber [PRIMARY_FIBER]);
}


Above fibers is online to see the article introduces the example code: function is copying files, https://www.cnblogs.com/lanuage/p/7725683.html

Want to know that "read fibers" and "write fibers" calls and a thread inside each other directly while (1) {read no judgement; Read function; Write a function;) What's the difference?

CodePudding user response:

No one notice,,,

CodePudding user response:

[code]=c
Void CFiberCopyDlg: : OnStart ()
{
//TODO: Add your the control notification handler code here
//
Cstrings prompt;
LPFIBERDATASTRUCT fs.
//the allocate storage for our fiber data structures
The fs=(LPFIBERDATASTRUCT) HeapAlloc (GetProcessHeap (), 0, sizeof (FIBERDATASTRUCT) * FIBER_COUNT);
If (fs==NULL)
{
Prompt. The Format (" HeapAlloc error! (err %=lu) \ n ", GetLastError ());
TRACE0 (prompt);
return ;
}
//the allocate storage for the read/write buffer
G_lpBuffer=(LPBYTE) HeapAlloc (GetProcessHeap (), 0, BUFFER_SIZE);
If (g_lpBuffer==NULL)
{
Prompt. The Format (" HeapAlloc error! (err %=lu) \ n ", GetLastError ());
TRACE0 (prompt);
return ;
}
//open the source file
Fs [READ_FIBER] hFile=CreateFile (".. \ \ REDIST. TXT ", GENERIC_READ FILE_SHARE_READ,
NULL, OPEN_EXISTING FILE_FLAG_SEQUENTIAL_SCAN, NULL);

If (fs) [r]. READ_FIBER hFile==INVALID_HANDLE_VALUE)
{
Prompt. The Format (" HeapAlloc error! (err %=lu) \ n ", GetLastError ()); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related