I wrote a program, which should write "1" in thread for three seconds. But when I try to debug or add controls outputs, I realize that the thread often isn't created (no outputs in the console or achievements of debug point). When I check the return value of CreateThread()
, it's ok. I read about log in file for I/O, but I think I don't need it. I want a program with two threads; one writing "1" for three seconds, and the second writing "2" for three seconds. Then, compare the result. Doesn't matter if "1" and "2" are mixed in the input file.
#include <iostream>
#include <fstream>
#include <windows.h>
#include <stdio.h>
#include <WinBase.h>
#include <ctime>
#define NTHREAD 2
std::ofstream myfile;
DWORD WINAPI fce1 (LPVOID a){
time_t timerStart, timerNow;
time(&timerStart);
timerNow = timerStart;
while((timerNow - timerStart) < 3)
{
myfile << "1";
myfile.flush();
time(&timerNow);
}
return 0;
}
int main()
{
HANDLE threads[NTHREAD];
DWORD dwThreads[NTHREAD];
myfile.open("file.txt");
threads[0] = CreateThread(NULL, 0, fce1, NULL, 0, &dwThreads[0]);
if (threads[0] == NULL){
printf("Error\n");
}
myfile.close();
return 0;
}