prints as pi
This commit is contained in:
73
main.cpp
73
main.cpp
@@ -1,4 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
@@ -9,16 +11,14 @@
|
||||
|
||||
|
||||
void threadWorker(std::uint16_t threadNum, FifoQueue* threadSafeQueue, Map* threadSafeMap) {
|
||||
while(!threadSafeQueue->empty()){
|
||||
int index = threadSafeQueue->front();
|
||||
threadSafeQueue->pop();
|
||||
unsigned int piDigit = computePiDigit(index);
|
||||
threadSafeMap->safeInsert(index, piDigit);
|
||||
std::cout << ".";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
|
||||
while(!threadSafeQueue->empty()){
|
||||
int index = threadSafeQueue->front();
|
||||
threadSafeQueue->pop();
|
||||
unsigned int piDigit = computePiDigit(index);
|
||||
threadSafeMap->safeInsert(index, piDigit);
|
||||
std::cout << ".";
|
||||
std::cout.flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,24 +29,49 @@ int main() {
|
||||
for(int i = 1; i <= 1000; i++){
|
||||
threadSafeQueue->push(i);
|
||||
}
|
||||
//
|
||||
// Make as many threads as there are CPU cores
|
||||
//
|
||||
// Make as many threads as there are CPU cores
|
||||
// Assign them to run our threadWorker function, and supply arguments as necessary for that function
|
||||
std::vector<std::shared_ptr<std::thread>> threads;
|
||||
for (std::uint16_t core = 0; core < std::thread::hardware_concurrency(); core++)
|
||||
std::vector<std::shared_ptr<std::thread>> threads;
|
||||
for (std::uint16_t core = 0; core < std::thread::hardware_concurrency(); core++)
|
||||
// The arguments you wish to pass to threadWorker are passed as
|
||||
// arguments to the constructor of std::thread
|
||||
threads.push_back(std::make_shared<std::thread>(threadWorker, core, threadSafeQueue, threadSafeMap));
|
||||
threads.push_back(std::make_shared<std::thread>(threadWorker, core, threadSafeQueue, threadSafeMap));
|
||||
|
||||
//
|
||||
// Wait for all of these threads to complete
|
||||
for (auto&& thread : threads)
|
||||
thread->join();
|
||||
//
|
||||
// Wait for all of these threads to complete
|
||||
for (auto&& thread : threads)
|
||||
thread->join();
|
||||
std::cout << std::endl << std::endl;
|
||||
|
||||
std::cout << std::endl << std::endl << "3.";
|
||||
|
||||
for(int i = 1; i <= 1000; i++){
|
||||
std::cout << threadSafeMap->find(i);
|
||||
std::ifstream piShape("../piShape.txt");
|
||||
if(piShape.is_open()){
|
||||
std::string c;
|
||||
int t = 0;
|
||||
int j = 1;
|
||||
while(getline(piShape, c)){
|
||||
for(int i = 0; i < c.size(); i++){
|
||||
if(c[i] == ' '){
|
||||
std::cout << " ";
|
||||
}
|
||||
else{
|
||||
if(t == 0){
|
||||
std::cout << "3";
|
||||
t++;
|
||||
}
|
||||
else if(t == 1){
|
||||
std::cout<<".";
|
||||
t++;
|
||||
}else{
|
||||
std::cout << threadSafeMap->find(j); j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
else{std::cout << "file piShape.txt not found or could not be opened" << std::endl;}
|
||||
|
||||
// std::cout << threadSafeMap->find(i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user