p****e 发帖数: 37 | 1 1. print a linked list reversely.
I write 3 methods,
1. recursive
2. use a temporary vector to save list nodes
3. reverse the linked list first, and then print (we could reverse it back
if required)
Then I was asked in which situation method 2 is better than 3. (the answer
he want is multi-thread, I didn't get it :( )
2. Suppose we have a following struct:
struct Bits{ unsigned known, unsigned value};
if a certain bit in known is set, it means the value of this bit is known,
and its corresponding value is saved in Bits->value.
for example, if known = 0011, value = 0001, that means Bits has last 2 bits
value known, one is 0 and the other is 1.
implement the following method to calculate the or of 2 Bits:
void or(Bits* out, const Bits& in1, const Bits& in2);
BTW, still didn't got hr's feedback yet.... |
g*****i 发帖数: 2162 | 2 bless
第一题多线程为啥2好?感觉list被其他thread修改的话两种方法都受影响啊
第二题value初始值都是0吗?如果是的话似乎3行code就出来了啊.
int k=in1.known & in2.known;
int v=in1.value | in2.value;
return k & v; |
P**********c 发帖数: 3417 | 3 我觉得可能是解法2没有对原linked list做改动,对这个函数本身而言是thread safe
的。当然如果其他thread对它有改动还是会有问题,但是这个至少有可能做到thread
safe。如果大家都是读,不需要lock.
解法3 reverse的时候自己就把linked list改了,没法thread safe了,只能lock了。
而且这个lock必须从第一次reverse linked list开始,一直到reverse back
为止。否则期间其他thread access linked list就乱七八糟了。
【在 g*****i 的大作中提到】 : bless : 第一题多线程为啥2好?感觉list被其他thread修改的话两种方法都受影响啊 : 第二题value初始值都是0吗?如果是的话似乎3行code就出来了啊. : int k=in1.known & in2.known; : int v=in1.value | in2.value; : return k & v;
|
g*****i 发帖数: 2162 | 4 多谢解释
safe
【在 P**********c 的大作中提到】 : 我觉得可能是解法2没有对原linked list做改动,对这个函数本身而言是thread safe : 的。当然如果其他thread对它有改动还是会有问题,但是这个至少有可能做到thread : safe。如果大家都是读,不需要lock. : 解法3 reverse的时候自己就把linked list改了,没法thread safe了,只能lock了。 : 而且这个lock必须从第一次reverse linked list开始,一直到reverse back : 为止。否则期间其他thread access linked list就乱七八糟了。
|
c*******n 发帖数: 63 | 5 写个简单的第二题
void OR(Bits *out, const Bits &in1, const Bits &in2)
{
unsigned int sntnl = 1;
assert(out);
out->known = 0;
out->value = 0;
while(sntnl >0)
{
if( (in1.known&sntnl && in1.value&sntnl)
|| (in2.known&sntnl && in2.value&sntnl) )
{
out->value |= sntnl;
out->known |= sntnl;
}
else if(in1.known&sntnl && in2.known&sntnl)
{
out->known |= sntnl;
}
sntnl <<= 1;
}
} |
f*******t 发帖数: 7549 | 6 第二题,有两种情况下输出的值是确定的
1.in1->known = 1, in2->known = 1
2.某个inX->known = 1,以及inX->value = 1,因为是or操作,所以输出必然是1
我觉得答案是:
out->known = (in1.known & in1.value) | (in2.known & in2.value) | (in1.known & in2.known);
out->value = in1.value | in2.value; |
l**********d 发帖数: 29 | 7 第二题,这样如何?
out->known = in1.known | in2.known;
out->value = (in1.value&in1.known) | (in2.value&in2.known); |
d****3 发帖数: 93 | 8 out->known = (in1->known&in1->value) | (in2->known&in2->value) | ((in1->
known&in2->known)&(in1->value|in2->value));
out->value = (in1->known&in1->value) | (in2->known&in2->value) | (in1->known
&in2->known);
【在 p****e 的大作中提到】 : 1. print a linked list reversely. : I write 3 methods, : 1. recursive : 2. use a temporary vector to save list nodes : 3. reverse the linked list first, and then print (we could reverse it back : if required) : Then I was asked in which situation method 2 is better than 3. (the answer : he want is multi-thread, I didn't get it :( ) : 2. Suppose we have a following struct: : struct Bits{ unsigned known, unsigned value};
|
r*******g 发帖数: 1335 | 9 看不懂第二题,物理意义是什么,到底是or两个known部分还是or两个value部分?
貌似Bits这个struct维护的就是哪些bit对应有value,这样的话貌似结果就是known的&
和value的|就行了?
【在 p****e 的大作中提到】 : 1. print a linked list reversely. : I write 3 methods, : 1. recursive : 2. use a temporary vector to save list nodes : 3. reverse the linked list first, and then print (we could reverse it back : if required) : Then I was asked in which situation method 2 is better than 3. (the answer : he want is multi-thread, I didn't get it :( ) : 2. Suppose we have a following struct: : struct Bits{ unsigned known, unsigned value};
|