mutex 相关问题

互斥(“互斥”)是一种在多个线程同时访问(特别是更改)相同数据或资源时确保完整性的机制。

在 Dart 中使用互斥量

我有一个 flutter 桌面应用程序,想防止它的多个实例同时运行。我尝试为此使用 Mutex,但代码不起作用。谁能纠正它...

回答 0 投票 0

我的工作线程实现 c++ 中有这个死锁问题

我正在尝试实现一个工作对象,它是一个等待任务并仅在销毁时终止的线程: #包括 #包括 #包括 #

回答 2 投票 0

单个作者和多个读者的单次写入并发

NOTE:本题与其他并发题不同的是,不仅writer是单一的,而且write操作严格发生一次。 Go to 中有什么同步方法...

回答 1 投票 0

在消费者-生产者问题中我应该把阅读条件放在哪里?

假设我有两种生成器,一种通过读取文本文件(偶数集)生成数据,另一种使用 rand() 函数生成奇数。 现在我想要两种消费者

回答 0 投票 0

Rust 中 `ndarray` `Array2<f64>` 值的并行计算

我正在尝试加快计算速度,但我不确定如何最好地使用 .into_par_iter() 或 ndarray crate 中的一些 Zip:: 选项,以便“正确”并行化

回答 1 投票 0

使用互斥多线程时,在 Form1 上更新进度条

我正在尝试在将数据写入 MS Access 数据库时更新进度条。为了使写入数据库的速度更快。我已经将数据表中的数据分成批次......

回答 0 投票 0

我可以在同一线程中共享 shared_lock 和 unique_lock 之间的互斥量吗?

