我已经看到这个问题了很多但从未见过真正的具体答案.所以我将在这里发布一个,希望能帮助人们理解为什么在使用随机数生成器时会出现"模数偏差",就像rand()在C++中一样.
我有许多不同的模式,但是每个模式都包含一组字段.我想知道是否有办法让不同的模式扩展父模式并继承其字段.例如,这就是我想要的:
message Parent {
required string common1 = 0;
optional string common2 = 1;
}
message Child1 { // can we extend the Parent?
// I want common1, common2 to be fields here
required int c1 = 2;
required string c2 = 3;
}
message Child2 { // can we extend Parent?
// I want common1, common2 to be fields here
repeated int c3 = 2;
repeated string c4 = 3;
}
Run Code Online (Sandbox Code Playgroud)
这样Child1和Child2也包含来自Parent的字段common1和common2(可能更多).
这有可能吗?如果可以的话怎么样?
我有以下代码,注释行上的死锁.基本上f1和f2作为程序中的单个线程运行.f1期望i为1并递减它,通知cv.f2期望i为0并递增它,通知cv.我假设如果f2将i增加到1,则调用死锁,调用cv.notify(),然后f1读取过时的i值(为0),因为互斥锁和i之间没有内存同步,然后等待并且永远不会被唤醒起来.然后f2也进入睡眠状态,现在两个线程都在等待一个永远不会被通知的cv.
如何编写此代码以便不会发生死锁?基本上我想要实现的是拥有一些由两个线程更新的原子状态.如果其中一个线程的状态不正确,我不想旋转; 相反,我想使用cv功能(或类似的东西)在值正确时唤醒线程.
我使用g ++ - 7用O3编译代码(尽管在O0和O3中都发生了死锁).
#include <atomic>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
std::atomic_size_t i{0};
std::mutex mut;
std::condition_variable cv;
void f1() {
while (1) {
{
std::unique_lock<std::mutex> lk(mut);
cv.wait(lk, []() { return i.load() > 0; }); // deadlocks
}
--i;
cv.notify_one();
std::cout << "i = " << i << std::endl; // Only to avoid optimization
}
}
void f2() {
while (1) {
{
std::unique_lock<std::mutex> lk(mut);
cv.wait(lk, []() { return i.load() < 1; }); // deadlocks …Run Code Online (Sandbox Code Playgroud) 我正在使用:https://github.com/mumrah/kafka-python作为Python中的kafka api.我想获取指定主题的分区数.我怎么做?
我有以下代码:
struct Baz {
x: usize,
y: usize,
}
struct Bar {
baz: Baz,
}
impl Bar {
fn get_baz_mut(&mut self) -> &mut Baz {
&mut self.baz
}
}
struct Foo {
bar: Bar,
}
impl Foo {
fn foo(&mut self) -> Option<&mut Baz> {
for i in 0..4 {
let baz = self.bar.get_baz_mut();
if baz.x == 0 {
return Some(baz);
}
}
None
}
}
Run Code Online (Sandbox Code Playgroud)
它无法编译:
error[E0499]: cannot borrow `self.bar` as mutable more than once at a time …Run Code Online (Sandbox Code Playgroud) 所以我正在编写一些代码,我注意到除了语法,类型和其他编译时错误之外,C++不会抛出任何其他异常.所以我决定用一个非常简单的程序来测试它:
#include<iostream>
int main() {
std::count<<5/0<<std::endl;
return 1
}
Run Code Online (Sandbox Code Playgroud)
当我用g ++编译它时,g ++给了我一个警告,说我除了0但是它仍然编译了代码.然后,当我运行它时,它打印了一些非常大的任意数字.当我想知道的是,C++如何处理异常?整数除以0应该是一个非常简单的例子,它应该抛出异常并且程序应该终止.
我是否必须将我的整个程序封装在一个巨大的try块中然后捕获某些异常?我在Python中知道抛出异常时,程序将立即终止并打印出错误.C++做什么?是否有运行时异常停止执行并终止程序?
c++ error-handling exception-handling runtime-error exception
我有一个想要运行验证的 SQLAlchemy 模型。验证的一部分是确保在(少数)列上存在 UniqueConstraint。我知道列是什么。有没有用 SQLAlchemy 做到这一点的好方法?我使用的底层数据库是 MySQL。
我需要定义一个二叉搜索树,其中每个节点都可以访问父节点:
enum Tree<'a> {
Leaf,
Node {
left: Box<Tree<'a>>,
right: Box<Tree<'a>>,
parent: &'a Tree<'a>,
data: u64,
}
}
impl <'a> Tree<'a> {
pub fn new(data: u64, parent: &'a Tree) -> Tree<'a> {
Tree::Node {
left: Box::new(Tree::Leaf),
right: Box::new(Tree::Leaf),
parent,
data
}
}
pub fn insert_at_left_leaf(&'a mut self, data: u64) {
match *self {
Tree::Leaf => panic!("Leaf has no children"),
Tree::Node {ref mut left, ..} => {
**left = Tree::new(data, self);
}
}
}
}
fn main() {
let parent = …Run Code Online (Sandbox Code Playgroud) 我试图在我的C ++代码中使用aligned_alloc,但编译失败:
cc1plus: warning: command line option '-std=c11' is valid for C/ObjC but not for C++
vec.cpp: In function 'int main()':
vec.cpp:13:30: error: 'aligned_alloc' was not declared in this scope
uint8_t *arr1 = (uint8_t *)aligned_alloc(16, 16);
Run Code Online (Sandbox Code Playgroud)
我正在编译:g++-7 -std=c++17 vec.cpp在Mac OS X 10.13.3G ++版本上:g++-7 (Homebrew GCC 7.2.0_1) 7.2.0
aligned_alloc应该是C ++ 17的一部分,所以我很困惑为什么它不起作用。还有其他方法可以在C ++ 17中获得对齐的内存吗?
这是一个无效的代码示例:
#include <cstdlib>
#include <cstdint>
int main() {
uint8_t *arr1 = (uint8_t *)std::aligned_alloc(16, 16);
std::free(arr1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想比较两个 16 字节的向量并获取每个匹配的索引。一个小例子来说明我想要的:
fn get_matching_idx(arr1: &[u8], arr2: &[u8]) {
let vec1 = u8x16::load_aligned(arr1);
let vec2 = u8x16::load_aligned(arr2);
let matches = vec1.eq(vec2);
for i in 0..16 {
if matches.extract_unchecked(i) {
// Do something with the index
}
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我只想对设置的索引“做某事”,而不是检查每一个索引(匹配的数量会很少)。
有没有办法使用内在函数来获取匹配索引,而不是迭代整个向量?以 gcc 为例,我可以使用_mm_movemask_epi8对向量进行位打包,然后重复应用 来__builtin_clz获取第一个设置位的索引(这对于我拥有的稀疏数字来说性能更高)。或者,我可以有一个查找表,它对我的位打包整数中的每个半字节执行正确的操作(例如此处的第一个答案)。
Rust 中是否有与这些指令等效的指令?
我正在针对 Intel x86-64 处理器进行编译,并且不需要跨平台支持。
注意:我更喜欢原生(安全)铁锈的解决方案,但这不是硬性要求。我可以编写不安全的 rust,甚至可以使用某种 FFI 链接到上述方法。
c++ ×4
rust ×3
c++17 ×2
python ×2
python-2.7 ×2
allocation ×1
apache-kafka ×1
atomic ×1
exception ×1
extends ×1
inheritance ×1
intrinsics ×1
lifetime ×1
metadata ×1
modulo ×1
mutable ×1
mysql ×1
pointers ×1
random ×1
reference ×1
simd ×1
sqlalchemy ×1
x86 ×1