C++, signal and threads

I'm having some hard time designing the main workflow of my application, using threads and signals. My goal here is to have a main thread, catching signals, and n other threads doing stuff periodically (actually using sensors on a raspberry pi, and saving retrieved data). I want the ability to close the program in a clean way, ie wait that a sensor has finished writing data (if they are wh

C ++,信号和线程

我很难用线程和信号设计我的应用程序的主要工作流程。 我的目标是有一个主线程,捕获信号和其他n个线程周期性地执行任务(实际上使用树莓派上的传感器,并保存检索到的数据)。 我希望能够以一种干净的方式关闭程序,即等待传感器在关闭之前完成数据写入(如果是信号发生时)。 我正在使用C ++ 11。 现在,我有这个例子: #include <iostream> #include <thread> #include <csignal> #include <mutex

How to prevent threads from starvation in C++11

I am just wondering if there is any locking policy in C++11 which would prevent threads from starvation. I have a bunch of threads which are competing for one mutex. Now, my problem is that the thread which is leaving a critical section starts immediately compete for the same mutex and most of the time wins. Therefore other threads waiting on the mutex are starving. I do not want to let the

如何防止线程在C ++ 11中挨饿

我只是想知道在C ++ 11中是否有锁定策略可以防止线程挨饿。 我有一堆线程正在争夺一个互斥体。 现在,我的问题是,离开关键部分的线程立即开始竞争相同的互斥体,并且大部分时间赢得胜利。 因此等待互斥体的其他线程正在挨饿。 我不想让线程留下临界区睡眠一段时间,让其他线程有机会锁定互斥锁。 我认为必须有一些参数能够对等待互斥体的线程进行公平的锁定,但我无法找到任何合适的解决方案。 那么我发现std :: this

Read whole ASCII file into C++ std::string

This question already has an answer here: What is the best way to read an entire file into a std::string in C++? 11 answers Update: Turns out that this method, while following STL idioms well, is actually surprisingly inefficient! Don't do this with large files. (See: http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html) You can make a streambuf iterator out of the

将整个ASCII文件读入C ++ std :: string

这个问题在这里已经有了答案: 在C ++中将整个文件读入std :: string的最佳方式是什么? 11个答案 更新:原来,这个方法在遵循STL习语的同时,实际上效率却非常低下! 不要用大文件来做这件事。 (请参阅:http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html) 你可以在文件之外创建一个streambuf迭代器并用它初始化字符串: #include <string> #include <fstream> #include <str

C++: "std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this instead: std::cout << "Test linen"; Is there a technical reason to prefer one over the other, or is it just a matter of coding style? The varying line-ending characters don't matt

C ++:“std :: endl”vs“\ n”

许多C ++书籍包含这样的示例代码... std::cout << "Test line" << std::endl; ...所以我也一直这样做。 但是我看到了很多来自这样的开发人员的代码: std::cout << "Test linen"; 是否有技术上的理由相对于另一个更喜欢它,还是仅仅是编码风格的问题? 假设文件在文本模式下打开,这是不变的行结束字符并不重要,除非您要求二进制文件,否则这是您所得到的。 编译后的程序会为编译的系统写出正确的内

Is there an implementation of std::async which uses thread pool?

The standard function std::async: The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool) and returns a std::future that will eventually hold the result of that function call. There is two launch polices std::launch::async and std::launch::deferred. In my compiler's ( GCC 6.2 ) standard library impelmentation, t

是否有使用线程池的std :: async的实现?

标准函数std :: async: 模板函数async异步运行函数f(可能在一个单独的线程中,可能是线程池的一部分),并返回一个std :: future,最终将保存该函数调用的结果。 有两个启动策略std :: launch :: async和std :: launch :: deferred。 在我的编译器( GCC 6.2 )标准库建造中,第一个总是创建一个新线程,第二个对调用线程进行懒惰评估。 默认情况下使用std::launch::deferred 。 是否有一些实现使用大小等于可用硬件线

C++11 Segmentation fault with std::promise

C++ std::promise Segmentation Fault This code creates multiple threads and sends a promise& to them. It segfaults when they call promise::set_value to try and return data back to main. Can anyone explain why this code produces a segmentation fault? void workerFunc(promise<long>& prom) { long number = doInterestingThings(); //SEGFAULT!!! prom.set_value(number);

C ++ 11分段错误,使用std :: promise

C ++ std :: promise分割错误 此代码创建多个线程并向它们发送一个promise& 。 当它们调用promise::set_value尝试并将数据返回给main时,会发生段错误。 任何人都可以解释为什么这段代码会产生分段错 void workerFunc(promise<long>& prom) { long number = doInterestingThings(); //SEGFAULT!!! prom.set_value(number); return; } 线程函数。 它在prom.set_value处发生段prom.set_val

Storing a future in a list

I want to store the futures of several threads spawned using async in a list to retrieve their results later. future<int> f = async(doLater, parameter); list<future<int>> l; l.push_back(f); However the compiler prints the following error message /usr/include/c++/4.7/bits/stl_list.h:115:71: error: use of deleted function 'std::future<_Res>::future(const std::future&

将未来存储在列表中

我想在列表中存储使用异步生成的几个线程的未来,以便稍后检索它们的结果。 future<int> f = async(doLater, parameter); list<future<int>> l; l.push_back(f); 但是,编译器会输出以下错误消息 /usr/include/c++/4.7/bits/stl_list.h:115:71:错误:使用已删除的函数'std :: future <_Res> :: future(const std :: future <_Res>&)[with _Res = int; std :: future <_Res> =

efficient handling of synchronous case

You can have an async API like this std::future<int> GetAsync() There are cases when for example you already have the result cached and would like to return the future initialized with the result. Is there a way with current standard (or proposal) to achieve that without recurring to creating an additional std::promise or making an additional async (with launch policy deferred) ? EDIT

高效处理同步案例

你可以有一个像这样的异步API std::future<int> GetAsync() 有些情况下,例如,您已经将结果缓存起来,并希望返回使用结果初始化的将来。 有没有现行的标准(或提案)的方式来实现这一点,而无需重复创建额外的std :: promise或进行额外的异步(使用推迟策略)? 编辑 考虑结果是基于GetAsync()函数的本地状态计算的结果,以及在某些情况下,如果选择使用std :: async并且启动策略被推迟 我显然误解了一些事情

What is std::promise?

I'm fairly familiar with C++11's std::thread , std::async and std::future components (eg see this answer), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The standard document itself doesn't contain a whole lot of information beyond its class synopsis, and neither does just::thread. Could someo

什么是std :: promise?

我非常熟悉C ++ 11的std::thread , std::async和std::future组件(例如参见这个答案),这很简单。 然而,我不能完全理解std::promise是什么,它做了什么以及在哪种情况下最好使用它。 标准文档本身并不包含超过其类纲要的大量信息,也不仅仅是:: thread。 有人能否给出一个简短的简洁例子,说明需要std::promise情况,以及哪里是最常用的解决方案? 用[futures.state]的话来说, std::future是一个异步返回对象(“一个

What is std::move(), and when should it be used?

What is it? What does it do? When should it be used? Good links are appreciated. http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics In C++11, in addition to copy constructors, objects can have move constructors. (And in addition to copy assignment operators, they have move a

什么是std :: move(),什么时候应该使用它?

它是什么? 它有什么作用? 什么时候应该使用它? 良好的联系表示赞赏。 http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics 在C ++ 11中,除了复制构造函数之外,对象还可以具有移动构造函数。 (除了复制赋值操作符之外,他们还有移动赋值操作符。) 如果对象具有类型“rvalue-reference”(