我是std :: thread的新手,我尝试编写代码parallel_for.我编写了以下内容:
// parallel_for.cpp
// compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread
// execution: time ./parallel_for 100 50000000
// (100: number of threads, 50000000: vector size)
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <thread>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
// Parallel for
template<typename Iterator, class Function>
void parallel_for(const Iterator& first, const Iterator& last, Function&& f, const int nthreads = 1, const int threshold = 1000)
{
const unsigned int group = …Run Code Online (Sandbox Code Playgroud) 我试图了解如何使用Visual Studio 2012使用新的std :: thread.我正在尝试编译以下代码.
#include <iostream>
#include <thread>
class scoped_thread
{
std::thread t_;
public:
explicit scoped_thread(std::thread & t): t_(std::move(t))
{
if(!t_.joinable())throw std::logic_error("No thread");
}
~scoped_thread()
{
t_.join();
}
private:
scoped_thread(scoped_thread const &);
scoped_thread & operator=(scoped_thread const &);
};
struct local_functor
{
int& i_;
local_functor(int & i):i_(i){}
void operator()()
{
while(i_ < 1e5)i_++;
}
};
// can potentially throw exceptions
void callAnotherFunc()
{
std::cout << "this function can throw an exception" << std::endl;
// try (un)commenting the line below and …Run Code Online (Sandbox Code Playgroud) 我注意到很多已经为C++ 11更新过的经典C++参考资料源,例如cplusplus.com和Josuttis标准库参考书,似乎根本没有涵盖/有任何文档.在C++ 11并发标准库的功能,如std::thread,std::atomic,和std::async.
这些并发功能是否比标准库的其他部分"更不标准"?或者文档是否缺乏其他原因?
我正在尝试创建一个std :: thread的形式,它将一个包装器放在线程中执行的代码中.不幸的是,由于我对rvalues和Function我试图通过的模板类型的理解不充分,我无法将其编译.这是我的代码:
#include <vector>
#include <thread>
#include <utility>
void Simple2(int a, int b) {}
template <typename Function, typename... Args>
void Wrapper(Function&& f, Args&&... a) {
f(std::forward<Args>(a)...);
}
class Pool {
public:
template <typename Function, typename... Args>
void Binder(Function&& f, Args&&... a) {
std::thread t(Wrapper<Function, Args...>,
std::forward<Function>(f), std::forward<Args>(a)...);
}
};
int main() {
Wrapper(Simple2, 3, 4); // Works
Pool pool;
pool.Binder(Simple2, 3, 4); // Doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
这里看起来很重要的Clang3.0输出是:
/usr/include/c++/4.6/functional:1286:9: error: non-const lvalue reference to type 'void (int, int)' …Run Code Online (Sandbox Code Playgroud) 我想创建一个用于实验目的的线程池(以及有趣的因素).它应该能够处理各种各样的任务(所以我可以在以后的项目中使用它).
在我的线程池类中,我将需要某种任务队列.由于标准库std::packaged_task从C++ 11标准开始提供,我的队列看起来像std::deque<std::packaged_task<?()> > task_queue,所以客户端可以std::packaged_task通过某种公共接口函数将s 推入队列(然后池中的一个线程将通知一个条件变量来执行它,等等.
我的问题与std::packaged_task<?()>deque中s 的模板参数有关.
函数签名?()应该能够处理任何类型/数量的参数,因为客户端可以执行以下操作:
std::packaged_task<int()> t(std::bind(factorial, 342));
thread_pool.add_task(t);
所以我不必处理参数的类型/数量.
但是回报价值应该是多少?(因此问号)
如果我将整个线程池类作为模板类,它的一个实例将只能处理具有特定签名的任务(如std::packaged_task<int()>).
我希望一个线程池对象能够处理任何类型的任务.
如果我使用std::packaged_task<void()>并且调用的函数返回一个整数,或者任何东西,那么这就是未定义的行为.
我在一个Server对象中有多个正在执行相同任务的线程。这些线程是通过Server :: *例程初始化的。
在此例程中,存在一些处理的无限循环。
我想知道对多个线程使用相同的方法是否安全?难怪对于该类的字段,如果我想读或写它,我将使用互斥体。但是例程本身呢?
由于函数是地址,因此那些线程将在同一内存区中运行?
我是否需要为每个线程创建具有相同代码的方法?
ps:我用std :: mutex(&Server :: Task,this)
我创建一个newstd :: thread对象,然后detach()它.线程运行任意一段时间,然后自行终止.由于我创建了对象new,我是否需要delete在某个时候释放它的资源?或者线程delete在终止时是否有效?
如果它本身有效delete,如果我delete在它终止后明确它会发生什么坏事吗?
我目前正在开发一个程序,需要从套接字服务器下载一些图像,下载工作将执行很长时间.所以,我创造了一个新std::thread的做法.
一旦下载,std::thread它将调用当前Class的成员函数,但此类可能已被释放.所以,我有一个例外.
如何解决这个问题呢?
void xxx::fun1()
{
...
}
void xxx::downloadImg()
{
...a long time
if(downloadComplete)
{
this->fun1();
}
}
void xxx::mainProcees()
{
std::thread* th = new thread(mem_fn(&xxx::downloadImg),this);
th->detach();
//if I use th->join(),the UI will be obstructed
}
Run Code Online (Sandbox Code Playgroud) 尝试使用标准模板库中的多线程编译程序时遇到了一些麻烦.当我尝试编译以下程序时,它返回一个模糊的错误:
#include <iostream>
#include <thread>
void foo()
{
std::cout << "Thread 1\n";
}
int main(int argc, char** argv)
{
std::thread tr(foo);
std::cout << "Main thread\n";
tr.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个错误:
/tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » :
file.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21) : undefined reference to « pthread_create »
collect2: error : ld has return 1 execution status code
Run Code Online (Sandbox Code Playgroud)
我编译它:
g ++ -std = c ++ 14 file.cpp -o test -Wall
有人可以帮我吗?
让我们说一些线程有std::thread::id myId一些其他线程.现在我想要检索std::thread与之关联的对象,myId以便我可以.join().有可能std吗?或者我必须手动跟踪它?
c++ ×10
stdthread ×10
c++11 ×8
std ×2
cocos2d-x ×1
g++ ×1
standards ×1
std-function ×1
stdasync ×1
visual-c++ ×1