I am quite new to C++, and I am trying to understand how threads work. I have encountered an interesting task. It was: “Create a program that runs 10 threads. Each of them has to run a specific ‘threadFunction()’. This thread function has to have a simple loop inside to make it look like it does something important that takes a long time. After the end of this function, the thread should end its existence by itself. Also, every time this function is called, it should increase some global counter by 1. But when any of those 10 threads finishes its execution, any other thread should not increase the counter, and if possible, new threads should not be created (an additional but not necessary requirement). If the program is being executed in debug mode, it should write that ‘thread 123 started’ or ‘thread 123 finished’ and the counter’s value should be added to the end of the line.”In fact, I wrote a possible solution to this task, but the problem is that it is correct in 70–80% of tests (results seem to be random). Here is the code:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <atomic>
// debug mode switcher
#define DEBUG
std::atomic<int> functionCallCount(0);
std::atomic<bool> stopThreads(false);
#ifdef DEBUG
std::ofstream logFile;
CRITICAL_SECTION logCriticalSection;
#define LOG(msg) do {
EnterCriticalSection(&logCriticalSection);
logFile << msg << std::endl;
LeaveCriticalSection(&logCriticalSection);
} while(0)
void InitializeLogging() {
InitializeCriticalSection(&logCriticalSection);
logFile.open("log.txt");
}
void CloseLogging() {
logFile.close();
DeleteCriticalSection(&logCriticalSection);
}
#else
#define LOG(msg)
void InitializeLogging() {}
void CloseLogging() {}
#endif
DWORD WINAPI ThreadFunction(LPVOID lpParam) {
if (!stopThreads) {
functionCallCount += 1; // thread function call count
}
LOG("Thread " << GetCurrentThreadId() << " started.");
for (int i = 0; i < 100000; ++i) { // loop for continuous execution
}
if(stopThreads.load() != true)
{
stopThreads = true;
LOG("Thread " << GetCurrentThreadId() << " finished.");
}
return 0;
}
int main() {
const int numThreads = 10;
std::vector<HANDLE> threads(numThreads);
InitializeLogging();
for (int i = 0; i < numThreads; ++i) {
if (!stopThreads.load()) {
threads[i] = CreateThread(
NULL, // safety attributes
0, // stack size
ThreadFunction, // thread func
NULL, // thread func parameter
0, // thread creation flag
NULL // thread identificator
);
}
else {
break;
}
}
// Waiting for all threads to complete
WaitForMultipleObjects(numThreads, threads.data(), TRUE, INFINITE);
// Forcefully terminate all threads
for (HANDLE thread : threads) {
if (WaitForSingleObject(thread, 0) != WAIT_OBJECT_0) {
TerminateThread(thread, 0);
}
CloseHandle(thread);
}
LOG("Total function call count: " << functionCallCount.load());
CloseLogging();
std::cout << "Total function call count: " << functionCallCount.load() << std::endl;
return 0;
}
As for the result I am getting something like that. It is a correct result:
Thread 14556 started.
Thread 14612 started.
Thread 16092 started.
Thread 12664 started.
Thread 22828 started.
Thread 17928 started.
Thread 4916 started.
Thread 22776 started.
Thread 14556 finished.
Thread 1116 started.
Thread 1280 started.
Total function call count: 8
But sometimes something like this can happen:
Thread 21476 started.
Thread 22488 started.
Thread 18036 started.
Thread 21476 finished.
Thread 21264 started.
Thread 13288 started.
Thread 23220 started.
Thread 17992 started.
Thread 3308 started.
Thread 10432 started.
Thread 21472 started.
Total function call count: 5
It is incorrect and it should return Total function call count: 3
Great thanks to everyone who can help me understand this problem.