Article ID: 114374
Article Last Modified on 10/2/2003
#define DIG_LEN 37
static char digits [ DIG_LEN + 1 ] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
// Within the thread, generate a template name
// using the thread id.
char templte[9];
unsigned tid = GetCurrentThreadId();
strcpy ( templte , "@@XXXXXX" ); // initialize template name
templte[0] = digits [ tid % DIG_LEN ]; // assign first character
// in template name using
// the tid to index into
// the digits array
templte[1] =
digits [ ( tid / DIG_LEN ) % DIG_LEN ]; // second character
path1 = _mktemp(templte);
/* Compile options needed: /MT /D_CONSOLE
*/
#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <io.h> /* _mktemp */
#include <stdio.h>
void Check1(void *);
void Check2(void *);
char *path1, *path2, *path3;
char templateName[4][9] = {"fnXXXXXX","fnXXXXXX","fnXXXXXX",
"tnXXXXXX"};
void main()
{
_beginthread(Check1, 0, NULL );
_beginthread(Check2,0,NULL);
Sleep(200L);
printf("Program finished\n");
}
/* Check */
void Check1( void * dummy )
{
FILE *fp;
path1 = _mktemp(templateName[0]);
Sleep(100L); // Give thread 2 a chance to call mktemp before the
// the file is actually opened
if (path1 != NULL)
if ((fp = fopen(path1,"w")) == NULL) {
printf("Thread 1 - Failed to open file %s\n",path2);
_endthread();
}
fclose(fp);
remove(path1);
printf("Thread 1 - File %s successfully opened\n",path1);
_endthread();
}
/* Check 2 */
void Check2( void * dummy )
{
FILE *fp;
// Path2 is set to the same as path1 which is INCORRECT
path2 = _mktemp(templateName[2]);
// Workaround is to specify an unused template name
if (path2 != NULL)
if ((fp = fopen(path2,"w")) == NULL) {
printf("Thread 2 - Failed to open file %s\n",path2);
_endthread();
}
fclose(fp);
remove(path2);
printf("Thread 2 - File %s successfully opened\n",path2);
_endthread();
}
Additional query words: 1.00
Keywords: kbcrt kbprb KB114374