Created
January 23, 2018 10:54
-
-
Save Zikoel/6238570e2176c3b4cd68a53324b9a054 to your computer and use it in GitHub Desktop.
Code for evidence that errno was thread safe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <thread> | |
#include <vector> | |
#include <dirent.h> | |
#include <cerrno> | |
struct ScanResult { | |
int returnCode; | |
int errnoCode; | |
}; | |
ScanResult scandir(std::string path) | |
{ | |
struct dirent** dents = NULL; | |
int n = scandir(path.c_str(), &dents, NULL, NULL); | |
ScanResult result = {n, errno}; | |
return result; | |
} | |
void runner(std::string path, size_t iterations, int expectedReturnCode, int expectedErrnoCode) | |
{ | |
int allOk = true; | |
for(size_t i = 0; i < iterations; i++) | |
{ | |
ScanResult r = scandir(path); | |
if ( r.returnCode != expectedReturnCode || r.errnoCode != expectedErrnoCode ) | |
{ | |
allOk = false; | |
std::cout << "juston we have a problem: return code expected " | |
<< expectedReturnCode | |
<< " (found " << r.returnCode << ")" | |
<< " errno expected " << expectedErrnoCode | |
<< " (found " << r.errnoCode << ")" | |
<< std::endl; | |
} | |
} | |
std::this_thread::sleep_for(std::chrono::milliseconds(25 + ( std::rand() % ( 63 - 25 + 1 ) ))); | |
if ( !allOk ) { | |
std::cout << "Something goes wrong scan " << path << std::endl; | |
} else { | |
std::cout << "All ok from worker scanning " << path << std::endl; | |
} | |
} | |
int main() | |
{ | |
std::string aValidDir = "fixture"; | |
std::string aNotValidDir = "fixture/empty_file"; | |
std::vector<std::thread*> threads; | |
std::thread* t = new std::thread(runner, aValidDir, 1000*1000, 3, 0); | |
std::thread* t2 = new std::thread(runner, aNotValidDir, 1000*1000, -1, ENOTDIR); | |
threads.push_back(t); | |
threads.push_back(t2); | |
for(auto && t : threads) { | |
t->join(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment