我需要处理来自多个线程的数组,因此我使用CRITICAL SECTION为它提供对数据的独占访问.
这是我的模板:
#include "stdafx.h"
#ifndef SHAREDVECTOR_H
#define SHAREDVECTOR_H
#include <vector>
#include <windows.h>
template<class T>
class SharedVector {
std::vector<T> vect;
CRITICAL_SECTION cs;
SharedVector(const SharedVector<T>& rhs) {}
public:
SharedVector();
explicit SharedVector(const CRITICAL_SECTION& CS);
void PushBack(const T& value);
void PopBack();
unsigned int size() const;
T& operator[](int index);
virtual ~SharedVector();
};
template<class T>
SharedVector<T>::SharedVector() {
InitializeCriticalSection(&cs);
}
template<class T>
SharedVector<T>::SharedVector(const CRITICAL_SECTION& r): cs(r) {
InitializeCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PushBack(const T& value) {
EnterCriticalSection(&cs);
vect.push_back(value);
LeaveCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PopBack() {
EnterCriticalSection(&cs); …Run Code Online (Sandbox Code Playgroud) 我将学习Qt,我只是想知道C++,OO设计的哪些部分以及我必须具备的其他内容?模板,RAII,模式,....?
当我连接到服务器的客户端应用程序 - 使用IP地址和端口时,boost.asio不使用基本概念,这有点奇怪.可能是我在Boost中有点诺贝 - 我接受了 - 但无论如何我不明白.
所以,我有这样的代码让客户端连接到localhost上的服务器:
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while(error && endpoint_iterator != end) {
socket.close();
socket.connect(*endpoint_iterator++, error);
}
Run Code Online (Sandbox Code Playgroud)
WinSock 2.0中的Windows使用两个参数 - IP和端口 - 来标识服务器.
因此,问题是 - Asio究竟如何找出服务器正在监听连接的端口?它扫描所有端口吗?而且,如果是这样,如果两个服务器同时在不同的端口上侦听会发生什么?
我有这个代码代表银行:
class Bank {
friend class InvestmentMethod;
std::vector<BaseBankAccount*> accounts;
public:
//...
Run Code Online (Sandbox Code Playgroud)
BaseBankAccount是银行中所有帐户的抽象类:
class BaseBankAccount {
public:
BaseBankAccount() {}
virtual int getInterest() const = 0;
virtual int getInvestedSum() const = 0;
virtual void increaseDepositSum(int additionalSum) = 0;
virtual void close(std::string& reason) = 0;
virtual ~BaseBankAccount() {}
};
Run Code Online (Sandbox Code Playgroud)
问题是,当我通过指向基类对象的指针操作指向派生类对象的指针时,我可以调用的方法集受BaseBankAccount公共接口的限制 - 无论REAL对象是什么类型.
例如,并非每个帐户都可以选择增加已投入的金额 - 所以,我没有在基类中包含此方法:
class BankAccount: public BaseBankAccount {
protected:
BaseDeposit* deposit;
double sumInvested;
public:
BankAccount(int sum, int term, int inter): sumInvested(sum), depositTerm(term), interest(inter) {}
int getInterest() const { return interest; } …Run Code Online (Sandbox Code Playgroud) 根据我所学到的,如果经常在类层次结构中使用向下转换,那就不好了.我同意这一点,但是如果有的话,这个规则有什么例外?
这就是我的图形编辑器设计显得很薄的地方:我有两个层次结构,其中几何图形层次结构与图形基元分离.像这样:
public class GeometricPrimitive {...}
public class RectangeGeometric: Geometric Primitive {...}
public class GraphicPrimitive {...}
public class Rectangle: GraphicPrimitive {
private RectangleGeometric figure;
...
}
Run Code Online (Sandbox Code Playgroud)
因此,每个具体的图形图类都封装了具体几何类的实例.
这种方法是正确的,还是我更喜欢更通用的方法? - 不幸的是,在这种情况下会使用向下转换:
public class GraphicPrimitive {
protected GeometryPrimitive figure;
....
}
public class Rectangle: GraohicPrimitive {
public Rectangle(Color c, TwoDPoint leftHighPoint, TwoDPoint rightLowPoint):
base(new RectangleGeometric(leftHighPoint.Point2D, rightLowPoint.Point2D), c) { }
#region Geometric Properties
public TwoDPoint LeftTopCorner {
get { return new TwoDPoint(base.color, (base.figure as RectangleGeometric).LeftTopCorner); }
}
public TwoDPoint RightBottomCorner {
get { …Run Code Online (Sandbox Code Playgroud) 情况非常简单 - 我写了一个扩展方法,并使其与返回类型异步Task<T>.但是当我尝试使用await调用它时,编译器会抛出一个错误,表明扩展方法根本没有被识别为异步.这是我的代码:
public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class
{
var node = client.Create(source);
var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/{0}/labels", node.Id);
var serializedLabel = string.Empty;
using (var tempStream = new MemoryStream())
{
new DataContractJsonSerializer(label.GetType()).WriteObject(tempStream, label);
serializedLabel = Encoding.Default.GetString(tempStream.ToArray());
}
var bytes = Encoding.Default.GetBytes(serializedLabel);
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json");
webClient.Headers.Add("Accept", "application/json; charset=UTF-8");
await webClient.UploadDataTaskAsync(url, "POST", bytes);
}
return node;
}
var newNode = await client.CreateWithLabel(new Task(), "Task"); …Run Code Online (Sandbox Code Playgroud) 所以我从Nuget(Google.Apis.Storage.v1,版本= 1.8.1.10)下载了用于云存储的.NET客户端,使用Developer Console创建了一个项目,生成了API密钥,启用了计费,创建了一个存储桶.
但是,当我尝试列出存储桶中的所有对象时,我收到此错误(需要登录).任何想法为什么会这样?在控制台中启用了云存储及其JSON API.
以下是我的示例应用程序的完整代码:https://gist.github.com/chester89/5d6a62633abab3969c32
我有一个巨大的应用程序,它使用 Entity Framework 4。它是服务器上的 WCF(托管在 Windows 服务中)和客户端上的 WPF。
它的某些部分很慢,我想知道是不是因为数据库访问很慢。
我想不断测量该代码的性能,例如记录每个查询的执行时间(最好包括 EF 自己的东西,如 materialization),以便稍后分析。我怎么做?
EFProf 之类的工具(因为即使他们可以测量数据库服务器执行特定查询所花费的时间,我该如何运行它们一周?)和 Glimpse(它不支持 Windows 服务和 WPF)在我的情况下不起作用.
我考虑过创建自己的 SqlConnection 子类,但我的版本只接受 ObjectContext ctor 中的 EntityConnection,并且 EntityConnection 被标记为密封。而这个选项只允许测量原始 sql 查询的执行时间,而不是 EF 的代码。
我现在看到的唯一选项是:1)更新到 EF 6 并编写 SqlConnection 子类(这不会衡量 EF 自己的代码)2)更新到更高版本并使用类似EF hooks 库的 东西
当然,它不在一个书(只是看了掌握实体框架) - 因为我们大多数人只需要使用图书馆,不知道它有多快(讽刺)。
我有一个应用程序可以一次将多行插入到 SQL Server 中。
我使用SqlBulkCopy类或自己编写的代码来生成一个巨大的insert into table_name(...) values (...)语句。
我的表有几个索引和一个聚集索引。
问题是:这些索引是如何更新的?对于我插入的每一行?每笔交易?
有点奇怪的问题 - 这种情况是否有一个通用术语,例如“批量插入索引行为”?我试图用谷歌搜索几个关键字组合,没有找到任何东西。我问的原因是因为我有时会使用 Postgres 并且也想知道它的行为。
我一直试图找到关于这个主题的文章,好几次了,没有任何运气。
如果你能指点我任何文档、文章或有相关章节的书,那就太好了
我正在尝试将TeamCity配置为与NuGet一起使用.到目前为止 - 没有运气.我配置了4个构建步骤:
但是,当我触发构建时,我甚至无法通过第一个构建步骤.我配置TC来下载丢失的NuGet包.当我运行构建时,TC说:
无法在{here_is_my_path}找到repositories.config".
我知道问题是我使用这个名为的VS功能Enable NuGet package restore- 因此,repositories.config我的解决方案文件夹中没有这样的文件,这就是为什么TC无法找到它并且构建失败的原因.那么,我该怎么配置TC呢?任何想法和帮助表示赞赏.
如果有帮助,我正在使用.NET 4.0,VS2010和TeamCity 7.
c++ ×4
.net ×2
sql-server ×2
async-await ×1
boost ×1
boost-asio ×1
bulkinsert ×1
c# ×1
c#-5.0 ×1
indexing ×1
inheritance ×1
nuget ×1
oop ×1
pointers ×1
qt ×1
sqlbulkcopy ×1
teamcity ×1
winapi ×1
wpf ×1