如何创建可变数量的线程



我正在尝试创建一个程序,该程序将对全局变量进行加减运算。加法和减法功能将由线程处理。如何不断地进行加法/减法运算,直到全局变量达到某个阈值?

假设我有以下代码,


#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 Accountn";
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 Accountn";
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;
}

我目前已经创建了两个线程,一个用于加法,另一个用于减法。但我的程序只会对全局变量进行一次加减运算。我想我应该创建一个动态的线程数组,直到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();
}

最新更新