Home > Back-end >  Multiple threads if statement is wrong
Multiple threads if statement is wrong

Time:10-22

The thread function used in the if statement, also entered the statement execution when the condition does not meet the
Without overloading the==
Used to compare two variables for the same type of pointer
See

, every answer, up for a long time, can you give a solution

CodePudding user response:

To lock the lock, including printf also want to add,
Fyi:
 # include & lt; Stdio. H> 
#include
#include
# ifdef WIN32
#include
#include
#include
# define MYVOID void
# the else
#include
#include
#include
# define CRITICAL_SECTION pthread_mutex_t
# define _vsnprintf vsnprintf
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define ARRSIZE (x) (sizeof (x)/sizeof (x [0]))
#include
#include
#include
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
Char logstr [16000].
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef WIN32
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;
If (1==_vsnprintf (logstr, ARRSIZE (logstr), pszFmt, argp)) logstr [ARRSIZE (logstr) - 1)=0.
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);
}
Flog=fopen (logfilename1, "a");
If (NULL==flog) return;
}
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}
Int No_Loop=0;
MYVOID testThread (void * PCN) {
Int n, I;

N=(int) PCN;
i=0;
While (1) {
Sleep_ms (1000);
The Log (" testThread in % d: I==% ds \ n ", n + + I);
If (i>=5) No_Loop=1;
}
}
Int main (int arg c, char * argv []) {
int i;
# ifdef WIN32
InitializeCriticalSection (& amp; Cs_log);
# the else
Pthread_mutex_init (& amp; Cs_log, NULL);
Pthread_t threads [1].
Int threadsN;
Int rc;
# endif
The Log ("=========BEGIN==================\ n ");
# ifdef WIN32
(__cdecl _beginthread (void *) (void *)) testThread, 0, 1 (void *));
# the else
ThreadsN=0;
Rc=pthread_create (& amp; (threads [threadsN++]), NULL, testThread, 1) (void *); If (rc) Log (" % d=pthread_create error % d! \ n ", rc, threadsN - 1);
# endif
i=0;
While (1) {
Sleep_ms (100);
The Log (" in the main: I==% d \ n ", + + I);
If (No_Loop==1) break;//
}
The Log ("=========END====================\ n ");
# ifdef WIN32
DeleteCriticalSection (& amp; Cs_log);
# the else
Pthread_mutex_destroy (& amp; Cs_log);
# endif
return 0;
}
//the 2012-06-14 16:27:21. 500=========BEGIN==================
//the 2012-06-14 16:27:21. 609 in the main: I==1
//the 2012-06-14 16:27:21. 718 in the main: I==2
//the 2012-06-14 16:27:21. 828 in the main: I==3
//the 2012-06-14 16:27:21. 937 in the main: I==4
//the 2012-06-14 16:27:22. 046 in the main: I==5
//the 2012-06-14 16:27:22. 156 in the main: I==6
//the 2012-06-14 16:27:22. 265 in the main: I==7
//the 2012-06-14 16:27:22. 375 in the main: I==8
//the 2012-06-14 16:27:22. 484 in the main: I==9
//the 2012-06-14 16:27:22. 500 in testThread 1: I==1 s
//the 2012-06-14 16:27:22. 593 in the main: I==10
//the 2012-06-14 16:27:22. 703 in the main: I==11
//the 2012-06-14 16:27:22. 812 in the main: I==12
//the 2012-06-14 16:27:22. 921 in the main: I==13
//the 2012-06-14 16:27:23. 031 in the main: I==14
//the 2012-06-14 16:27:23. 140 in the main: I==15
//the 2012-06-14 16:27:23. 250 in the main: I==16
//the 2012-06-14 16:27:23. 359 in the main: I==17
//the 2012-06-14 16:27:23. 468 in the main: I==18
//the 2012-06-14 16:27:23. 500 in testThread 1: I==2 s
//the 2012-06-14 16:27:23. 578 in the main: I==19
//the 2012-06-14 16:27:23. 687 in the main: I==20
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related