You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
2.3 KiB
C++
121 lines
2.3 KiB
C++
//
|
|
// Created by anton on 6/22/22.
|
|
//
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <functional>
|
|
#include <unordered_map>
|
|
#include <stack>
|
|
#include <queue>
|
|
#include <memory>
|
|
#include <vector>
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <chrono>
|
|
#include <atomic>
|
|
#include <random>
|
|
#include <sodium.h>
|
|
#include <unordered_set>
|
|
#include <map>
|
|
#include <optional>
|
|
|
|
#include "call_frame.h"
|
|
#include "global_thread_pool.h"
|
|
#include "helpers.h"
|
|
|
|
using namespace pmp::core;
|
|
|
|
|
|
|
|
|
|
void *task1(void *args, void *scope) {
|
|
//std::cout << "hello from task 1" << std::endl;
|
|
|
|
assert(("Scope can't be nullptr", scope != nullptr));
|
|
|
|
auto *print_lock = reinterpret_cast<std::mutex *>(scope);
|
|
|
|
using u64 = unsigned long long int;
|
|
|
|
u64 word = 0;
|
|
u64 len = 0;
|
|
|
|
long long int seed = 0;
|
|
|
|
std::random_device dev;
|
|
|
|
// FIXME: random_device entropy seems to be stuck at 0.
|
|
// currently falling back to libsodium, but that's not sustainable.
|
|
if (dev.entropy() == 0) {
|
|
randombytes_buf(&seed, sizeof(long long int));
|
|
} else {
|
|
seed = dev();
|
|
}
|
|
|
|
std::mt19937 rng(seed);
|
|
std::uniform_int_distribution<std::mt19937::result_type> next_number(0, 26);
|
|
|
|
std::map<u64, u64> lengths;
|
|
//std::unordered_map<u64, u64> words;
|
|
|
|
for (u64 i = 0; i < 10000000; i++) {
|
|
unsigned long num = next_number(rng);
|
|
|
|
if (num == 0) {
|
|
lengths[len]++;
|
|
//words[word]++;
|
|
word = 0;
|
|
len = 0;
|
|
continue;
|
|
}
|
|
|
|
word *= 26;
|
|
word += num;
|
|
len++;
|
|
}
|
|
|
|
print_lock->lock();
|
|
|
|
std::cout << "Word lengths:\n";
|
|
for (auto& pair : lengths) {
|
|
if (pair.first > 20) {
|
|
break;
|
|
}
|
|
std::cout << pair.first << ": " << pair.second << "\n";
|
|
}
|
|
|
|
std::cout << "Most used words:\n";
|
|
|
|
print_lock->unlock();
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
int main() {
|
|
sodium_init();
|
|
|
|
std::cout << "Hello, World!" << std::endl;
|
|
|
|
auto pool = global_thread_pool(nullptr);
|
|
std::mutex stdout_lock;
|
|
|
|
std::cout << "Using " << pool.thread_count() << " workers" << std::endl;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
pool.insert_frame(nullptr, &stdout_lock, task1, 0);
|
|
}
|
|
|
|
std::cout << pool.insert_frame(nullptr, &stdout_lock, task1,0) << std::endl;
|
|
|
|
pool.join();
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|