我想用一个例子说明我的问题.
假设有一个N /*(N>>1)*/线程数组被设置为运行此函数:
void Process() {
//Some thread safe processing which requires in-deterministic computation time
unsigned char byte;
std::cin >> byte;
}
Run Code Online (Sandbox Code Playgroud)
一旦所有这些都同时启动,会发生什么?如何处理并发std :: cin访问?在控制台上操作的最终用户可以看到/体验什么?
编辑:还有一件事我想补充一点.下面的代码是否安全到足以放弃在一个(可能是主要的)线程中使用std:cin的想法?
void Process() {
//Some thread safe processing which requires in-deterministic computation time
//Mutex lock
unsigned char byte;
std::cin >> byte;
//Mutex unlock
}
Run Code Online (Sandbox Code Playgroud) 我通过 API 创建了一个非常简单的 LLVM IR 代码。在主块内,我想插入对 C 标准库中可用函数的调用,例如malloc,printf或我自己用 C 编写的其他一些函数。
; ModuleID = 'main.d2'
source_filename = "main.d2"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin17.7.0"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
Run Code Online (Sandbox Code Playgroud)
在此处插入呼叫
%2 = load i32, i32* %1
ret i32 %2
}
Run Code Online (Sandbox Code Playgroud)
我用
llc -filetype=obj -o main.o main.bit
并将对象与
clang main.o -o main
命令。
我是否需要在最后一步将 C stdlib 与编译器标志链接和/或我是否需要在 C 中准备一个包装库,其中包含我需要的函数以便编译类似clib.o外部链接的东西?第二个命令是链接此类外部依赖项的正确方法吗?
另外,如果我使用相同的方法,它在 Windows 上的表现如何(我现在在 Mac 或 Ubuntu …
为我的忽视而道歉.我无法弄清楚如何使用不同的源文件声明内部类的主体.我想要实现的是这样的:
source1.h:
class A {
class B;
}
Run Code Online (Sandbox Code Playgroud)
source2.h:
class B {
//
}
Run Code Online (Sandbox Code Playgroud)
source3.cpp: //Implementation of A
source4.cpp: //Implementation of B
额外:在这些声明中如何隐含A和B之间的友谊?