考虑以下spin_lock()
实现,最初来自这个答案:
void spin_lock(volatile bool* lock) {
for (;;) {
// inserts an acquire memory barrier and a compiler barrier
if (!__atomic_test_and_set(lock, __ATOMIC_ACQUIRE))
return;
while (*lock) // no barriers; is it OK?
cpu_relax();
}
}
Run Code Online (Sandbox Code Playgroud)
我所知道的:
volatile
防止编译器*lock
在while
循环的每次迭代中优化重新读取;volatile
不插入内存或编译器障碍 ;x86
(例如在Linux内核中)和其他一些架构;spin_lock()
执行针对通用体系结构; 这个例子插入它们__atomic_test_and_set()
.问题:
volatile
这里是否足够或者是否存在while
循环中需要内存或编译器障碍或原子操作的架构或编译器?
1.1根据C++
标准?
1.2在实践中,对于已知的体系结构和编译器,特别是它支持的GCC和平台?
while
根据C++11
它的内存模型,循环是否安全?有几个相关的问题,但我无法从它们构建一个明确和明确的答案:
原则上:是的,如果程序执行从一个核心移动到下一个核心,则可能看不到在先前核心上发生的所有写入. …
我有一个嵌入式系统,我在其上telnet
运行,然后在后台运行一个应用程序:
./app_name &
Run Code Online (Sandbox Code Playgroud)
现在如果我关闭我的终端并telnet
从其他终端做,如果我检查然后我可以看到这个过程仍在运行.
为了检查这个,我写了一个小程序:
#include<stdio.h>
main()
{
while(1);
}
Run Code Online (Sandbox Code Playgroud)
我在后台运行我的本地linux pc程序,然后我关闭了终端.
现在,当我从其他终端检查这个过程时,我发现这个过程也被杀死了.
我的问题是:
我将另一个分支重新绑定到我的结账分支上,并在rebase期间发生冲突.我解决了合并冲突.
$ git status
rebase in progress; onto 77c951b
You are currently rebasing branch 'test' on '77c951b'.
(all conflicts fixed: run "git rebase --continue")
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: br_boss_buha_faktura/forms/br_boss_buha_faktura_head_dtl.frm
modified: br_boss_buha_faktura/valuelists/br_boss_buha_faktura_client.val
new file: br_boss_buha_faktura/valuelists/br_boss_buha_faktura_client_name.val
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: br_boss_buha_faktura/valuelists/br_boss_buha_faktura_client.val
Run Code Online (Sandbox Code Playgroud)
我是否需要提交上述已解决的合并冲突,git commit
还是可以直接进一步使用git rebase --continue
?
我有两个(或更多)python进程在运行,并且想要创建一个类似于共享资源的排除互斥锁的概念.在这种情况下,"共享资源"是一个目录.我最容易/标准地/等等如何实现互斥锁?.lock
每个进程同意检查的隐藏文件,如果存在,则将其PID附加为新行,然后在有权访问该文件时弹出其PID?
我基本上只是想清除一个目录,并确保在我清除它时没有其他进程尝试读取或写入它.
是否有标准的linux方式这样做?也许我可以用python的shell行执行某些操作?
假设我们有一个FILE_SIZE
字节文件,并且:
FILE_SIZE <= min(page_size, physical_block_size)
;truncate()
或追加write()
);文件只能通过使用以下方法完全覆盖其内容来修改:
pwrite(fd, buf, FILE_SIZE, 0);
是否保证ext4
:
对于系统崩溃,此类写入是事务性的吗?
(即,崩溃后文件的内容完全来自先前的写入,我们永远不会看到部分写入或空文件)
第二个是真的吗:
data=ordered
?用data=journal
或者用单个文件日志启用?
(使用ioctl(fd, EXT4_IOC_SETFLAGS, EXT4_JOURNAL_DATA_FL)
)
什么时候physical_block_size < FILE_SIZE <= page_size
?
2
。我对 Go 还很陌生,但我来自 C++ 学校。我只想制作一个项目并将逻辑分成多个文件。
在 C++ 中,我只需要穿上我的main_file.cpp
单
#include "my_own_lib.hpp"
Run Code Online (Sandbox Code Playgroud)
(类似于Node.js 中的module.exports
和 then require('relative/path/to/my-own-lib')
)
就是这样。在 Go 中我遵循相同的逻辑,但我的结果是:
$ go run main.go
main.go:4:8: open /Users/mt/Documents/Codes/go/src/github.com/mt/Apollo/tst: no such file or directory
Run Code Online (Sandbox Code Playgroud)
我的文件:
主程序
package main
import "fmt"
import "./tst"
func main() {
fmt.Println("just testing")
tst.p()
}
Run Code Online (Sandbox Code Playgroud)
尖沙咀
package tst
import "fmt"
func p() {
fmt.Println("ola")
}
Run Code Online (Sandbox Code Playgroud)
当然我的文件结构是:
myFolder/
|- main.go
|_ tst.go
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这样做的正确方法是什么吗?
我正在尝试与 Go 中的一些 C 代码交互。使用 cgo,这一直相对简单,直到我遇到这种(相当常见的)情况:需要将指针传递给本身包含指向某些数据的指针的结构。如果不诉诸于将结构的创建放入 C 代码本身,我似乎无法弄清楚如何从 Go 中做到这一点,我不想这样做。这是一个说明问题的片段:
package main
// typedef struct {
// int size;
// void *data;
// } info;
//
// void test(info *infoPtr) {
// // Do something here...
// }
import "C"
import "unsafe"
func main() {
var data uint8 = 5
info := &C.info{size: C.int(unsafe.Sizeof(data)), data: unsafe.Pointer(&data)}
C.test(info)
}
Run Code Online (Sandbox Code Playgroud)
虽然编译正常,但尝试运行它会导致:
panic: runtime error: cgo argument has Go pointer to Go pointer
Run Code Online (Sandbox Code Playgroud)
在我的例子中,传递给 C 调用的数据在调用后不会持续存在(即,有问题的 C 代码深入结构,复制它需要的内容,然后返回)。
我的函数的返回值类型是OrderedDict
,
现在我想在文件上写这个:
这是我的代码:
mainDict = OrderedDict([('a',1),('b',2),('c',3),('d',[4,5,6])])
with open(outFileName, 'w', encoding='utf-8') as outFile :
outFile.write(ujson.dumps(mainDict, indent=4))
Run Code Online (Sandbox Code Playgroud)
我希望它能保持文件中字典的顺序,但它混淆了.
是因为使用ujson.dumps
?以及如何OrderedDict
在输出文件中保留a的顺序?
我需要使用“gotests”命令在 Go 中使用测试驱动开发。
gotests -all *
Run Code Online (Sandbox Code Playgroud)
这是行不通的。我做了go get -u /github.com/cweill/gotests
和go install
。但是没有在$GOPATH/bin
.
go ×3
linux ×3
python ×2
c++ ×1
cgo ×1
ext4 ×1
filesystems ×1
gcc ×1
git ×1
git-branch ×1
git-merge ×1
git-rebase ×1
import ×1
job-control ×1
linux-kernel ×1
python-3.4 ×1
shell ×1
spinlock ×1
telnet ×1
ujson ×1