Note that there are some explanatory texts on larger screens.

plurals
  1. POtrouble with boost error lock
    primarykey
    data
    text
    <p>I can't figure out where is the problem with this simple code, I think that here is the trouble with output to Console maybe deadlock or something, can somebody, please help.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include &lt;boost/thread.hpp&gt; using namespace std; struct IntegrateTask { int id; double from, to, step, result; IntegrateTask(int id, double from, double to, double step) { this -&gt; id; this -&gt; from = from; this -&gt; to = to; this -&gt; step = step; } ~IntegrateTask() {} }; vector&lt;IntegrateTask&gt; * tasks = new vector&lt;IntegrateTask&gt;(); 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 -&gt; step; for(double i = task -&gt; from ; i != task -&gt; to; i =+ step) { result += foo(i) * step; } task -&gt; result = result; } void integrateThread() { boost::thread::id id = boost::this_thread::get_id(); try { { boost::mutex::scoped_lock iolock(iomutex); cout &lt;&lt; "Thread #" &lt;&lt; id &lt;&lt; " is working!" &lt;&lt; endl; } while(!isInterrupted) { IntegrateTask * currtask = NULL; { boost::mutex::scoped_lock lock(mutlist); while(!isInterrupted &amp;&amp; tasks -&gt; empty()) { condtask.wait(lock); } if (!tasks -&gt; empty()) { currtask = &amp;tasks-&gt;back(); tasks-&gt;pop_back(); } } if (currtask != NULL) { integrate(currtask); boost::mutex::scoped_lock iolock(iomutex); cout &lt;&lt; "Task #" &lt;&lt; (currtask-&gt;id) &lt;&lt; "; result = " &lt;&lt; (currtask-&gt;result) &lt;&lt; endl; } } boost::mutex::scoped_lock iolock(iomutex); cout &lt;&lt; "Thread # " &lt;&lt; id &lt;&lt; " stoped working normal!" &lt;&lt; endl; } catch(boost::thread_interrupted) { boost::mutex::scoped_lock ioLock(iomutex); cout &lt;&lt; "Thread # " &lt;&lt; id &lt;&lt; " stoped working by interruption!" &lt;&lt; endl; } catch(exception &amp; e) { boost::mutex::scoped_lock iolock(iomutex); cout &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; endl; } } int main() { cout &lt;&lt; "Function for integration: f(x)=x*x" &lt;&lt; endl; cout &lt;&lt; "For stopping program press EXIT" &lt;&lt; endl; int thcount = 6;// or boost::thread::hardware_concurrency() boost::thread_group thgroup; for (int i = 1; i &lt;= thcount; i++){ thgroup.create_thread(&amp;integrateThread); } int id = 0; while (true) { string line; { boost::mutex::scoped_lock iolock(iomutex); cout &lt;&lt; "Task #" &lt;&lt; ++id &lt;&lt; "; 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 &gt;&gt; from &gt;&gt; to &gt;&gt; step; IntegrateTask * task = new IntegrateTask(id, from, to, step); { boost::mutex::scoped_lock lock(mutlist); tasks-&gt;push_back(*task); } condtask.notify_one(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload