我在多线程应用程序中使用std :: vector作为共享数据.我将线程封装在一个类中,例如,
class ABC {
public:
double a, b, c;
};
boost::mutex mutex1;
class XYZ {
public:
XYZ(vector<ABC> & pVector) {
ptrVector = &pVector;
m_thread = boost::thread(&XYZ::Start, this);
}
~XYZ() {}
void Start();
public:
vector<ABC> * ptrVector;
boost::thread m_thread;
};
void XYZ::Start() {
try {
while(1) {
boost::this_thread::interruption_point();
for (unsigned int i=0; i<ptrVector->size(); i++) {
{
boost::mutex::scoped_lock lock(mutex1);
ptrVector->at(i).a = double(rand())/10000;
ptrVector->at(i).b = double(rand())/10000;
ptrVector->at(i).c = double(rand())/10000;
}
}
}
}
catch(boost::thread_interrupted) {}
catch(std::exception) {}
}
Run Code Online (Sandbox Code Playgroud)
当我关闭应用程序时,有时候,在调试中会有2条错误消息, …
如何在对象中启动线程?例如,
class ABC
{
public:
void Start();
double x;
boost::thread m_thread;
};
ABC abc;
... do something here ...
... how can I start the thread with Start() function?, ...
... e.g., abc.m_thread = boost::thread(&abc.Start()); ...
Run Code Online (Sandbox Code Playgroud)
以后我可以做点什么,
abc.thread.interrupt();
abc.thread.join();
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在使用 Eclipse CDT 编写应用程序。该应用程序使用第三方 API,它需要以 sudo 身份运行,以便打开原始套接字等。
在命令行中,我可以运行类似的程序
su
./program
Run Code Online (Sandbox Code Playgroud)
但是在Eclipse CDT环境下,按Ctrl+F11(Run->Run Last Launched)就不行了,估计是我的Linux GUI登录不是su。
无论如何,我可以在 Eclipse CDT 中以 su(使用 su 密码)身份运行吗?
谢谢。
我是多线程编程的新手,并且对Mutex如何工作感到困惑.在Boost :: Thread手册中,它指出:
互斥锁保证只有一个线程可以锁定给定的互斥锁.如果代码段被互斥锁定和解锁包围,则可以保证一次只有一个线程执行该段代码.当该线程解锁互斥锁时,其他线程可以进入该代码区域:
我的理解是,Mutex用于保护一段代码不被多个线程同时执行,不保护变量的内存地址.我很难掌握这个概念,如果我有2个不同的函数试图写入相同的内存地址会发生什么.
在Boost库中有这样的东西:
谢谢.
我将我的应用程序移动到另一个Linux框,编译后,它返回一个错误说
#include <atomic>
Run Code Online (Sandbox Code Playgroud)
无法解决.
我想新的GNU C++ 11头文件/库没有安装在新机器上.
我的问题是如何安装它们?
我在Redhat Enterprise上运行,所以yum安装?
谢谢.
我是C++编程的新手,我想知道什么是"从这里实例化"错误?
struct Data {
Data(int a, int b) {
x = a;
y = b;
}
int x;
int y;
};
std::map<int, Data> m;
m[1] = Data(1, 2);
Run Code Online (Sandbox Code Playgroud)
我收到了几条错误消息
谢谢.
我使用g ++ 4.4.6在Redhat Linux 6上编写了一个小应用程序.编译后,我收到一个错误
/usr/bin/ld: cannot find -lcrypto
Run Code Online (Sandbox Code Playgroud)
我搜索了加密库并在这里找到它们,
[root@STL-DUNKEL01 bin]# find / -name libcrypto*
/usr/lib64/libcrypto.so.0.9.8e
/usr/lib64/libcrypto.so.10
/usr/lib64/libcrypto.so.6
/usr/lib64/libcrypto.so.1.0.0
Run Code Online (Sandbox Code Playgroud)
我的问题是编译错误是由/ usr/bin/ld在搜索路径中没有/ usr/lib64 /引起的?如果是,我该如何添加它?
谢谢.
我是C++的新手,这个问题对许多人来说似乎很明显.
如果我写的东西像
std::map<int, double> m;
Run Code Online (Sandbox Code Playgroud)
例如,
class own_int_less : public binary_function<int, int, bool>
{
public:
bool operator()( const double &left, const double &right ) const
{
return (abs(left - right) > epsilon) && (left < right);
};
double epsilon;
};
Run Code Online (Sandbox Code Playgroud)
谢谢.
我遇到了以下代码的错误
struct MapKey {
std::string x;
std::string y;
}
std::map<MapKey, int> m;
if (m.find(key) != m.end()) {
......
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说,
no match for "operator<" in '__x < __y'
Run Code Online (Sandbox Code Playgroud)
我相信问题是MapKey需要有一个比较方法,我想知道如何为Mapkey实现一个.例如,
struct MapKey {
bool operator<(const MapKey &key) const {
... what shall I put here? ...
}
std::string x;
std::string y;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.