直到现在我std::queue
在我的项目中使用.我测量了此队列上特定操作所需的平均时间.
在两台机器上测量时间:我的本地Ubuntu VM和远程服务器.使用时std::queue
,两台机器的平均值几乎相同:约750微秒.
然后我"升级" std::queue
到boost::lockfree::spsc_queue
,所以我可以摆脱保护队列的互斥锁.在我的本地虚拟机上,我可以看到巨大的性能提升,平均现在是200微秒.然而,在远程机器上,平均值高达800微秒,这比以前慢了.
首先我认为这可能是因为远程机器可能不支持无锁实现:
并非所有硬件都支持同一组原子指令.如果硬件不可用,则可以使用防护装置在软件中进行仿真.然而,这具有失去无锁属性的明显缺点.
要确定是否支持这些指令,请boost::lockfree::queue
调用一个方法bool is_lock_free(void) const;
.但是,boost::lockfree::spsc_queue
没有这样的功能,对我来说,这意味着它不依赖于硬件而且总是无锁 - 在任何机器上.
性能损失的原因是什么?
// c++11 compiler and boost library required
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <async>
#include <thread>
/* Using blocking queue:
* #include <mutex>
* #include <queue>
*/
#include <boost/lockfree/spsc_queue.hpp>
boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024>> queue;
/* Using blocking queue:
* std::queue<int> queue;
* std::mutex mutex;
*/
int main()
{
auto producer = std::async(std::launch::async, …
Run Code Online (Sandbox Code Playgroud) 我想在Linux上使用C++访问WebSocket API.我见过不同的图书馆(比如libwebsockets或websocketpp),但我不确定应该使用哪个.我唯一需要做的就是连接到API并将数据接收到字符串.所以我正在寻找一个非常基本和简单的解决方案,没有太复杂.也许有人已经获得了WebSocket库的经验?
我正在用C#(Visual Studio 2015)开发一个程序,我想在某种情况下向用户显示一个toast消息.我从MSDN下载了这段代码,运行正常:
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += …
Run Code Online (Sandbox Code Playgroud) 我在Bjarne Stroustrup的"The C++ Programming Language,4th Edition"(第119页)中偶然发现了以下代码:
queue<Message> mqueue;
condition_variable mcond;
mutex mmutex;
void consumer()
{
while(true) {
unique_lock<mutex> lck{mmutex};
mcond.wait(lck);
auto m = mqueue.front();
mqueue.pop();
lck.unlock();
// process m
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个生产者线程,它推Message
送到队列并在循环中通知等待的线程.
我的问题是:是否需要unique_lock
在循环的每次迭代中创建一个新的?这对我来说似乎是不必要的,因为在下一行中,mcond.wait(lck)
锁在锁定之前直接解锁.
从性能的角度来看,lck
变量只能在循环开始之前初始化?
我正在为 Linux 上的 TCP 连接编写 C++ SSL 服务器。当程序用于SSL_write()
写入关闭的管道时,会抛出 SIGPIPE-Exception,从而导致程序关闭。我知道这是正常行为。但是当对等方没有正确关闭连接时,程序不应该总是死掉。
我已经在谷歌上搜索了很多,并尝试了几乎所有我发现的东西,但似乎没有什么对我有用。signal(SIGPIPE,SIG_IGN)
不起作用 - 仍然会抛出异常(对于signal(SIGPIPE, SomeKindOfHandler)
.
gdb 输出:
Program received signal SIGPIPE, Broken pipe.
0x00007ffff6b23ccd in write () from /lib/x86_64-linux-gnu/libpthread.so.0
(gdb) where
#0 0x00007ffff6b23ccd in write () from /lib/x86_64-linux-gnu/libpthread.so.0
#1 0x00007ffff7883835 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#2 0x00007ffff7881687 in BIO_write () from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#3 0x00007ffff7b9d3e0 in ?? () from /lib/x86_64-linux-gnu/libssl.so.1.0.0
#4 0x00007ffff7b9db04 in ?? () from /lib/x86_64-linux-gnu/libssl.so.1.0.0
#5 0x000000000042266a in NetInterface::SendToSubscribers(bool) () at ../Bether/NetInterface.h:181
#6 0x0000000000425834 in …
Run Code Online (Sandbox Code Playgroud) 当我点击全屏按钮时,我的WPF应用程序(使用Elysium Extra)在窗口右侧有一个边距:
在右侧,您可以看到我的桌面背景.
我检查是否有保证金,但它设置为0 px
所有方面.我也定了
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
Run Code Online (Sandbox Code Playgroud)
App.xaml中:
<extra:ElysiumApplication x:Class="CTS.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:extra="http://schemas.extra.com/ui"
xmlns:local="clr-namespace:CTS"
Theme="Dark"
StartupUri="MainWindow.xaml" />
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml:
<extra:Window x:Class="CTS.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:extra="http://schemas.extra.com/ui"
xmlns:local="clr-namespace:CTS"
mc:Ignorable="d"
Title="..." Height="521.877" Width="1239.945" FontFamily="Open Sans" Foreground="#FF0970D1" Background="#FF22313F">
....
Run Code Online (Sandbox Code Playgroud)
编辑:我已经检查了Elysium Extra演示应用程序.它也有同样的问题,所以它似乎是由框架引起的.但是,我想继续使用它.
我怎样才能摆脱这个边际?
我用C++编写了一个用于Ubuntu Server(64位)的程序,该程序应该全天候运行.服务器有2GB内存,但显然我的程序分配了太多的内存.
这是top
约2小时后的输出
top - 13:35:57 up 1:39, 1 user, load average: 0.15, 0.13, 0.08
Tasks: 68 total, 2 running, 66 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.9 us, 5.7 sy, 0.0 ni, 92.3 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 2050048 total, 540852 used, 1509196 free, 34872 buffers
KiB Swap: 1509372 total, 0 used, 1509372 free. 93060 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
902 root …
Run Code Online (Sandbox Code Playgroud) 我想连接到Poloniex的Push API.在他们的页面上,他们写下以下内容
要使用推送API,请连接到wss://api.poloniex.com并订阅所需的订阅源.
wss = WebSocket安全 - > SSL保护
他们还举例说明了Node.js和Autobahn | JS:
var autobahn = require('autobahn');
var wsuri = "wss://api.poloniex.com";
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = function (session) {
function marketEvent (args,kwargs) {
console.log(args);
}
function tickerEvent (args,kwargs) {
console.log(args);
}
function trollboxEvent (args,kwargs) {
console.log(args);
}
session.subscribe('BTC_XMR', marketEvent);
session.subscribe('ticker', tickerEvent);
session.subscribe('trollbox', trollboxEvent);
}
connection.onclose = function () {
console.log("Websocket connection closed");
}
connection.open();
Run Code Online (Sandbox Code Playgroud)
但是,我不想使用JavaScript,而是使用C++.还有一个用于C++的Autobahn-Library,叫做Autobahn | CPP.我已经安装了它并尝试运行他们的订阅者示例代码 …
从技术上讲,在 C++ 中,我们可以使用大括号来声明新的作用域。例如在这个函数中,它交换两个数字
void swap_int(int& first, int& second)
{
int temp = first;
first = second;
second = temp;
}
Run Code Online (Sandbox Code Playgroud)
我们还可以temp
在它自己的块内声明:
void swap_int(int& first, int& second)
{
// Do stuf...
{
int temp = first;
first = second;
second = temp;
}
// Do other stuff...
}
Run Code Online (Sandbox Code Playgroud)
这样做显然有一个好处,就是temp
不再需要的时候就直接删除。
但是,在我编写的代码中我从未使用过它。另外,在第三方库的代码中我几乎从未见过它。
为什么不公开使用?它是否会带来任何性能提升,或者只是意味着额外的打字工作?
我在线性程序中有一个浮点变量x
,该变量应位于0
两个常量CONSTANT_A
和之间或之间CONSTANT_B
:
LP.addConstraint(x == 0 OR CONSTANT_A <= x <= CONSTANT_B)
当然,OR
线性规划中不存在显式的东西。有没有办法表达这个约束?