为什么在没有任何专门化的情况下将通用 lambda 传递给 std::thread() 是合法的,而另一方面,这对于函数模板来说是非法的。
下面演示了我的查询。
#include <thread>
template <class T>
void f(T t)
{
}
int main()
{
std::thread t([](auto i){}, 1); // Works
std::thread t(f, 1); // Doesn't work
std::thread t(f<int>, 1); // Works
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud) Linux对我来说是一个新平台.我已经用c ++在Windows上编码了很多年,并且已经适应了该平台上的多线程.
当我需要在linux平台上学习c ++时,C++ 11就出现了.
Linux似乎在大多数情况下使用pthreads - 好吧还有boost :: threads和QT也有自己的线程.但是随着C++ 11出现了std :: thread,一种全新的(跨平台和C++标准)做线程的方式.
所以我想我将不得不学习pthreads和std :: threads.最终,std :: thread似乎更重要,但那里有很多遗留代码,所以我必须知道两者.
对于Windows上的线程同步,我将使用WaitForMultipleObjects等待许多任务完成,然后继续进一步的工作.
pthreads是否存在类似的同步机制?的std ::线程?
我已经看过pthread_join了,它似乎只能在一个线程上等待一次.我可能错过了另一个pthread电话吗?
使用此代码,我得到了错误:
错误1错误C2064:term不评估为带有1个参数的函数c:\ program files(x86)\ microsoft visual studio 11.0\vc\include\functional 1152 1 Pipeline
class PipelineJob {
private:
std::thread *thread;
void execute(PipelineJob* object);
public:
void execute(PipelineJob* object)
{
}
PipelineJob()
{
this->thread = new std::thread(&PipelineJob::execute, this);
}
};
Run Code Online (Sandbox Code Playgroud)
我尝试了很多变化,现在任何一个如何解决这个问题?
我正在研究多线程中值函数,作为更大项目的一部分.我有很少的C++经验.下面的中值函数应该采用3维int向量的向量,并返回int的3维向量,其中每个条目是输入向量中该索引中所有条目的中值.因此,如果输入为<< 3,2,1>,<1,2,3>,<2,2,2 >>,则返回<2,2,2>.此代码将用于实现中值模糊以用于实时视频,因此需要多线程化.
#include <thread>
#include <iostream>
#include <mutex>
#include <vector>
#include <algorithm>
#include "median.h"
// mutex to protect bgrPixel (possibly not needed)
std::mutex mtx;
std::vector<int> median(const std::vector<std::vector<int> >& input)
{
std::vector<int> bgrPixel; // Vector to store median BGR value
std::thread first(thread_function, bgrPixel, input, 0); // thread for each colour channel
std::thread second(thread_function, bgrPixel, input, 1);
std::thread third(thread_function, bgrPixel, input, 2);
first.join();
second.join();
third.join();
return bgrPixel;
}
void thread_function(std::vector<int>& bgrPixel, const std::vector<std::vector<int> >& input1, int channel)
{
std::vector<int> input = …Run Code Online (Sandbox Code Playgroud) 我想包装一个对std :: thread构造函数的调用(以跟踪所有运行的线程,以便我可以加入它们或做其他事情).在此示例中,t1线程正确构造,但t2线程不使用gcc 4.8.1.但是,在Windows(VS2012)上,它编译时没有错误,并且运行没有错误.基于此处的讨论,这似乎是gcc中的一个错误,但可以说它实际上是VS中的一个错误.这样做的正确方法是什么?
#include <iostream>
#include <thread>
class A {
public:
void foo(int n ) { std::cout << n << std::endl; }
};
template<class F, class Arg>
std::thread& wrapper(F&& f, Arg&& a)
{
std::thread* t = new std::thread(f,a,100);
return *t;
}
int main()
{
A a;
std::thread t1(&A::foo, &a, 100);
t1.join();
std::thread t2 = wrapper(&A::foo, &a);
t2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器错误
-bash-4.1$ make
g++ -std=c++11 main.cpp -o main
main.cpp: In function ‘int …Run Code Online (Sandbox Code Playgroud) 我有一个在两个并发线程之间共享的资源.资源包含两个线程都需要读取和写入的向量.因此,我通过互斥锁访问向量.到目前为止,资源共享工作顺利,没有任何问题.
但是,当我尝试为sharedResource编写复制构造函数时,问题就会出现,如下所示.
class sharedResource{
public:
sharedResource(){}
sharedResource(const sharedResource &other) {
vec = other.GetVec();
}
std::vector<int> GetVec() const {
std::lock_guard<std::mutex> lock(vecMutex); // Gives error
return vec;
}
private:
std::vector<int> vec;
std::mutex vecMutex;
};
int main()
{
sharedResource bacon1;
sharedResource bacon2 = bacon1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于此代码,我收到错误
error C2664: 'std::lock_guard<std::mutex>::lock_guard(const std::lock_guard<std::mutex> &)' : cannot convert argument 1 from 'const std::mutex' to 'std::mutex &'
Run Code Online (Sandbox Code Playgroud)
你能解释一下我为什么会收到错误,如果有办法使用互斥锁而不会出现编译错误.
如果所有其他方法都失败了,我将创建一个线程不安全的GetVec2成员函数,它将返回vec而不通过锁定保护.但我想避免这种可能性.
std::vector<int> GetVec2() const {
return vec;
}
Run Code Online (Sandbox Code Playgroud) 我正在努力获得一个C++应用程序,以便在Windows和Linux中进行编译,在我发现的一些调试过程中
std::this_thread::get_id().hash()
Run Code Online (Sandbox Code Playgroud)
不能在Linux上用gcc 4.8编译(感谢这个帖子中的注释).建议的解决方法是使用:
std::hash<std::thread::id>()(std::this_thread::get_id())
Run Code Online (Sandbox Code Playgroud)
有谁知道这些是否产生相同的输出?
我正在尝试使用std::thread.我的线程应该调用一个方法并传递struct一个参数,正如许多例子所示.除了我非常简单的代码将无法编译.为了记录,我知道这个问题,但似乎没有任何帮助我.
在哪里我称之为线程:
void Exporter::save() const {
thread(write_to_disk, this->parameter).detach();
}
Run Code Online (Sandbox Code Playgroud)
签名write_to_disk:
void write_to_disk(const Parameter& parameter)
Run Code Online (Sandbox Code Playgroud)
write_to_disk在.cpp文件中的无名空间中定义.
我收到以下错误:
src/Exporter.cpp:65:5: error: no matching constructor for initialization of 'std::__1::thread'
thread(write_to_disk, this->parameter).detach();
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:374:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:263:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
thread(const thread&);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:270:5: note: candidate constructor not …Run Code Online (Sandbox Code Playgroud) 我无法理解编译器错误我正在尝试使用声明为c ++ 11 std::thread对象中的类的朋友的函数.我创建了一个小例子来展示我遇到的问题.基本上,当编译器只看到friend函数原型时aa,它会在函数传递给线程对象时抛出一个未定义的符号错误.我正在使用g ++ 4.9.2.
//test.cpp
#include <thread>
class A {
public:
friend void aa(A &p);
void av(void);
};
void A::av() {
std::thread t1(aa,std::ref(*this));
}
void aa(A &p) {
p;
}
int main() {
A ai;
ai.av();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译命令:
g++ -o test test.cpp -g -O0 -std=c++11 -lpthread
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误:
test.cpp: In member function ‘void A::av()’:
test.cpp:12:18: error: ‘aa’ was not declared in this scope
std::thread t1(aa,std::ref(*this));
Run Code Online (Sandbox Code Playgroud)
在上面,如果我更换线
std::thread t1(aa,std::ref(*this));
Run Code Online (Sandbox Code Playgroud)
同
aa(*this);
Run Code Online (Sandbox Code Playgroud)
编译得很好.或者,如果我在定义上面添加另一个函数的原型声明A::av …
我想知道,当你移动会发生什么unique_lock是持有recursive_mutex。
具体来说,我在看这段代码:
recursive_mutex g_mutex;
#define TRACE(msg) trace(__FUNCTION__, msg)
void trace(const char* function, const char* message)
{
cout << std::this_thread::get_id() << "\t" << function << "\t" << message << endl;
}
future<void> foo()
{
unique_lock<recursive_mutex> lock(g_mutex);
TRACE("Owns lock");
auto f = std::async(launch::async, [lock = move(lock)]{
TRACE("Entry");
TRACE(lock.owns_lock()? "Owns lock!" : "Doesn't own lock!"); // Prints Owns lock!
this_thread::sleep_for(chrono::seconds(3));
});
TRACE(lock.owns_lock()? "Owns lock!" : "Doesn't own lock!"); // Prints Doesn't own lock!
return f;
}
int main()
{ …Run Code Online (Sandbox Code Playgroud)