我有一个C++库,它应该在多个线程上进行一些计算.我创建了独立的线程代码(即它们之间没有共享变量),除了一个数组.问题是,我不知道如何使其成为线程安全的.
我看了互斥锁/解锁(QMutex因为我正在使用Qt),但它不适合我的任务 - 而一个线程将锁定互斥锁,其他线程将等待!
然后我读到了std::atomic,看起来就像我需要的那样.不过,我尝试以下列方式使用它:
std::vector<std::atomic<uint64_t>> *myVector;
Run Code Online (Sandbox Code Playgroud)
并且它产生了编译器错误(使用已删除的函数'std :: atomic :: atomic(const std :: atomic&)').然后我找到了解决方案 - 使用特殊的包装器std::atomic.我试过这个:
struct AtomicUInt64
{
std::atomic<uint64_t> atomic;
AtomicUInt64() : atomic() {}
AtomicUInt64 ( std::atomic<uint64_t> a ) : atomic ( atomic.load() ) {}
AtomicUInt64 ( AtomicUInt64 &auint64 ) : atomic ( auint64.atomic.load() ) {}
AtomicUInt64 &operator= ( AtomicUInt64 &auint64 )
{
atomic.store ( auint64.atomic.load() );
}
};
std::vector<AtomicUInt64> *myVector;
Run Code Online (Sandbox Code Playgroud)
这个东西成功编译,但是当我无法填充向量时:
myVector = new std::vector<AtomicUInt64>();
for ( int …Run Code Online (Sandbox Code Playgroud) 一天前,我开始在简单的Windows窗体项目(C#)中使用Entity Framework CodeFirst.我创建了两个模型:
[Table("SavesSet")]
public partial class Saves
{
public Saves()
{
this.SkillsSet = new HashSet<Skills>();
}
[Key]
public int SaveID { get; set; }
public string Player { get; set; }
public int Age { get; set; }
public int Money { get; set; }
public virtual ICollection<Skills> SkillsSet { get; set; }
}
[Table("SkillsSet")]
public partial class Skills
{
[Key]
public int SkillID { get; set; }
public string Name { get; set; }
public int Value { …Run Code Online (Sandbox Code Playgroud) 我使用 Qt Creator 为它制作静态 C++ 库和 Qt 应用程序。
我的库包括 MyLib_global.h:
#if defined(MYLIB_LIBRARY)
# define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
Run Code Online (Sandbox Code Playgroud)
myclass.h 文件:
#include "MyLib_global.h"
class MYLIBSHARED_EXPORT MyClass : public QObject
{
Q_OBJECT
public:
enum Log
{
SomeValue,
NotARealValue
};
MyClass(double var, Log e);
~MyClass();
}
Run Code Online (Sandbox Code Playgroud)
和 myclass.cpp 文件:
#include "myclass.h"
MyClass::MyClass(double var, Log e)
{
}
MyClass::~MyClass()
{
}
Run Code Online (Sandbox Code Playgroud)
我在 .pro 文件中写的这个块:
QT -= gui
QMAKE_CXXFLAGS += -std=c++0x
TARGET = MyLib
TEMPLATE = lib
CONFIG += staticlib
Run Code Online (Sandbox Code Playgroud)
所以我在 …
我想使用 Outlook 的 SMTP 服务器在 Django 应用程序中发送电子邮件。问题是,每次尝试发送消息时,我都会收到SSL 错误的版本号错误。
错误回溯:
Traceback (most recent call last):
File "F:\Development\Python\lib\smtplib.py", line 366, in getreply
line = self.file.readline()
File "F:\Development\Python\lib\socket.py", line 297, in readinto
return self._sock.recv_into(b)
File "F:\Development\Python\lib\ssl.py", line 453, in recv_into
return self.read(nbytes, buffer)
File "F:\Development\Python\lib\ssl.py", line 327, in read
v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1450)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Development\Python\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
response …Run Code Online (Sandbox Code Playgroud) 我是C++初学者,我遇到了C++ 0x随机数生成器的问题.我想使用Mersenne twister引擎生成随机的int64_t数字,并且我使用我之前发现的一些信息编写了一个函数:
#include <stdint.h>
#include <random>
int64_t MyRandomClass::generateInt64_t(int64_t minValue, int64_t maxValue)
{
std::random_device rd;
std::default_random_engine e( rd() );
unsigned char arr[8];
for(unsigned int i = 0; i < sizeof(arr); i++)
{
arr[i] = (unsigned char)e();
}
int64_t number = static_cast<int64_t>(arr[0]) | static_cast<int64_t>(arr[1]) << 8 | static_cast<int64_t>(arr[2]) << 16 | static_cast<int64_t>(arr[3]) << 24 | static_cast<int64_t>(arr[4]) << 32 | static_cast<int64_t>(arr[5]) << 40 | static_cast<int64_t>(arr[6]) << 48 | static_cast<int64_t>(arr[7]) << 56;
return (std::abs(number % (maxValue - minValue)) + minValue);
}
Run Code Online (Sandbox Code Playgroud)
当我在Qt应用程序中尝试使用此代码时,我收到此错误:
terminate …Run Code Online (Sandbox Code Playgroud) 我有一个C++ Qt程序,它使用QThread和使用QMutex和QWaitCondition实现的暂停/恢复机制.这就是它的样子:
MyThread.h:
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
void pauseThread();
void resumeThread();
private:
void run();
QMutex syncMutex;
QWaitCondition pauseCond;
bool pause = false;
}
Run Code Online (Sandbox Code Playgroud)
MyThread.cpp:
void MyThread::pauseThread()
{
syncMutex.lock();
pause = true;
syncMutex.unlock();
}
void MyThread::resumeThread()
{
syncMutex.lock();
pause = false;
syncMutex.unlock();
pauseCond.wakeAll();
}
void MyThread::run()
{
for ( int x = 0; x < 1000; ++x )
{
syncMutex.lock();
if ( pause == true )
{
pauseCond.wait ( &syncMutex );
}
syncMutex.unlock();
//do some work …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Qt C++应用程序.我需要下载一些文件(可能很大)并向用户显示下载进度.要执行此任务,我使用以下代码:
QNetworkAccessManager* networkManager = new QNetworkAccessManager();
QNetworkRequest request(fileUrl); //fileUrl is a QUrl variable
QVariant responseLength = request.header(QNetworkRequest::ContentLengthHeader);
int fileSize = responseLength.toInt();
ui->progressBar->setMaximum(fileSize);
QNetworkReply reply = networkManager->get(request);
QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(downloadProgressChanged(qint64,qint64)));
Run Code Online (Sandbox Code Playgroud)
downloadProgressChanged带有此代码的插槽在哪里:
void downloadProgressChanged(qint64 downloaded, qint64 total)
{
ui->progressBar->setValue(ui->progressBar->value() + 1);
ui->labelProgress->setText(QString::number((downloaded / 1024)));
}
Run Code Online (Sandbox Code Playgroud)
(我使用QProgressBar命名progressBar显示进度,QLabel命名labelProgress显示下载的千字节).
我的问题是我无法访问Content-Length标头(int fileSize值为0),因此我无法显示操作的进度.我检查了我的网络服务器上的HTTP标头 - Content-Length工作正常.
在这个SO问题中,我读到我可以使用QNetworkReply::metaDataChanged()信号,但我怎样才能用它来显示进度?文档说下载已经开始时可以发出信号,但我需要在开始下载之前获取标题内容- 设置我的progressBar.