Home > Software engineering >  Two threads semaphore interaction in a matter of time before
Two threads semaphore interaction in a matter of time before

Time:10-12

As title, I want to send a file according to the line to the lower machine, prepared two threads, every row, upper machine under a machine after receipt ACK command,
A thread order to detect an ACK, read A file is branch CFile: : readstring (), the data preparation to the sendbuffer, and SetEvent events hEvent,
B thread monitoring hEvent event, the data will be sengbuffer Writefile () sends out,

The question now is, see, receive send waveform display, a machine in order to send each ACK, is sent to the upper machine under a data center after a 14 ms or so, the fluctuating in 1 ms,

Whether can reduce the interval of the 14 ms, let download data faster, thank you

CodePudding user response:

Fyi:
//loop to a function every time send 200 bytes (this is the fixed) buffer, 
//a function need to incoming buffer, the circulation of 240 bytes (fixed) new buffer for processing,
//when dealing with each new printing takes two bytes of the buffer
# ifdef _MSC_VER
# pragma warning (4996) disable:
# endif
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; string.h>
# ifdef _MSC_VER
# include & lt; Windows. H>
# include & lt; Process. H>
# include & lt; IO. H>
# define MYVOID void
# define vsnprintf _vsnprintf
# the else
# include & lt; unistd.h>
# include & lt; Sys/time. H>
# include & lt; Pthread. H>
# define CRITICAL_SECTION pthread_mutex_t
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define MAXLINSIZE 16000
# include & lt; Time. H>
# include & lt; Sys/timeb. H>
# include & lt; Stdarg. H>
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
The static char logstr [MAXLINSIZE + 1];
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef _MSC_VER
The Lock (CRITICAL_SECTION void * l) {
The EnterCriticalSection (l);
}
Void Unlock (CRITICAL_SECTION * l) {
LeaveCriticalSection (l);
}
Void sleep_ms (int) ms {
Sleep (ms).
}
# the else
The Lock (CRITICAL_SECTION void * l) {
Pthread_mutex_lock (l);
}
Void Unlock (CRITICAL_SECTION * l) {
Pthread_mutex_unlock (l);
}
Void sleep_ms (int) ms {
Usleep (ms * 1000);
}
# endif
Void LogV (const char * pszFmt, va_list argp) {
* struct tm now;
Struct timeb TB;

If (NULL==pszFmt | | 0==pszFmt [0]) return;
Vsnprintf (logstr, MAXLINSIZE pszFmt, argp);
Ftime (& amp; TB);
Now=localtime (& amp; TB. Time);
Sprintf (datestr, "% d % % 4 d - 02-02 d", now - & gt; Tm_year + 1900, now - & gt; Tm_mon + 1, now - & gt; Tm_mday);
Sprintf (timestr, "02 02 02 % d: % d: % d", now - & gt; Tm_hour, now - & gt; Tm_min, now - & gt; Tm_sec);
Sprintf (MSS, "% 3 d", TB. Millitm);
Printf (" % s % s. % s % s ", datestr, timestr, MSS, logstr);
Flog=fopen (logfilename1, "a");
If (NULL! Flog)={
Fprintf (flog, "% s % s. % s % s", datestr, timestr, MSS, logstr);
If (ftell (flog) & gt; MAXLOGSIZE) {
The fclose (flog);
If (rename (logfilename1 logfilename2)) {
Remove (logfilename2);
Rename (logfilename1 logfilename2);
}
} else {
The fclose (flog);
}
}
}
Void the Log (const char * pszFmt,... ) {
Va_list argp;

The Lock (& amp; Cs_log);
Va_start (argp, pszFmt);
LogV (pszFmt argp);
Va_end (argp);
Unlock (& amp; Cs_log);
}
//the Log}
# define ASIZE 200
# define BSIZE 240
# define CSIZE 2
Char Abuf [ASIZE];
Char Cbuf [CSIZE];
CRITICAL_SECTION cs_HEX;
CRITICAL_SECTION cs_BBB;
Struct FIFO_BUFFER {
Int the head;
Int tail;
Int size;
Char data [BSIZE];
} BBB;
Int No_Loop=0;
Void HexDump) (int cn, char * buf, int len) {
Int I, j, k;
Char binstr [80].

The Lock (& amp; Cs_HEX);
for (i=0; iIf (0==16) (I %) {
Sprintf (binstr, "% 3 d % 4 x -", cn, I);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; J<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
The Log (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
For (j=0; JSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; JSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
The Log (" % s \ n ", binstr);
}
Unlock (& amp; Cs_HEX);
}
Int GetFromRBuf (int cn, CRITICAL_SECTION * cs, struct FIFO_BUFFER * fbuf, char * buf, int len) {
Int lent, len1 len2;

Lent=0;
The Lock (cs);
If (fbuf - & gt; Size> Len)={
Lent=len;
If (fbuf - & gt; The head + lent> BSIZE) {
Len1=BSIZE fbuf - & gt; The head;
Memcpy (buf, fbuf - & gt; The data + fbuf - & gt; The head, len1);
Len2=lent - len1;
Memcpy (buf + len1 fbuf - & gt; The data, len2);
Fbuf - & gt; The head=len2;
} else {
Memcpy (buf, fbuf - & gt; The data + fbuf - & gt; The head, lent);
Fbuf - & gt; The head +=lent;
}
Fbuf - & gt; Size -=lent;
}
Unlock (cs);
Return lent;
}
MYVOID thdB (void * PCN) {
Char * recv_buf;
Int recv_nbytes;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related