由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问一个面试题
相关主题
贡献两道google面试题问求array中3个数和最接近k的解法
一道面试题以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)
参院最后法案吧H1B 名额又增加很多?问一道F家面试题
问一个不知道算是软工还是C++的面试题google 一题
L家和G家的几道面试题不懂新手请教:C++ decrement loop (转载)
A simple google interview questionM onsite面经
LRU Cache class:有没有面试可用的精简一些的Sample CodeSQL, recruiter发过来的面试题
攒人品之facebook电面面经Coding test: you can get a job if you can provide a solution
相关话题的讨论汇总
话题: val话题: dest话题: int话题: old话题: mem
进入JobHunting版参与讨论
1 (共1页)
I*******g
发帖数: 7600
1
Imagine the following compare_and_swap (CAS) primitive function is available
to
you. Using this function, write an integer counter class providing increment,
decrement, set and get APIs in an atomic and thread-safe way in a context
subject to
periodic highly intensive concurrent activity .
/* In a critical section, if (*dest) == old_val, then updates dest to new_
val and returns true otherwise leaves dest unchanged and returns false.
*/
bool compare_and_swap(int* dest, int old_val, int new_val)
I*******g
发帖数: 7600
2
要求用C++

available
increment,

【在 I*******g 的大作中提到】
: Imagine the following compare_and_swap (CAS) primitive function is available
: to
: you. Using this function, write an integer counter class providing increment,
: decrement, set and get APIs in an atomic and thread-safe way in a context
: subject to
: periodic highly intensive concurrent activity .
: /* In a critical section, if (*dest) == old_val, then updates dest to new_
: val and returns true otherwise leaves dest unchanged and returns false.
: */
: bool compare_and_swap(int* dest, int old_val, int new_val)

z******g
发帖数: 271
3
class AtomicCounter {
private:
volatile int mem;
public:
AtomicCounter(): mem(0) {}
int get() { return mem; }
void inc() {
int new_val, old_val;
do {
old_val = mem;
new_val = old_val + 1;
} while(!compare_and_swap(&mem, old_val, new_val));
}
void dec() {
// same
}
};
1 (共1页)
进入JobHunting版参与讨论
相关主题
Coding test: you can get a job if you can provide a solutionL家和G家的几道面试题不懂
a MS interview question about C++A simple google interview question
求STRING COMPRESSION一题C++解法(CC150 1.5)LRU Cache class:有没有面试可用的精简一些的Sample Code
问个bloomberg的面试题攒人品之facebook电面面经
贡献两道google面试题问求array中3个数和最接近k的解法
一道面试题以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)
参院最后法案吧H1B 名额又增加很多?问一道F家面试题
问一个不知道算是软工还是C++的面试题google 一题
相关话题的讨论汇总
话题: val话题: dest话题: int话题: old话题: mem