boost-thread

Boost Threading : Mutex with Conditional Variable ,main thread hanging

烈酒焚心 提交于 2019-12-25 08:15:40
问题 I am trying to implement a state machine as a part of a class called" Source Transaction". Evey-time I receive a request in the main thread, it generates an instance of this class and the state machine starts executing until it reaches a state where it has to wait for a response in the main thread in the "event_handler". To solve this issue, I am implementing a conditional variable using boost libraries as following: Source Transaction Class boost::mutex mut; boost::condition_variable cond;

Lost in threads - how can I extend this example correctly?

放肆的年华 提交于 2019-12-25 06:46:04
问题 I am adapting this example to check if a message has taken too long to receive. read_complete is called, when read_start has finished. However, I have to check, if the received data is complete and interpret it as incomplete, if it is not fully received in a given time. These are my extensions to the code above in code/pseudocode: void read_complete(const boost::system::error_code& error, size_t bytes_transferred) { // the asynchronous read operation has now completed or failed and returned

Error in linking boost

◇◆丶佛笑我妖孽 提交于 2019-12-25 02:22:09
问题 I am new to boost threads and am trying to compile a simple example I found: #include <iostream> #include <boost/thread.hpp> #include <boost/date_time.hpp> void workerFunc() { boost::posix_time::seconds workTime(3); std::cout << "Worker: running" << std::endl; // Pretend to do something useful... boost::this_thread::sleep(workTime); std::cout << "Worker: finished" << std::endl; } int main(int argc, char* argv[]) { std::cout << "main: startup" << std::endl; boost::thread workerThread

C++: Timeout for an external application call

时光总嘲笑我的痴心妄想 提交于 2019-12-24 15:24:20
问题 For a c++ program I'm working on, I have to call an external application to do some operations. I can't modify the application. This operations may take too much time so I have to add a timeout. I tried using system() and boost threads int main() { [...] boost::thread t(function1); t.timed_join(boost::posix_time::seconds(10)); [...] return 0; } void function1() { system("external application"); } but when I return to the main 10 seconds later, the external application is still running in

mutex and threads independence

半世苍凉 提交于 2019-12-24 11:44:39
问题 I run the following program on a 32 cores computer: #include<iostream> #include<algorithm> #include<boost/thread.hpp> using namespace std; boost::thread_group g; boost::mutex _mtx; class A{ public: void foo() { for(int ix = 0; ix < 10000000; ++ix) vec.push_back(ix); sort(vec.rbegin(), vec.rend()); } private: vector<int> vec; }; void thread_fun() { A a; _mtx.lock(); //line 24 a.foo(); _mtx.unlock(); //line 26 } int main() { g.add_thread(new boost::thread(thread_fun)); g.add_thread(new boost:

Execute piece of code once per thread in OpenMP without default constructor

醉酒当歌 提交于 2019-12-24 05:46:18
问题 I try to write a parallel for loop using openMP V.2.0. In the middle of the parallel region I construct an Object which I would like to be constructed once per thread. #pragma omp parallel for for (long i = 0; i < static_cast<long>(general_triangles.size()); ++i) { TrianglePointer tri = general_triangles[i]; if (tri.GetClassification() == TO_CLASSIFY) { bool tri_has_correct_normal = true; // --- Construct tree once per thread --- Tree tree(*(gp_boolean_operator->mp_group_manager)); if (tree

ofstream shared by mutiple threads - crashes after awhile

孤者浪人 提交于 2019-12-24 03:05:43
问题 this function takes a ofstream as a reference, then builds struct packets and threads off the structs with the ofstream to a trie matching class. A stack is returned with n matches in order of match distance. The ofstream, packet, and stack are then passed by reference to a print function that writes the matches to the same output file - waiting for when the ofstream is not in use. The problem is the ofstream crashes after half the matches are written. The ofstream, packet, and outfile header

boost thread on interruption doesn't print exit message

偶尔善良 提交于 2019-12-24 01:58:20
问题 I have this piece of code for executing three threads where the second thread should get interrupted on pressing enter and print the exit message: void input_val() { // DO STUFF return; } void process_val() { // DO STUFF try{ cout << "waiting for ENTER..." << endl; boost::this_thread::sleep(boost::posix_time::milliseconds(200)); } catch(boost::thread_interrupted&){ cout << "exit process thread" << endl; return; } return; } void output_val() { // DO STUFF } int main() { char key_pressed; boost

Interrupting boost thread

我们两清 提交于 2019-12-24 00:27:24
问题 I would like to be able to interrupt a thread as follows. void mainThread(char* cmd) { if (!strcmp(cmd, "start")) boost::thread thrd(sender); //start thread if (!strcmp(cmd, "stop")) thrd.interrupt(); // doesn't work, because thrd is undefined here } thrd.interrupt() is not possible because thrd object is undefined when I try to interrupt it. How can I fix this? 回答1: Use the move assignment operator: void mainThread(char* cmd) { boost::thread thrd; if (!strcmp(cmd, "start")) thrd = boost:

How to call Python from a boost thread?

蓝咒 提交于 2019-12-23 19:55:17
问题 I have a Python app that calls a C++ boost python library and it all works. However, I have a callback C++ to Python scenario where C++ from a boost thread calls python and I get an access violation on the C++ side. If I do exactly the same callback using the python thread it works perfectly. Therefore I suspect that I can not simply callback Python from C++ using a boost thread but need to do something extra for it to work? 回答1: The most likely culprit is that the Global Interpreter Lock