daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include <iostream>
  2. #include <string>
  3. #include <thread>
  4. #include <vector>
  5.  
  6. void culc(std::string* s1, int begin, int end) {
  7.   for (int i = begin; i < end; i++) {
  8.     (*s1)[i] = 'a' + ((*s1)[i] + 3 - 'a') % 26;
  9.   }
  10. }
  11.  
  12. void CaesarEncrypt(std::string* s) {
  13.   std::thread thread;
  14.   int k = 1;
  15.   int k1 = 2;
  16.   std::vector<std::thread*> threads;
  17.   int N = thread.hardware_concurrency();
  18.   auto tr = new std::thread(culc, std::ref(s), 0, s->size() / N);
  19.   threads.push_back(tr);
  20.   for (int i = 0; i < N - 1; i++) {
  21.     if (i == N - 2) {
  22.       auto t = new std::thread(culc, std::ref(s), s->size() / N * k, s->size());
  23.       threads.push_back(t);
  24.     } else {
  25.       auto t = new std::thread(culc, std::ref(s), s->size() / N * k,
  26.                                s->size() / N * k1);
  27.       k1++;
  28.       k++;
  29.       threads.push_back(t);
  30.     }
  31.   }
  32.   for (int i = 0; i < N; i++) {
  33.     std::cout << threads[i]->get_id() << std::endl;
  34.     threads[i]->join();
  35.   }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top