标签: synchronization

C 编程语言中的严格交替(来自 Tanenbaum)

为什么进程0的第一个入口不严格交替测试 while (turn == 0) //then Enter 进程0怎么能进入 while (turn != 0),这和 while (turn != 0) 不一样吗== 1) ?

turn = 0;
//process 0 to enter
while (TRUE) {
  while (turn != 0)
    critical_region();
  turn = 1;
  noncritical_region();
}

//process 1 to enter
while (TRUE) {
  while (turn != 1)
    critical_region();
  turn = 0;
  noncritical_region();
}
Run Code Online (Sandbox Code Playgroud)

c testing synchronization

0
推荐指数
1
解决办法
3991
查看次数

在 shell 脚本中将同步 cmd,等待第一个 cmd 完全执行

在 shell 脚本中,第二个 cmd 会等到第一个 cmd 完全执行(即同步文件夹的情况下)吗?

例如:

cmd1 sync folder1 
cmd2 cp folder1 to folder2
Run Code Online (Sandbox Code Playgroud)

同步完成后cmd2会在这里运行吗?如果同步失败怎么办?

bash shell synchronization

0
推荐指数
1
解决办法
746
查看次数

__threadfence 暗示了 __syncthreads 的效果吗?

我正在 CUDA 中实现并行缩减。

内核等待__syncthreads所有线程完成对共享内存的 2 次读取,然后将总和写回共享内存。

我应该使用 a__threadfence_block来确保下一次迭代的所有线程都可以看到对共享内存的写入,还是__syncthreads按照NVIDIA 示例中给出的方式使用?

parallel-processing synchronization cuda reduction

0
推荐指数
1
解决办法
671
查看次数

Firebase 离线 iOS:如何可靠地保持同步?

