From f114aac0cff2f8435bab6733d3beae2b17d81c8a Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 15 Jan 2023 14:31:55 +0100 Subject: [PATCH] #7: Make `commentsCount` and `requestsPerChannel` compatible with multithreading --- main.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 3e2a655..4ae5762 100644 --- a/main.cpp +++ b/main.cpp @@ -42,9 +42,9 @@ set channelsAlreadyTreated; map channelsToTreat; map channelsToTreatRev; vector keys; -unsigned int commentsCount = 0, - commentsPerSecondCount = 0, - requestsPerChannel = 0; +unsigned int commentsPerSecondCount = 0; +map commentsCountThreads, + requestsPerChannelThreads; unsigned short THREADS_NUMBER = 1; string CHANNELS_DIRECTORY = "channels/", CHANNELS_FILE_PATH = "channels.txt", @@ -160,6 +160,9 @@ void treatChannels(unsigned short threadId) PRINT(threadId, "Treating channel " << channelToTreat << " (treated: " << channelsAlreadyTreated.size() << ", to treat: " << channelsToTreat.size() << ")") + commentsCountThreads[threadId] = 0; + requestsPerChannelThreads[threadId] = 0; + channelsToTreat.erase(channelsToTreatRev[channelToTreat]); channelsToTreatRev.erase(channelToTreat); @@ -181,14 +184,13 @@ void treatChannels(unsigned short threadId) deleteDirectory(channelToTreatDirectory); PRINT(threadId, "Deleting directory finished.") - PRINT(threadId, commentsCount << " comments were found for this channel.") - commentsCount = 0; - requestsPerChannel = 0; + PRINT(threadId, commentsCountThreads[threadId] << " comments were found for this channel.") } channelsAlreadyTreatedAndToTreatMutex.unlock(); } +// Have to pay attention not to recursively call this function with another channel otherwise we break the ability of the program to halt at any top level channel. void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, string channelToTreat) { string pageToken = ""; @@ -303,6 +305,7 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str } if(isChannel) { + // `CHANNELS` string pageToken = ""; while(true) { @@ -357,7 +360,7 @@ void treatComment(unsigned short threadId, json comment, string channelId) string channelId = snippet["authorChannelId"]["value"]; addChannelToTreat(threadId, channelId); } - commentsCount++; + commentsCountThreads[threadId]++; commentsPerSecondCount++; } @@ -478,8 +481,8 @@ json getJson(unsigned short threadId, string url, bool usingYoutubeDataApiv3, st } ostringstream toString; - toString << CHANNELS_DIRECTORY << directoryPath << "/" << requestsPerChannel << ".json"; - requestsPerChannel++; + toString << CHANNELS_DIRECTORY << directoryPath << "/" << requestsPerChannelThreads[threadId] << ".json"; + requestsPerChannelThreads[threadId]++; writeFile(threadId, toString.str(), "w", url + "\n" + content); return data;