我知道在C中的函数中声明一个静态变量意味着该变量在函数调用之间保留其状态.在线程的上下文中,这会导致变量在多个线程上保持其状态,还是在每个线程之间具有单独的状态?
以下是我正在努力回答的过去的纸质考试问题:
以下C函数旨在用于为其调用者分配唯一标识符(UID):
Run Code Online (Sandbox Code Playgroud)get_uid() { static int i = 0; return i++; }解释get_uid()在多线程调用它的环境中可能会以何种方式正常工作.使用特定示例方案,详细说明可能发生此类错误行为的原因和方式.
目前我假设每个线程都有一个单独的变量状态,但我不确定这是否正确,或者答案是否与缺少互斥有关.如果是这种情况,那么在这个例子中如何实现信号量呢?
我写过这个钩子:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/mutex.h>
static struct nf_hook_ops nfho;
static struct mutex critical_section;
unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *)) {
mutex_lock(&critical_section);
mutex_unlock(&critical_section);
return NF_ACCEPT;
}
int init_module() {
nfho.hook = hook_func;
nfho.hooknum = NF_INET_PRE_ROUTING;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;
mutex_init(&critical_section);
nf_register_hook(&nfho);
return 0;
}
void cleanup_module() {
nf_unregister_hook(&nfho);
}
Run Code Online (Sandbox Code Playgroud)
init部分:
mutex_init(&queue_critical_section);
mutex_init(&ioctl_critical_section);
Run Code Online (Sandbox Code Playgroud)
我已经定义了静态变量:
static struct mutex queue_critical_section; …Run Code Online (Sandbox Code Playgroud) c netfilter linux-device-driver linux-kernel mutual-exclusion
如果我有一个正则表达式列表,是否有一种简单的方法可以确定它们中没有两个都会返回相同字符串的匹配项?
也就是说,列表是有效的,当且仅当对于所有字符串,列表中最多一个项目将匹配整个字符串.
似乎这将是非常困难的(可能不可能?)来明确证明,但我似乎无法找到关于这个主题的任何工作.
我问的原因是我正在处理一个接受正则表达式的标记化器,我想确保一次只有一个标记可以匹配输入的头部.
我有一个代表有限状态机的类,它应该在永久循环中运行并检查它的当前状态.在每个状态机器中将设置它的下一个状态,或者进入idle状态或做一些工作.我想允许另一个线程在它运行时改变机器的状态.这将导致预期的竞争条件.所以我添加了一个互斥锁定/解锁包装循环的机器和公共方法,允许其他线程改变机器的当前状态.
class Robot
{
public:
enum StateType {s1,s2,s3,idle,finish};
void run();
void move();
private:
StateType currentState;
StateType nextState;
StateType previousState;
std::mutex mutal_state;
};
Run Code Online (Sandbox Code Playgroud)
执行:
void Robot::run()
{
this->currentState = s1;
while(true)
{
mutal_state.lock();
switch(currentState)
{
case s1:
// do some useful stuff here...
currentState = idle;
nextState = s3;
break;
case s2:
// do some other useful stuff here...
currentState = idle;
nextState = finish;
break;
case s3:
// again, do some useful things...
currentState = idle;
nextState = …Run Code Online (Sandbox Code Playgroud) 我有一个表交易,我在一个交易中保存两个记录,一个用于借记和其他贷记.
所以,我有表两列creditAmount(Money)和debitAmount(Money).
我想要一个表级约束,每列中的任何一列都不为空.即如果第3行为creditAmount空则debitAmount必须保留一些值和反之亦然.
连续插入记录时如何确保相同?
无论如何使用cassandra内置函数同步客户端?
我需要执行一些操作,这些操作需要与所有其他客户端同步(互斥).
在RDBMS中,我可以锁定整个表或为同步目的准备特殊表,并在其上使用SELECT ... FOR UPDATE.
我可以在没有任何第三方应用程序的情况下与Cassandra达成协议吗?如果不是最好的方法是什么?优选的语言是Java和Python.
我有一个网页,其中异步触发某个Ajax事件.这个Ajax部分可以被调用一次或多次.我无法控制触发此事件的次数,也无法控制时间.
此外,Ajax部分中有一些代码应作为关键部分运行,这意味着,当它运行时,不应该运行该代码的其他副本.
这是一个伪代码:
我的问题是,我怎样才能按照上述方式运行第2步?如何使用JavaScript或jQuery创建/保证互斥部分.
我理解理论(信号量,锁,等等),但我无法使用JavaScript或jQuery实现解决方案.
编辑
如果你建议一个布尔变量进入临界区,这将不起作用,下面的行将解释原因.
关键部分的代码如下(使用布尔变量建议):
load_data_from_database = function () {
// Load data from the database. Only load data if we almost reach the end of the page
if ( jQuery(window).scrollTop() >= jQuery(document).height() - jQuery(window).height() - 300) {
// Enter critical section
if (window.lock == false) {
// Lock the critical section
window.lock = true;
// Make Ajax call
jQuery.ajax({
type: 'post',
dataType: 'json',
url: path/to/script.php,
data: {
action: 'action_load_posts'
},
success: function (response) …Run Code Online (Sandbox Code Playgroud) javascript jquery semaphore critical-section mutual-exclusion
我已经阅读了 Peterson 互斥算法的这篇文章。然后有一个问题,如果我们在 do...while 循环中重新排序第一个和第二个命令,会发生什么?如果我们这样做,我看不到会发生什么...有人可以告诉我我错过了什么吗?
do {
flag[me] = TRUE;
turn = other;
while (flag[other] && turn == other)
;
critical section
flag[me] = FALSE;
remainder section
} while (TRUE);
Run Code Online (Sandbox Code Playgroud) 我想编写一个一次只能由一个线程访问的函数。我不需要忙碌的等待,如果另一个线程已经在运行它,那么残酷的“拒绝”就足够了。这是我到目前为止想出的:
std::atomic<bool> busy (false);
bool func()
{
if (m_busy.exchange(true) == true)
return false;
// ... do stuff ...
m_busy.exchange(false);
return true;
}
Run Code Online (Sandbox Code Playgroud)
std::memory_order_acq_rel?据我了解,宽松的排序 ( std::memory_order_relaxed) 不足以防止重新排序。mutual-exclusion ×10
c ×2
c++ ×2
locking ×2
algorithm ×1
c++11 ×1
cassandra ×1
command ×1
concurrency ×1
javascript ×1
jquery ×1
linux-kernel ×1
lock-free ×1
netfilter ×1
regex ×1
semaphore ×1
sql-server ×1
static ×1
stdatomic ×1
t-sql ×1