2012-04-25 144 views
-2

我不知道这个简单的代码问题在哪里,我认为这是输出到控制台的问题可能死锁或什么的,可以有人请帮助。麻烦升压错误锁

#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();  
    } 

} 
+3

-1:如果你想问一个好问题,清楚地说明你的问题的性质。出了什么问题?错误?没有“可能是僵局”这样的事情。要么是死锁,要么显示调试器的回溯,要么不显示。 – thiton 2012-04-25 18:39:59

+0

也在此处发布代码,没有指向googledoc或pastebin的链接 – CharlesB 2012-04-25 18:41:10

+0

integration()中for循环中的= +可能应该是+ =,除此之外请参阅[thiton的评论](http://stackoverflow.com/questions/) 10321674 /故障与升压错误锁#comment13288551_10321674)。 – DerKuchen 2012-04-25 20:14:50

回答

1

我没有试过跟随你的目标在这里实现什么样的逻辑,但也有2个问题(至少):

  1. 您尚未使用idIntegrateTask的构造函数。通常应该偏向于在构造器分配初始化列表,并在功能上的签名使用的类成员变量名是一个灾难太多,所以我想你的构造函数更改为:

    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) {} 
    
  2. 挂的可能原因您在integratefor循环中使用!=。如果您将其更改为<,那么您的程序不应该挂起,但我不确定这是否违反了程序的逻辑。