升压错误锁定问题
trouble with boost error lock
我不知道这个简单代码的问题在哪里,我认为这是输出到控制台的麻烦,可能是死锁或其他什么,有人可以帮忙。
#include <iostream>
#include <string>
#include <sstream>
#include <boost/thread.hpp>
using namespace std;
struct IntegrateTask
{
int id;
double from, to, step, result;
IntegrateTask(int id, double from, double to, double step)
{
this -> id;
this -> from = from;
this -> to = to;
this -> step = step;
}
~IntegrateTask()
{}
};
vector<IntegrateTask> * tasks = new vector<IntegrateTask>();
boost::mutex mutlist;
boost::mutex iomutex;
boost::condition_variable condtask;
bool isInterrupted = false;
double foo(double x)
{
return x * x;
}
void integrate(IntegrateTask * task)
{
double result = 0;
double step = task -> step;
for(double i = task -> from ; i != task -> to; i =+ step)
{
result += foo(i) * step;
}
task -> result = result;
}
void integrateThread()
{
boost::thread::id id = boost::this_thread::get_id();
try
{
{
boost::mutex::scoped_lock iolock(iomutex);
cout << "Thread #" << id << " is working!" << endl;
}
while(!isInterrupted)
{
IntegrateTask * currtask = NULL;
{
boost::mutex::scoped_lock lock(mutlist);
while(!isInterrupted && tasks -> empty())
{
condtask.wait(lock);
}
if (!tasks -> empty())
{
currtask = &tasks->back();
tasks->pop_back();
}
}
if (currtask != NULL)
{
integrate(currtask);
boost::mutex::scoped_lock iolock(iomutex);
cout << "Task #" << (currtask->id) << "; result = " << (currtask->result) << endl;
}
}
boost::mutex::scoped_lock iolock(iomutex);
cout << "Thread # " << id << " stoped working normal!" << endl;
}
catch(boost::thread_interrupted)
{
boost::mutex::scoped_lock ioLock(iomutex);
cout << "Thread # " << id << " stoped working by interruption!" << endl;
}
catch(exception & e)
{
boost::mutex::scoped_lock iolock(iomutex);
cout << "Error: " << e.what() << endl;
}
}
int main()
{
cout << "Function for integration: f(x)=x*x" << endl;
cout << "For stopping program press EXIT" << endl;
int thcount = 6;// or boost::thread::hardware_concurrency()
boost::thread_group thgroup;
for (int i = 1; i <= thcount; i++){
thgroup.create_thread(&integrateThread);
}
int id = 0;
while (true)
{
string line;
{
boost::mutex::scoped_lock iolock(iomutex);
cout << "Task #" << ++id << "; left bound, right bound and step: ";
getline(cin, line);
}
if (line.find("e") != string::npos)
{
isInterrupted = true;
condtask.notify_all();
thgroup.interrupt_all();
thgroup.join_all();
return 0;
}
double from, to, step;
istringstream input(line);
input >> from >> to >> step;
IntegrateTask * task = new IntegrateTask(id, from, to, step);
{
boost::mutex::scoped_lock lock(mutlist);
tasks->push_back(*task);
}
condtask.notify_one();
}
}
我没有尝试遵循您在这里要实现的目标的逻辑,但是(至少)存在 2 个问题:
-
您没有在
IntegrateTask
的构造函数中使用id
。 您通常应该更喜欢构造函数体中的初始化列表而不是赋值,并且在函数签名中使用类成员变量名称也会导致灾难,因此我将构造函数更改为:IntegrateTask(int id_init, double from_init, double to_init, double step_init) : from(from_init), to(to_init), step(step_init), id(id_init) {}
- 挂起的可能原因是您在
integrate
的for
循环中使用了!=
。 如果您将其更改为<
您的程序不应该挂起,但我不确定这是否会破坏程序的逻辑。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- MCS 锁定实现的问题
- 仔细检查锁定问题,c++
- 以编程方式锁定注册表项以避免并发问题
- 线程读写锁定问题
- 升压错误锁定问题
- C++ 网络和锁定问题
- 问题5.3 QPlainTextEdit实现滚动锁定
- 这个解决方案对MSVC的双重检查锁定错误和函数静态有什么问题?
- 为什么lock_guard可以通过unique_lock获得一个已经锁定的互斥锁?-仍然有问题
- 发现可能由于线程锁定(可能)导致的性能问题
- Qt android屏幕锁定java问题
- perror()调用影响信号量锁定/解锁的问题