我在我的应用中离线使用 Firebase ,以在用户的​​蜂窝信号接收不佳的情况下保留功能。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {    
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
    let all = FIRDatabase.database().reference(withPath:"all")
    all.keepSynced(true)
...
Run Code Online (Sandbox Code Playgroud)

但是,我注意到数据并不总是新鲜的。有时在其他地方修改的数据直到强制退出才会显示。此外,有时在注销并重新登录后不会出现新创建的数据。

是否有手动执行同步的功能?

synchronization ios firebase swift firebase-realtime-database

0
推荐指数
1
解决办法
1557
查看次数

使用 POSIX 信号量的可重用屏障实现

需要一个创建 5 个 pthread 的解决方案。每个 pthread 执行一个函数,该函数涉及循环 10 次。在循环的每次迭代中,线程将 int 从 0 增加到 0.9*MAX_INT,然后打印迭代编号。确保 5 个线程中的每一个都在它们可以开始第 (i+1) 次迭代之前完成循环的第 i 次迭代(即所有线程在每次迭代结束时同步/会合)。我需要使用使用 POSIX 信号量实现的两阶段屏障来强制执行同步约束

我写了以下代码我正确吗?

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

int thread_count;

void* MyThread(void* rank);

int main()

{

  long thread;

   pthread_t* thread_handles;

   thread_count = 5;

   thread_handles = malloc (thread_count*sizeof(pthread_t));

   for (thread = 0; thread < thread_count; thread++)

       pthread_create(&thread_handles[thread],NULL,MyThread,(void*) thread);

   for (thread = 0; thread < thread_count; thread++)

       pthread_join(thread_handles[thread], NULL);

   free(thread_handles);

   return 0;

}

void* Hello(void* rank)

{

    long my_rank = (long) …
Run Code Online (Sandbox Code Playgroud)

c multithreading synchronization semaphore pthreads

0
推荐指数
1
解决办法
4393
查看次数

如果 bar() 和 foo() 互斥,如何在 foo() 中运行 bar()

我有两个函数,比如 foo() 和 bar(),我希望它们互斥,即在 bar() 运行时完全阻止 foo() 的运行或在 foo() 运行时完全阻止 bar() 的运行为线程安全而运行。

但是,我可能会在 foo() 内部调用 bar(),也就是说,当 foo() 在 foo() 内部调用 bar() 时,让 bar() 运行,但其他线程不会调用 bar()。

是否可以?如果是,你能提供大致的想法吗?

尝试在C中使用一个或多个互斥体,很容易使两个函数互斥,但是我不能在foo()中调用bar(),它们会进入死锁。

我不能在 foo() 内部调用 bar() 之前就解锁互斥锁,因为我不能保证下一个 bar() 运行是在 foo() 内部调用的那个。

我正在寻找一种解决方案,即 foo 将阻止 bar,如果它们在不同的线程中运行,bar 将阻止 foo。但是当 foo 在其主体内(同一线程)调用 bar 时,让 bar 运行。

谢谢

c multithreading synchronization pthreads thread-safety

0
推荐指数
1
解决办法
58
查看次数

检查 pthread_cond_t 的值

我试图在 while 循环条件下检查 pthread_cond_t 类型的变量的值。

该变量在节点结构中定义,形式如下:

pthread_cond_t cv;
Run Code Online (Sandbox Code Playgroud)

当我尝试使用此方法检查其值时,!=或者== NULL出现无法完成此操作的错误。这是我尝试检查时的样子:

while(!node->cv) {
Run Code Online (Sandbox Code Playgroud)

其中node是指向包含 cv 的结构的指针。

我收到错误“一元感叹号的参数类型错误,因为我猜它不是布尔值。有没有办法检查这个条件变量是否有值?

c multithreading synchronization pthreads

0
推荐指数
1
解决办法
625
查看次数

锁定和 atom/reset!/swap! 有什么区别?在 Clojure

我正在阅读一些源代码并locking在 Clojure 中遇到了用法。这让我想到了 atom 版本。那么2个代码片段之间有什么区别,我认为它们做同样的事情?

(def lock (Object.))

(locking lock
  ...some operation)
Run Code Online (Sandbox Code Playgroud)
(def state (atom true))

(when @state
  (reset! state false)
  ...some operation
  (reset! state true))
Run Code Online (Sandbox Code Playgroud)

synchronization locking atomic clojure

0
推荐指数
1
解决办法
253
查看次数

使用互斥锁和解锁时 Go 测试挂起

我是 GO 的新手,我不得不在我的代码中使用互斥锁/解锁来防止并发访问。但是在我将 Lock/Unlock 添加到我的代码之后,我的测试开始永远运行。我简化了我的用例并添加了类及其测试文件。如果我单独运行测试,一切都运行良好。但是如果我运行整个文件,前两个就完成了,但第三个永远运行。如果我删除锁定/解锁,那么运行将正确执行。

如果有人能指出我在这里做错了什么,那就太好了。

被测代码:

package anything

import (
    "sync"
)

var maxLimit = 5
var Store = Cache{make([]string, 0)}
var mutex = new(sync.RWMutex)

type Cache struct {
    items []string
}

func (cache *Cache) Push(item string) {
    mutex.Lock()
    if len(cache.items) == maxLimit {
        cache.items = append(cache.items[:0], cache.items[1:]...)
    }
    cache.items = append(cache.items, item)
    mutex.Unlock()
}

func (cache *Cache) Get(content string) string {
    mutex.RLock()
    for _, item := range cache.items {
        if item == content {
            return content
        }
    } …
Run Code Online (Sandbox Code Playgroud)

synchronization unit-testing mutex go

0
推荐指数
1
解决办法
164
查看次数

Java - 允许方法中的一个线程无需等待

我遇到一种情况,我需要实现一个线程安全的方法,该方法一次只能由一个线程执行,当该方法由一个线程执行时,所有其他尝试执行同一方法的线程都应该执行t 等待并且必须退出该方法。

同步在这里没有帮助,因为线程将等待顺序执行该方法。

我想我可以通过使用下面的代码使用 ConcurrentHashMap 来实现这一点,但不确定这是否是实现它的完美方法。

Class Test {

private ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();

    public void execute() {
        
        if (map.putIfApsent("key", new Object()) != null) { // map has value for key which means a thread has already entered.
            return; // early exit
        }
        
        threadSafeMethod();
        map.remove("key");
    }
    
    private void threadSafeMethod() {
        // my code
    }
}
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading synchronization single-threaded

0
推荐指数
1
解决办法
843
查看次数