考虑以下功能: std::shared_mutex 互斥; 无效的 foo() { std::shared_lock readLock{ mutex }; // ... // 函数的只读部分 // ... { 标准::

回答 2 投票 0

go 例程同步失败

我试图在 golang 中实现一个速率限制器。我使用的算法是滑动窗口。 方法 我正在使用地图来存储特定秒内的请求数。所以当一个新的请求到来时,我...

回答 0 投票 0

Python 脚本没有创建足够的输出行

我需要在 Python 中使用多处理将 1,000,000 个随机数写入单个文件,并注意它如何影响执行时间。出于某种原因,每当我运行脚本时,我只会结束......

回答 0 投票 0

C++ std::thread 和 std::mutex 应用程序崩溃(程序异常终止)

我有一个 C++Builder VCL DLL 项目,我的窗体上有一个计时器。计时器调用一个函数,该函数创建一个新的 std::thread(分离),thread_function 调用其他函数并执行

回答 0 投票 0

Mutex 使用 2 个变量

我正在研究一个由 2 个独立设备组成的系统,这些设备通过“公共内存”交换数据。 为了同步它们,我想使用一个内存位置作为“状态变量”......

回答 1 投票 0

如果线程仅在持有互斥量时使用它,共享变量是否需要可变? [重复]

在 Java 语言规范中的 Java 内存模型 (JMM) 中,如果线程 A 写入一个字段然后解锁互斥量,则写入“发生在”解锁之前。这意味着两个线程

回答 0 投票 0

在进程之间共享条件变量和互斥量:之前是否必须锁定互斥量?

我需要一些帮助来理解如何在 C 中使用条件变量来解决练习。这是一个小例子: #包括 #包括 #包括 我需要一些帮助来理解如何在 C 中使用条件变量来解决练习。这是一个小例子: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #define OKTOWRITE "/oktowrite" #define MESSAGE "/message" #define MUTEX "/lock" int main(int argc, char** argv) { pthread_cond_t* condition; pthread_mutex_t *mutex; char* message; int des_cond, des_msg, des_mutex; int mode = S_IRWXU | S_IRWXG; des_mutex = shm_open(MUTEX, O_CREAT | O_RDWR | O_TRUNC, mode); if (des_mutex < 0) { perror("failure on shm_open on des_mutex"); exit(1); } if (ftruncate(des_mutex, sizeof(pthread_mutex_t)) == -1) { perror("Error on ftruncate to sizeof pthread_cond_t\n"); exit(-1); } mutex = (pthread_mutex_t*) mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, des_mutex, 0); if (mutex == MAP_FAILED ) { perror("Error on mmap on mutex\n"); exit(1); } pthread_mutex_init(mutex, NULL ); des_cond = shm_open(OKTOWRITE, O_CREAT | O_RDWR | O_TRUNC, mode); if (des_cond < 0) { perror("failure on shm_open on des_cond"); exit(1); } if (ftruncate(des_cond, sizeof(pthread_cond_t)) == -1) { perror("Error on ftruncate to sizeof pthread_cond_t\n"); exit(-1); } condition = (pthread_cond_t*) mmap(NULL, sizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_SHARED, des_cond, 0); if (condition == MAP_FAILED ) { perror("Error on mmap on condition\n"); exit(1); } pthread_cond_init(condition, NULL ); if (!fork()) { sleep(3); pthread_mutex_lock(mutex); pthread_cond_signal(condition); pthread_mutex_unlock(mutex); printf("son signaled\n"); exit(0); } else { printf("wait on condition\n"); pthread_mutex_lock(mutex); pthread_cond_wait(condition, mutex); pthread_mutex_unlock(mutex); printf("Signaled by son process, wake up\n"); pthread_mutex_destroy(mutex); pthread_cond_destroy(condition); shm_unlink(OKTOWRITE); shm_unlink(MESSAGE); shm_unlink(MUTEX); return 0; } } 问题是进程的父亲继续被锁定,即使在儿子发出信号之后也是如此。一切都在共享内存中(使用shm_open和mmap)所以两个进程的条件应该相同。 在调用 wait 或 signal 之前锁定互斥锁可能是我犯了一个错误吗? 编辑: 感谢所有帮助过我的人。这是标有关键部分的正确代码: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #define OKTOWRITE "/condwrite" #define MESSAGE "/msg" #define MUTEX "/mutex_lock" int main(int argc, char** argv) { pthread_cond_t* condition; pthread_mutex_t* mutex; char* message; int des_cond, des_msg, des_mutex; int mode = S_IRWXU | S_IRWXG; des_mutex = shm_open(MUTEX, O_CREAT | O_RDWR | O_TRUNC, mode); if (des_mutex < 0) { perror("failure on shm_open on des_mutex"); exit(1); } if (ftruncate(des_mutex, sizeof(pthread_mutex_t)) == -1) { perror("Error on ftruncate to sizeof pthread_cond_t\n"); exit(-1); } mutex = (pthread_mutex_t*) mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, des_mutex, 0); if (mutex == MAP_FAILED ) { perror("Error on mmap on mutex\n"); exit(1); } des_cond = shm_open(OKTOWRITE, O_CREAT | O_RDWR | O_TRUNC, mode); if (des_cond < 0) { perror("failure on shm_open on des_cond"); exit(1); } if (ftruncate(des_cond, sizeof(pthread_cond_t)) == -1) { perror("Error on ftruncate to sizeof pthread_cond_t\n"); exit(-1); } condition = (pthread_cond_t*) mmap(NULL, sizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_SHARED, des_cond, 0); if (condition == MAP_FAILED ) { perror("Error on mmap on condition\n"); exit(1); } /* HERE WE GO */ /**************************************/ /* set mutex shared between processes */ pthread_mutexattr_t mutexAttr; pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(mutex, &mutexAttr); /* set condition shared between processes */ pthread_condattr_t condAttr; pthread_condattr_setpshared(&condAttr, PTHREAD_PROCESS_SHARED); pthread_cond_init(condition, &condAttr); /*************************************/ if (!fork()) { sleep(10); pthread_mutex_lock(mutex); pthread_cond_signal(condition); printf("son signaled\n"); pthread_mutex_unlock(mutex); exit(0); } else { printf("father waits on condition\n"); pthread_mutex_lock(mutex); pthread_cond_wait(condition, mutex); pthread_mutex_unlock(mutex); printf("Signaled by son process, wake up!!!!!!!!\n"); pthread_condattr_destroy(&condAttr); pthread_mutexattr_destroy(&mutexAttr); pthread_mutex_destroy(mutex); pthread_cond_destroy(condition); shm_unlink(OKTOWRITE); shm_unlink(MESSAGE); shm_unlink(MUTEX); } return 0; } 要在进程之间共享,需要通过正确初始化的属性相应地初始化互斥量:http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_setpshared.html #include <pthread.h> ... pthread_mutex_t * pmutex = NULL; pthread_mutexattr_t attrmutex; /* Initialise attribute to mutex. */ pthread_mutexattr_init(&attrmutex); pthread_mutexattr_setpshared(&attrmutex, PTHREAD_PROCESS_SHARED); /* Allocate memory to pmutex here. */ /* Initialise mutex. */ pthread_mutex_init(pmutex, &attrmutex); /* Use the mutex. */ /* Clean up. */ pthread_mutex_destroy(pmutex); pthread_mutexattr_destroy(&attrmutex); (为了本示例的可读性,省略了错误检查) 这同样适用于应该在进程之间共享的条件变量:http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_setpshared.html #include <pthread.h> ... pthread_cond_t * pcond = NULL; pthread_condattr_t attrcond; /* Initialise attribute to condition. */ pthread_condattr_init(&attrcond); pthread_condattr_setpshared(&attrcond, PTHREAD_PROCESS_SHARED); /* Allocate memory to pcond here. */ /* Initialise condition. */ pthread_cond_init(pcond, &attrcond); /* Use the condition. */ /* Clean up. */ pthread_cond_destroy(pcond); pthread_condattr_destroy(&attrcond); (为了本示例的可读性,省略了错误检查) 另请参阅此答案:https://stackoverflow.com/a/2390670/694576 等待一个条件之前应该有一个 while 语句,像这样: pthread_mutex_lock(mutex); while(!conditionSatisfied) pthread_cond_wait(condition, mutex); pthread_mutex_unlock(mutex); while 信号应该通过以下方式完成: pthread_mutex_lock(mutex); conditionSatisfied = true; pthread_cond_signal(condition); pthread_mutex_unlock(mutex); 是的,必须在pthread_cond_wait之前锁定互斥量,但不必为pthread_cond_signal锁定。如果你回头看看你的代码,你会发现互斥量将被解锁两次,这是错误的标志......孩子也有可能在已被父母破坏的互斥量上调用解锁...... 顺便说一下,睡眠并不能保证父进程会先执行。为了确保这一点,您将需要……一个条件变量……

回答 3 投票 0

如何在 C++ 中将数据初始化保护到一个线程

使用现代 C++ 来拥有一些由一个线程初始化的共享内存的最佳方法是什么,第一个到达这一点,然后由多个线程读取。它需要尽可能轻巧……

回答 3 投票 0

为什么我的 pthread_cond_signal 没有立即唤醒阻塞的线程?

我正在编写一个多线程程序,其中辅助线程在满足条件后运行,即数据结构中存在一定数量的元素。 空白* gc_thread_handler(void *arg) {

回答 1 投票 0

C 互斥量被无限阻塞

所以我必须在 C 中实现一个队列,并使用一个系统来保护它并使其可以通过多个线程访问。 为此,我使用了生产者-消费者逻辑: #包括 #include ...

回答 1 投票 0

等到地图中的值在 Go 中可用

我有一个程序,我基本上有三种情况 - 为键设置值,如果它存在则获取值,或者等到给定键的值可用。我最初的想法——创建一个新类型

回答 3 投票 0

通过互斥体限制窗口进程?

我有一个 Windows 控制台应用程序,它正在查询数据库中的一个表。传递到控制台应用程序的参数包括一个连接字符串和一个在数据库的特定表中找到的值。这个缺点...

回答 0 投票 0

如何使用互斥锁防止多个实例但在不同用户上?

所以基本上我在我的 C# 应用程序中实现了一个互斥锁,它对一个用户非常有效,但是每当我将用户从 admin 更改为 User1 时,我都可以再次启动该应用程序。 使用...

回答 0 投票 0

在 Rust 中为线程同步创建多个互斥锁

我对 Rust 不熟悉。 我尝试编写的程序将在运行时确定 n 的值。 我希望程序具有以下行为:将创建 n 个线程,每个线程...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.