boost-thread

boost::lockfree::spsc_queue busy wait strategy. Is there a blocking pop?

醉酒当歌 提交于 2019-11-28 23:51:49
So i'm using a boost::lockfree::spec_queue to communicate via two boost_threads running functors of two objects in my application. All is fine except for the fact that the spec_queue::pop() method is non blocking. It returns True or False even if there is nothing in the queue. However my queue always seems to return True (problem #1). I think this is because i preallocate the queue. typedef boost::lockfree::spsc_queue<q_pl, boost::lockfree::capacity<100000> > spsc_queue; This means that to use the queue efficiently i have to busy wait constantly popping the queue using 100% cpu. Id rather not

Designing a thread-safe copyable class

余生颓废 提交于 2019-11-28 20:53:22
The straightforward way to make a class threadsafe is to add a mutex attribute and lock the mutex in the accessor methods class cMyClass { boost::mutex myMutex; cSomeClass A; public: cSomeClass getA() { boost::mutex::scoped_lock lock( myMutex ); return A; } }; The problem is that this makes the class non-copyable. I can make things work by making the mutex a static. However, this means that every instance of the class blocks when any other instance is being accessed, because they all share the same mutex. I wonder if there is a better way? My conclusion is that there is no better way. Making a

Boost.Thread Linking - boost_thread vs. boost_thread-mt

末鹿安然 提交于 2019-11-28 09:39:47
It's not clear to me what linking options exist for the Boost.Thread 1.34.1 library. I'm on Ubuntu 8.04 and I've found that when using either boost_thread or boost_thread-mt during linking both compile and run, but I don't see any documentation on these or any other linking options in above link. What Boost.Thread linking options are available and what do they mean? Well... The first amusing thing is that the -mt modifier in the name is to indicate the library is Ok for multithreading. Which could lead us to believe that boost_thread (without this modifier) could be multithread-unsafe... But

How can I achieve something similar to a semaphore using boost in c++? [duplicate]

不想你离开。 提交于 2019-11-28 09:21:43
This question already has an answer here: C++0x has no semaphores? How to synchronize threads? 9 answers I noticed that boost does not seem to support semaphores. What's the easiest way to achieve a similar effect? You either need Boost Interprocess semaphore or Boost Thread synchronization primitives. Mutex/Lock and condition are primitives that are commonly used to synchronize access to shared resources across multiple threads of a single process. There are exclusive , readers-writer and recursive/reentrant types of mutexes. Mutex, in other words, is an exclusive lock. Condition is used to

boost::asio with boost::unique_future

自作多情 提交于 2019-11-28 07:02:45
According to http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/overview/cpp2011/futures.html , we can use boost::asio with std::future . But I couldn't find any information about working with boost::unique_future , which has more functions, such as then() . How can I use? Boost.Asio only provides first-class support for asynchronous operations to return a C++11 std::future or an actual value in stackful coroutines . Nevertheless, the requirements on asynchronous operations documents how to customize the return type for other types, such as Boost.Thread's boost::unique_future . It

How to pass an argument to boost::thread?

╄→гoц情女王★ 提交于 2019-11-28 05:57:30
thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) ); is it possible that run has an argument like this : void clientTCP::run(boost:function<void(std::string)> func); and if yes how my boost::thread call should be written Thanks. The following code boost::bind( &clientTCP::run , this ) defines a function callback. It calls the function run on the current instance ( this ). With boost::bind you can do the following: // Pass pMyParameter through to the run() function boost::bind(&clientTCP::run, this, pMyParameter) See the documentation and example

How do I make the boost/asio library repeat a timer?

我们两清 提交于 2019-11-28 05:50:54
问题 Here is the Code given on the Boost library documentation. #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void print(const boost::system::error_code& /*e*/) { std::cout << "Hello, world!\n"; } int main() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.async_wait(print); io.run(); return 0; } Now when I run the above program it just waits for 5 seconds and then prints Hello World and stop. I

Trying to link Boost 1.52 thread

*爱你&永不变心* 提交于 2019-11-27 23:53:42
I am trying to compile my program but it wouldn't link at all. I have specified the path to the boost lib files and the linker still complain. Here's the linking error I got: 1>Edproj.obj : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAABVerror_category@12@XZ) 1>Edproj.obj : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAABVerror_category@12@XZ) 1>Edproj

How to use lock_guard when returning protected data

我的未来我决定 提交于 2019-11-27 23:31:14
I have a question concerning the use of boost::lock_guard (or similar scoped locks) and using variables that should be protected by the lock in a return statement. How is the order of destroying local objects and copying the return value? How does return value optimization affect this? Example: Data Class::GetData() { boost::lock_guard<boost::mutex> lock(this->mMutex); return this->mData; } Would this be correct (if mData is the variable protected by mMutex)? Or would I have to use a local scope and a temporary like shown in the example below: Data Class::GetData() { Data ret; { boost::lock

Can I create a software watchdog timer thread in C++ using Boost Signals2 and Threads?

北城余情 提交于 2019-11-27 23:06:07
I am running function Foo from somebody else's library in a single-threaded application currently. Most of the time, I make a call to Foo and it's really quick, some times, I make a call to Foo and it takes forever. I am not a patient man, if Foo is going to take forever, I want to stop execution of Foo and not call it with those arguments. What is the best way to call Foo in a controlled manner (my current environment is POSIX/C++) such that I can stop execution after a certain number of seconds. I feel like the right thing to do here is to create a second thread to call Foo, while in my main