我正在运行一个提供服务的 C++ 程序,并注意到即使不提供任何请求,它也会占用 100% 的 CPU。我将问题缩小到一个 while 循环,该循环调用std::sleep_for以防止服务退出。
为了测试,我编译并运行了这个简单的测试程序:
#include <chrono>
#include <thread>
int main(int argc, char * argv[])
{
std::this_thread::sleep_for(std::chrono::hours::max());
}
Run Code Online (Sandbox Code Playgroud)
我的预期是它会休眠很长时间,事实上,当我在 M1 Mac 上尝试它时,我看到了预期的行为。然而,当我在 Redhat Linux 8 机器上运行它时,它立即返回。我还在 Mac 上运行的 Rocky Linux 8 docker 容器上进行了尝试,这也立即返回。这证实了这种情况通常发生在 RHEL 8 系统上 - 或者至少在 gcc 8.5.0 上发生,因为两个 Linux 系统上的编译器版本是相同的(Mac 上的编译器是 Apple 提供的 clang)。
这解释了为什么我的服务占用了 100% 的 CPU,因为它是在 while 循环中调用的。但我从未听说过这种行为。还有其他人吗?
当然,我可以通过睡一会儿来轻松解决问题std::chrono::seconds(1)。我只是出于求知欲才问这个问题。
我试图确定哪些行具有唯一性(仅在该行中观察到的物种,而不是在物种矩阵的任何其他行中观察到的物种)。我设置了数据矩阵,其中列作为单独的物种,行作为单独的采样单位(在我们的例子中为横断面)。
例如,物种 1 仅在第 8 行中找到,而在数据集中没有其他位置,那么我想知道第 8 行包含 1 个唯一物种。如果物种 4 也只在第 8 行中发现,那么唯一物种的数量将为 2 等。注意:我在数据集中发现的一些唯一物种的丰度大于 1,这意味着它们在横断面上被发现的数量更多不止一次,但它们仍然只在该横断面中被发现(仍然被认为是唯一的)。
以下是一些示例数据,其中第 3 行有两个唯一值,第 5 行有 1 个唯一值:
example_data <- data.frame(Species1 = c(1, 2, 3, 4, 5),
Species2 = c(6, 7, 8, 9, 10),
Species3 = c(0, 0, 13, 0, 0),
Species4 = c(0, 0, 0, 0, 20),
Species5 = c(0, 0, 23, 0, 0))
Run Code Online (Sandbox Code Playgroud)
尝试使用 ChatGPT 但一无所获,我能够通过 VIA excel 找出该数据集确实具有唯一性。我还想出了如何让 R 告诉我哪些物种只被发现一次,但我更感兴趣的是哪些行(横断面)具有唯一性以及有多少,而不是哪些物种是唯一的。
另外,有没有一种方法可以列出所有行和唯一值的数量?对于示例数据,它将是 0,0,2,0,1。我感兴趣的是哪些行也没有唯一性(0 个唯一性)。
当您使用 R 连接两个向量时c(),它“组合参数并产生向量”。它是否通过创建一个新向量来获取两个向量的元素来组合它们,或者是否有一种方法可以按字面意思组合为两个向量分配的数据空间?
当我搜索时,我找不到解释。所有的视觉表示c()实际上只是将第二个向量附加到第一个向量的末尾,但我认为这只是为了让我们可以轻松地理解这个函数的作用,而不是实际发生的情况。
std我想使用自定义分配器为容器定义类型别名,例如
type MyBox<T> = Box<T, A=MyAlloc>;
Run Code Online (Sandbox Code Playgroud)
然而我被告知
error[E0229]: associated type bindings are not allowed here
--> src/lib.rs:12:24
|
12 | type MyBox<T> = Box<T, A=MyAlloc>;
| ^^^^^^^^^ associated type not allowed here
Run Code Online (Sandbox Code Playgroud)
为什么这是不允许的,是否有一些解决方法(每晚都可以)?
在13:00 左右的 WWDC 空间用户界面设计视频中,Apple 对于悬停效果做了以下说明:
当您设计锁定时,请包括一个形状,该形状将允许系统在人们关注它时显示悬停效果。在这里,我们有一些图像,下面有文字。每个锁定都是一个单独的交互元素。您需要定义一个自定义区域,以便它可以在人们观看时变亮。这有助于他们了解整个区域是一个可以选择的元素。并记住在每个包含的形状之间保留小空间。
它们包括来自音乐应用程序的示例:
我想在我的应用程序中执行此操作,在 LazyVGrid 中显示按钮,但无法实现该结果。我知道您需要使用plain按钮样式来隐藏边框按钮的背景但保持悬停效果。虽然悬停效果显示为椭圆形,但我希望它是一个圆角矩形,周围有额外的填充,悬停效果之间有 4 pt 的间距,如上所示。如何才能做到这一点?我以为.contentShape(.hoverEffect, .rect(cornerRadius: 50))会自定义该区域,但它似乎没有改变任何东西。
Button(action: tapAction) {
VStack {
Color.clear
.background(.regularMaterial)
.aspectRatio(1, contentMode: .fit)
.clipShape(.rect(cornerRadius: 10))
HStack(spacing: 0) {
VStack(alignment: .leading, spacing: 0) {
Text("Line one")
Text("Line two")
.foregroundStyle(.secondary)
}
Spacer()
MarkWateredButton(action: markWateredAction)
}
}
.contentShape(.hoverEffect, .rect(cornerRadius: 50).inset(by: -10)) // FIXME: Doesn't do anything
}
.buttonStyle(.plain)
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行 Angular Material 迁移工具以从 16 迁移到 17,但收到一条错误消息,指出目录必须是字符串。JSON 显示它是错误的。没有提示,我正在项目目录中运行该命令。我究竟做错了什么?
\nPS C:\\Users\\x\\portal\\client> ng generate @angular/material:mdc-migration\n? What components do you want to migrate? Button, Card, Checkbox, Chips, Dialog, Form Field, Input, Select,\nAutocomplete, List, Menu, Paginator, Progress Bar, Progress Spinner, Radio, Slide Toggle, Slider, Snack Bar, Table,\nTabs, Tooltip\nSchematic input does not validate against the Schema: {"directory":false,"components":["button","card","checkbox","chips","dialog","form-field","list","menu","paginator","progress-bar","progress-spinner","radio","slide-toggle","slider","snack-bar","table","tabs","tooltip"]}\nErrors:\n\n\xc2\xa0 Data path "/directory" must be string.\nRun Code Online (Sandbox Code Playgroud)\n 因此,在我正在处理的项目中尝试对函数指针使用显式对象参数时,我遇到了问题。乍一看,代码在语法上似乎很好,但由于这是一个新功能,我在网上找不到太多信息,所以我不知道为什么会出现随机语法错误。具体来说,错误是“语法错误:')'”
我唯一真正的猜测是,这是某种 MSVC 不完整性/错误,在这种情况下我可能不得不切换到不同的编译器。
///base.h
///...
#include <map>
template <class s>
using FunctionMap = std::map<std::string,void(*)(this s& self)>;
///^ Syntax Error Here
class base{
//...
public:
///Get Map of fPtr
template <class Self>
FunctionMap<Self>* get_scripts(this const Self& self);
}
Run Code Online (Sandbox Code Playgroud)
我预计它可以编译,但它不断出现语法错误。我知道显式对象参数仅在 MSVC 中部分实现。
以下代码将数组中每种颜色的最大值转换为哈希图。也在Rust Playground中。
use std::collections::HashMap;
use std::cmp;
fn main() {
let array = [
("blue", 1),
("green", 2),
("red", 3),
("blue", 4),
("green", 1),
("red", 2),
];
let mut scores = HashMap::new();
// Convert array of key value into hashmap
array
.into_iter()
.for_each(|(color, color_count)| {
// Update the HashMap
scores
.entry(color)
.and_modify(|e| { *e = cmp::max(*e, color_count) })
.or_insert(color_count);
});
for (key, value) in &scores {
println!("{}: {}", key, value);
}
println!("The product of the values: {}", scores.values().cloned().fold(1, |res, …Run Code Online (Sandbox Code Playgroud) #include <pthread.h>
#include <thread>
int main(){
pthread_mutex_t mut;
if (pthread_mutex_init(&mut, NULL) != 0){
return 0;
}
int a = 0;
auto t1 = std::thread([&](){
for(int c = 0; c<=10000;c++){
pthread_mutex_lock(&mut);
a += 1; //#1 suppose this non-atomic object is protected by the mutex
pthread_mutex_unlock(&mut);
}
});
auto t2 = std::thread([&](){
int r = 0;
for(;;){
pthread_mutex_lock(&mut);
r = a;
std::cout<< a <<std::endl; //#2 suppose this non-atomic object is protected by the mutex
pthread_mutex_unlock(&mut);
if(r>=10000){
return;
}
}
});
t1.join();
t2.join(); …Run Code Online (Sandbox Code Playgroud) 该表的列中有单位。如何将列名称中的单位分隔到另一列中?
例子:
| SUBJID | 剂量(毫克) | 最高温度(小时) |
|---|---|---|
| 2001年 | 200 | 5 |
| 2002年 | 200 | 5 |
这就是我要的。
| SUBJID | 剂量 | 剂量_单位 | 最高温度 | 最大温度单位 |
|---|---|---|---|---|
| 2001年 | 200 | 毫克 | 200 | H |
| 2002年 | 200 | 毫克 | 200 | H |
c++ ×3
r ×3
rust ×2
angular ×1
atomic ×1
c++-chrono ×1
c++20 ×1
c++23 ×1
compiler-bug ×1
dataframe ×1
hover ×1
linux ×1
swift ×1
synchronous ×1
tidyverse ×1
vegan ×1
visionos ×1
visual-c++ ×1