任何人都可以用简单的英语解释什么是std :: memory_order,以及如何将它们与std :: atomic <>一起使用?
我在这里找到了参考文献和一些例子,但根本不了解. http://en.cppreference.com/w/cpp/atomic/memory_order
谢谢.
$ time ./Test
real 0m2.906s
user 0m2.887s
sys 0m0.017s
Run Code Online (Sandbox Code Playgroud)
这是程序代码:
#include <iostream>
#include <map>
void func_a() {
std::map<int, int> m;
for (unsigned int i = 0; i < 10000; i++) {
m.insert(std::pair<int, int>(i, i));
}
}
void func_b() {
std::map<int, int> m;
for (unsigned int i = 0; i < 1000000; i++) {
m.insert(std::pair<int, int>(i, i));
}
}
int main() {
func_a();
func_b();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个int和一个bool变量,由多个线程访问(多次写/读).目前,我使用两个互斥锁,一个用于int,一个用于bool来保护这些变量.
我听说过使用原子变量和运算符来编写无锁多线程程序.我的问题是
这些代码是否是线程安全的?
double double_m; // double_m is only accessed by current thread.
std::atomic<bool> atomic_bool_x;
atomic_bool_x = true && (double_m > 12.5);
int int_n; // int_n is only accessed by current thread.
std::atomic<int> atomic_int_x;
std::atomic<int> atomic_int_y;
atomic_int_y = atomic_int_x * int_n;
Run Code Online (Sandbox Code Playgroud) 我对锁和互斥锁之间的区别感到非常困惑.在Boost文档中,它说,
锁定类型
互斥体类型
在另一篇文章中,我看到这样的函数,
boost::shared_mutex _access;
void reader()
{
boost::shared_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
}
void conditional_writer()
{
boost::upgrade_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
if (something) {
boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
// do work here, but now you have exclusive access
}
// do more work here, without anyone having …Run Code Online (Sandbox Code Playgroud) 似乎每次我向向量m_test添加一个对象时,都会调用析构函数方法.我错过了什么吗?我怎样才能防止这种情况发生?
class TEST
{
public:
TEST();
~TEST();
int * x;
};
TEST::TEST()
{
}
TEST::~TEST()
{
... it is called every time I push_back something to the vector ...
delete x;
}
vector<TEST> m_test;
for (unsigned int i=0; i<5; i++)
{
m_test.push_back(TEST());
}
Run Code Online (Sandbox Code Playgroud) 我在Linux下使用Eclipse CDT,任何人都可以在这种环境下推荐一个好的分析器.
我也是C++和多线程编程的新手,任何人都可以提供一些关于如何为多线程应用程序运行分析的建议,例如,寻找性能瓶颈等等.
谢谢.
我理解,boost::mutex::scoped_lock当a 超出范围时,由a锁定的变量会自动解锁.
怎么样boost::unique_lock,它是否在超出范围时自动解锁变量?
任何人都可以指出该功能的参考.
double x;
boost::mutex x_mutex;
void foo()
{
{
boost::unique_lock<boost::mutex> lock(x_mutex);
x = rand();
}
...... some calculation which takes 10 second ......
...... is x still locked here??? ......
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有一个奇怪的错误.
class INST
{
public:
boost::mutex m_mutex;
};
std::vector<INST> m_inst;
Run Code Online (Sandbox Code Playgroud)
错误C2248:'boost :: mutex :: mutex':无法访问类'boost :: mutex'中声明的私有成员'请参阅'boost :: mutex :: mutex'的声明
但是,我的其他课很好,
class VIEW
{
public:
boost::mutex m_mutex;
};
VIEW m_view;
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?我试图将m_mutex声明为private,但仍然存在同样的问题.
谢谢.
我正在尝试使用std :: atomic库.
std::atomic<int> xvs 的缺点是
int x什么?换句话说,原子变量的开销是多少?我在Redhat Enterprise中运行Eclipse CDT,gcc编译器的默认版本是4.4.6.最近支持团队添加了gcc 4.6.3,我想知道如何以这样的方式设置Eclipse,我可以选择使用gcc 4.4.6或4.6.3编译应用程序.
谢谢.