[英]How can I create a variable number of threads?
我正在嘗試創建一個程序來添加和減去全局變量。 加減功能將由線程處理。 如何不斷加/減直到全局變量達到某個閾值?
假設我有以下代碼,
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <numeric>
#include <cmath>
#include <sstream>
#include <thread>
#include <chrono>
#include <ctime>
#include <mutex>
int GetRandom(int max){
//generate a pseudo-random number with time as the seed
//time as seed will always return a different starting point
srand(time(NULL));
//mod max to ensure we return a number below max
return rand() % max;
}
std::string GetTime(){
auto nowTime = std::chrono::system_clock::now();
std::time_t sleepTime =
std::chrono::system_clock::to_time_t(nowTime);
return std::ctime(&sleepTime);
}
double acctBalance = 10;
// Protects shared data from being accessed at the
// same time
std::mutex acctLock;
void Add(int id,
double adding, int sleep){
int rand = GetRandom(10);
// The exception safe way to protect access
// to code within its scope. The lock is released
// after execution leaves this scope
std::lock_guard<std::mutex> lock(acctLock);
// Blocks access between lock and unlock
// until execution completes
// This isn't good to use however if an error
// occurs between lock and unlock
// acctLock.lock();
std::this_thread::sleep_for(std::chrono::seconds(sleep));
std::cout << id <<
": adding : " <<
rand << " on " <<
GetTime() << ": slept for " << sleep<<"seconds"<< "\n";
if((acctBalance - rand) <= 20){
acctBalance += rand;
std::cout << "New Account Balance is $" <<
acctBalance << "\n";
} else {
std::cout << "Not Enough Money in Account\n";
std::cout << "Current Balance is $" <<
acctBalance << "\n";
}
// acctLock.unlock();
}
void Subtract(int id, double subtract, int sleep){
int rand = GetRandom(10);
// The exception safe way to protect access
// to code within its scope. The lock is released
// after execution leaves this scope
std::lock_guard<std::mutex> lock(acctLock);
// Blocks access between lock and unlock
// until execution completes
// This isn't good to use however if an error
// occurs between lock and unlock
// acctLock.lock();
std::this_thread::sleep_for(std::chrono::seconds(sleep));
std::cout << id <<
" tries to withdrawal $" <<
rand << " on " <<
GetTime() << ": slept for " << sleep<<"seconds"<<"\n";
if((acctBalance - rand) <=20){
acctBalance -= rand;
std::cout << "New Account Balance is $" <<
acctBalance << "\n";
} else {
std::cout << "Not Enough Money in Account\n";
std::cout << "Current Balance is $" <<
acctBalance << "\n";
}
// acctLock.unlock();
}
int main()
{
// We will create a pool of threads that
// will access a bank account in no particular
// order
std::thread threads[2];
int rand = 3;
int x = 10;
threads[0] = std::thread(Add, 0, 15,rand);
threads[1] = std::thread(Subtract, 1, 15,rand);
for(int i = 0; i < 2; ++i){
threads[i].join();
}
return 0;
}
我目前創建了 2 個線程,1 個用於加法,另一個用於減法。 但是我的程序每次只會對全局變量進行一次加減運算。 我想我應該創建一個動態的線程數組,該數組會不斷增長,直到 acctBalance 達到某個數字。 我將如何實現這一目標?
這里沒有什么復雜的,只需創建一個向量並向其添加線程:
std::vector<std::thread> threads;
threads.reserve(count);
for (size_t i = 0; i < count; i++)
{
threads.emplace_back(Add, 0, 15,rand);
}
for (auto& thread : threads)
{
thread.join();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.