鉴于该代码这个岗位,实行Semaphore
只使用atomic<>
和mutex
.
我只是好奇,既然count
已经被警卫了updateMutex
,是atomic<>
必要的吗?
struct Semaphore {
int size;
atomic<int> count;
mutex updateMutex;
Semaphore(int n) : size(n) { count.store(0); }
void aquire() {
while (1) {
while (count >= size) {}
updateMutex.lock();
if (count >= size) {
updateMutex.unlock();
continue;
}
++count;
updateMutex.unlock();
break;
}
}
void release() {
updateMutex.lock();
if (count > 0) {
--count;
} // else log err
updateMutex.unlock();
}
};
Run Code Online (Sandbox Code Playgroud)
没有atomic
,我认为构造函数会出现同步问题.如果其他线程在构造后立即使用它,则可能无法看到计数分配.
如果是这样,怎么样size …
我创建了一个模板类,并将T作为默认类型参数传递.但是这会导致编译失败.谁能解释会发生什么?谢谢!
PS.我使用的编译器是VS2012.
#include <functional>
using namespace std;
template <typename T = void()>
struct delegate
{
typedef function<T> function_t;
function_t f;
};
int main()
{
delegate<> d;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
1>.\Microsoft Visual Studio 11.0\VC\include\functional(554): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1> with
1> [
1> _Tx=void (__cdecl *)(void)
1> ]
1> test.cpp(12) : see reference to class template instantiation 'std::function<_Fty>' being compiled
1> with
1> [
1> _Fty=void (__cdecl *)(void)
1> ]
1> test.cpp(17) : see reference to class …
Run Code Online (Sandbox Code Playgroud) StringOps doc中有一个Shadowed Implicit Value Members部分.例如:
def split(arg0:String,arg1:Int):Array [String]
隐式信息
此成员是通过scala.Predef中的方法unaugmentString执行的从StringOps到String的隐式转换添加的.阴影
此隐式继承的成员由此类中的一个或多个成员遮蔽.要访问此成员,您可以使用类型归属:Run Code Online (Sandbox Code Playgroud)(stringOps: String).split(arg0, arg1)
定义类
字符串
但是当我尝试运行以下程序时:
"aaa bbb ccc".split(" ", 2) //> res0: Array[String] = Array(aaa, bbb ccc)
调用String.split(arg0: String, arg1: Int)
不需要使用类型描述文档中描述的类型.
那么Shadowed Implicit Value会员指的是什么?我试过问谷歌但找不到任何参考.
是这样的:
class A {
def foo() = println("foo")
}
class AOps(a: A) {
def bar() = println("bar")
def foo() = println("new foo")
def foo(i: Int) = println("foo %d".format(i))
}
object Program {
implicit def A2AOps(a: …
Run Code Online (Sandbox Code Playgroud) 我正在读" Inside the C++ Object Model "一书.书中有一个例子:
struct Base1
{
int v1;
};
struct Base2
{
int v2;
};
class Derived : public Base1, public Base2 {};
printf("&Derived::v1 = %p\n", &Derived::v1); // Print 0 in VS2008/VS2012
printf("&Derived::v2 = %p\n", &Derived::v2); // Print 0 in VS2008/VS2012
Run Code Online (Sandbox Code Playgroud)
在前面的代码中,地址Derived :: v1&Derived :: v2的打印都是0.但是,如果通过变量打印相同的地址:
int Derived::*p;
p = &Derived::v1;
printf("p = %p (&Derived::v1)\n", p); // Print 0 in VS2008/VS2012 as before
p = &Derived::v2;
printf("p = %p (&Derived::v2)\n", p); // Print 4 …
Run Code Online (Sandbox Code Playgroud)