boost-thread

Boost threads - passing parameters by reference

这一生的挚爱 提交于 2019-12-04 12:16:43
My application has a section that resembles the following code void SomeClass::OtherMethod(std::vector<std::string>& g) { g.pushback("Something"); } void SomeClass::SomeMethod() { std::vector<std::string> v; boost::thread t(boost::bind(&SomeClass::OtherMethod,this,v) t.join(); std::cout << v[0]; //Why is this empty when the vector created on stack } I wanted to know why the vector v is empty when the vector is created on the stack and it works when it is created on the heap. I was expecting the above code to work since the vector remains in scope even when it is created on the stack. Bind

How to pass function parameters to boost::thread_groups::create_thread()

人盡茶涼 提交于 2019-12-04 10:49:37
问题 I am new to Boost.Threads and am trying to understand how to pass function arguments to the boost::thread_groups::create_thread() function. After reading some tutorials and the boost documentations, I understand that it is possible to simply pass the arguments to this function but I can't get this method to work. The other method I read about is to use functors to bind the parameters to my function but that would create copies of the arguments and I strictly require that const references be

Boost Thread - How to acknowledge interrupt

核能气质少年 提交于 2019-12-04 10:26:57
问题 I have blocking task which will be performed by find_the_question() function. However, I do not want thread executing this function take more than 10 seconds. So in case it takes more than 10 seconds, I want to close that thread with cleaning all the resources. I tried to write a code for that, but somehow I am not able to get a interrupt in find_the_question() function if thread takes more than 10 seconds. Could you please tell me what am I doing wrong? void find_the_question(std::string

Am I over-engineering per-thread signal blocking?

血红的双手。 提交于 2019-12-04 04:57:15
In my applications, I generally want to intercept SIGINT and SIGTERM signals in order to close down gracefully. In order to prevent worker threads from "stealing" signals, I do this in the entrypoint for each: // Block signals in this thread sigset_t signal_set; sigaddset(&signal_set, SIGINT); sigaddset(&signal_set, SIGTERM); sigaddset(&signal_set, SIGHUP); sigaddset(&signal_set, SIGPIPE); pthread_sigmask(SIG_BLOCK, &signal_set, NULL); If I don't, when I perform Ctrl + C , some of the time (it's unspecified as to which thread will get the signal) my handlers in the base thread won't be invoked

How to use boost::bind with non-copyable params, for example boost::promise?

不羁的心 提交于 2019-12-04 02:57:44
Some C++ objects have no copy constructor, but have move constructor. For example, boost::promise. How can I bind those objects using their move constructors ? #include <boost/thread.hpp> void fullfil_1(boost::promise<int>& prom, int x) { prom.set_value(x); } boost::function<void()> get_functor() { // boost::promise is not copyable, but movable boost::promise<int> pi; // compilation error boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1); // compilation error as well boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1); // PS. I know, it is possible to

What does boost::thread sleep() do?

半腔热情 提交于 2019-12-04 01:06:47
I am currently working on a small wrapper class for boost thread but I dont really get how the sleep function works, this is what I have got so far: BaseThread::BaseThread(){ thread = boost::thread(); bIsActive = true; } BaseThread::~BaseThread(){ join(); } void BaseThread::join(){ thread.join(); } void BaseThread::sleep(uint32 _msecs){ if(bIsActive) boost::this_thread::sleep(boost::posix_time::milliseconds(_msecs)); } This is how I implemented it so far but I dont really understand how the static this_thread::sleep method knows which thread to sleep if for example multiple instances of my

shared_ptr Assertion px != 0 failed

こ雲淡風輕ζ 提交于 2019-12-03 22:07:21
I have a fairly complex multi threaded application (server) that from time to time will crash due to an assert: /usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = msg::Player]: Assertion `px != 0' failed. I have been unable to pinpoint the cause and was wondering if this is a problem with boost::shared_ptr or it is me? I tried g++ 4.4.3-4ubuntu5 and llvm-g++ (GCC) 4.2.1 with optimization and without optimization and libboost1.40-dev (= 1.40.0-4ubuntu4). There should be no problem with using boost::shared_ptr as long

Creating a boost::thread with boost::bind() or without

北慕城南 提交于 2019-12-03 17:01:40
问题 Some people seem to launch boost::threads using the boost::bind() function, like in the accepted answer of the following question: Using boost thread and a non-static class function Whereas other people don't use it at all, like in the answer with the most upvotes of this question: Best way to start a thread as a member of a C++ class? So, what's the difference, if it exists? 回答1: As you can see by the code below that compile and gives the expected output, boost::bind is completely

When using boost::thread::interrupt(), do you *need* to catch the thread_interrupted exception?

落花浮王杯 提交于 2019-12-03 13:12:48
I've got several long running boost threads that I want to be able to shut down by interrupting them. All of the documentation I can find says that you can catch the thread_interrupted exception, but it doesn't really say what happens if you don't. I would assume it kills the thread (and hopefully the thread gets properly cleaned up). But then does the exception die off with the thread? Or does it get passed up to the main thread and kill it too? The exception is just like any other C++ exception. If you choose not to catch it, it will cause the same effect as any other unhandled exception. If

Is a boost::thread automatically removed from a boost::thread_group when it terminates?

…衆ロ難τιáo~ 提交于 2019-12-03 12:39:40
( This question , though similar, didn't really answer my question.) I've had problems with my own "thread group" implementation , and being no closer to solving or even identifying the issue, I'm looking into just using boost::thread_grp . Now, from what documentation I can find on the subject 1 , I've always believed that thread objects — no matter the duration of their actual work — remain alive and a part of a thread group until the thread group is destroyed. However, a cursory test seems to indicate that boost::thread_group::size() decreases on its own as threads do their work and