C++11 C++;使用静态互斥对象&;赛况 我用静态互斥体C++类作为类的私有成员来保护类中的另一个公共函数中的CUT。但当我从两个线程调用类的对象时,我得到了一个竞速条件。不知道为什么 class ThreadSafePrint { public: void myprint(int threadNumber) { std::lock_guard<std::mutex> gaurd(mymutex); cout <<"Thread " << threadNumber << endl; } private: static std::mutex mymutex; }; std::mutex ThreadSafePrint::mymutex; int main() { ThreadSafePrint obj; std::vector<std::thread> workers; int threadNumber; // create 2 threads and pass a number for(int i=0; i<2;++i) { // threadNumber = 0 for 1st thread if(i==0) { threadNumber = i; } // threadNumber = 1 for 2nd thread if(i==1) { threadNumber = i; } workers.push_back(std::thread([&obj,&threadNumber]() { obj.myprint(threadNumber); })); } // join all threads std::for_each(workers.begin(), workers.end(),[](std::thread & th) { th.join(); }); return 0; }
在两个工作线程中捕获对局部变量C++11 C++;使用静态互斥对象&;赛况 我用静态互斥体C++类作为类的私有成员来保护类中的另一个公共函数中的CUT。但当我从两个线程调用类的对象时,我得到了一个竞速条件。不知道为什么 class ThreadSafePrint { public: void myprint(int threadNumber) { std::lock_guard<std::mutex> gaurd(mymutex); cout <<"Thread " << threadNumber << endl; } private: static std::mutex mymutex; }; std::mutex ThreadSafePrint::mymutex; int main() { ThreadSafePrint obj; std::vector<std::thread> workers; int threadNumber; // create 2 threads and pass a number for(int i=0; i<2;++i) { // threadNumber = 0 for 1st thread if(i==0) { threadNumber = i; } // threadNumber = 1 for 2nd thread if(i==1) { threadNumber = i; } workers.push_back(std::thread([&obj,&threadNumber]() { obj.myprint(threadNumber); })); } // join all threads std::for_each(workers.begin(), workers.end(),[](std::thread & th) { th.join(); }); return 0; },c++11,C++11,在两个工作线程中捕获对局部变量threadNumber的引用,在两个线程中访问它,并在主线程中对其进行变异,而不进行任何同步。这确实是一种竞赛条件。取而代之的是按价值捕获 workers.push_back(std::thread([&obj, threadNumber]() 在两个工作线程中捕获对局部变量threadNumber的引用,在两个线程中访问它,并在主线程中对其进行变异,而不进行任何同步。这确实是一种竞赛条件。取而代之的是按价值捕获 workers.push_back
threadNumber
的引用,在两个线程中访问它,并在主线程中对其进行变异,而不进行任何同步。这确实是一种竞赛条件。取而代之的是按价值捕获
workers.push_back(std::thread([&obj, threadNumber]()
在两个工作线程中捕获对局部变量
threadNumber
的引用,在两个线程中访问它,并在主线程中对其进行变异,而不进行任何同步。这确实是一种竞赛条件。取而代之的是按价值捕获
workers.push_back(std::thread([&obj, threadNumber]()
您必须通过值而不是引用来捕获
threadNumber
交换:
workers.push_back(std::thread([&obj,&threadNumber]()
借
否则,通过第二次循环运行,第一个线程的变量threadNumber
也将改变
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <algorithm>
class ThreadSafePrint
{
public:
void myprint(int threadNumber)
{
std::lock_guard<std::mutex> gaurd(mymutex);
std::cout <<"Thread " << threadNumber << std::endl;
}
private:
static std::mutex mymutex;
};
std::mutex ThreadSafePrint::mymutex;
int main()
{
ThreadSafePrint obj;
std::vector<std::thread> workers;
int threadNumber;
// create 2 threads and pass a number
for(int i=0; i<2;++i)
{
// threadNumber = 0 for 1st thread
if(i==0)
{
threadNumber = i;
}
// threadNumber = 1 for 2nd thread
if(i==1)
{
threadNumber = i;
}
workers.push_back(std::thread([&obj,threadNumber]()
{
obj.myprint(threadNumber);
}));
}
// join all threads
std::for_each(workers.begin(), workers.end(),[](std::thread & th)
{
th.join();
});
return 0;
}
#包括
#包括
#包括
#包括
#包括
类线程安全打印
{
公众:
void myprint(int threadNumber)
{
std::锁定保护gaurd(mymutex);
std::cout您必须通过值而不是引用来捕获threadNumber
交换:
workers.push_back(std::thread([&obj,&threadNumber]()
借
否则,通过第二次循环运行,第一个线程的变量threadNumber
也将改变
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <algorithm>
class ThreadSafePrint
{
public:
void myprint(int threadNumber)
{
std::lock_guard<std::mutex> gaurd(mymutex);
std::cout <<"Thread " << threadNumber << std::endl;
}
private:
static std::mutex mymutex;
};
std::mutex ThreadSafePrint::mymutex;
int main()
{
ThreadSafePrint obj;
std::vector<std::thread> workers;
int threadNumber;
// create 2 threads and pass a number
for(int i=0; i<2;++i)
{
// threadNumber = 0 for 1st thread
if(i==0)
{
threadNumber = i;
}
// threadNumber = 1 for 2nd thread
if(i==1)
{
threadNumber = i;
}
workers.push_back(std::thread([&obj,threadNumber]()
{
obj.myprint(threadNumber);
}));
}
// join all threads
std::for_each(workers.begin(), workers.end(),[](std::thread & th)
{
th.join();
});
return 0;
}
#包括
#包括
#包括
#包括
#包括
类线程安全打印
{
公众:
void myprint(int threadNumber)
{
std::锁定保护gaurd(mymutex);
std::cout创建线程时,显式要求编译器向线程提供对主函数/线程正在使用的变量threadNumber
的同一实例的访问权限
[&threadNumber]
再次强调:这是一个明确的共享
事实上,您的代码表明您可能希望在尝试线程之前更好地掌握该语言,这段代码非常奇怪:
int threadNumber;
// create 2 threads and pass a number
for(int i=0; i<2;++i)
{
// threadNumber = 0 for 1st thread
if(i==0)
{
threadNumber = i;
}
// threadNumber = 1 for 2nd thread
if(i==1)
{
threadNumber = i;
}
创建线程时,显式要求编译器向线程提供对主函数/线程正在使用的变量threadNumber
的同一实例的访问权限
[&threadNumber]
再次强调:这是一个明确的共享
事实上,您的代码表明您可能希望在尝试线程之前更好地掌握该语言,这段代码非常奇怪:
int threadNumber;
// create 2 threads and pass a number
for(int i=0; i<2;++i)
{
// threadNumber = 0 for 1st thread
if(i==0)
{
threadNumber = i;
}
// threadNumber = 1 for 2nd thread
if(i==1)
{
threadNumber = i;
}