多线程编程中使用的同步原语,用于等待条件为真。
我需要一些帮助来理解如何在 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锁定。如果你回头看看你的代码,你会发现互斥量将被解锁两次,这是错误的标志......孩子也有可能在已被父母破坏的互斥量上调用解锁...... 顺便说一下,睡眠并不能保证父进程会先执行。为了确保这一点,您将需要……一个条件变量……
为什么我的 pthread_cond_signal 没有立即唤醒阻塞的线程?
我正在编写一个多线程程序,其中辅助线程在满足条件后运行,即数据结构中存在一定数量的元素。 空白* gc_thread_handler(void *arg) {
在同一个 std::condition_variable_any::wait_for 和 std::std::stop_token 上等待多个线程时死锁
在我正在编写的应用程序中,我有一个线程模型,它被简化如下: 生成器 jthread (m_WorkerGenerator) 正在启动异步任务。 多个异步任务工作直到...
C++ 11 - condition_variable - wait_until不能如期工作
我已经实现了一个示例程序来理解在C++11中wait_for和wait_until是如何工作的。 代码 - #include #include #include #include ...
我有一个有自己线程的类 player: player.cpp int soh::player::instance_count{ 0 }; int soh::player::current_player{ -1 }; std::mutex soh::player::mutex; std::condition_variable soh::player::...
我有一个多线程项目,我应该创建一个模拟的酒店。我有一个接待员结构,他们的工作是不断地寻找是否有空闲的房间(在 ...
C++11并发; condition_variable; fizz, buzz, fizzbuzz; STUCK
我找不到任何bug,程序不显示任何输出,但是如果我在while之后引入一个cout和一些print(),我看到一些输出,信号是否被遗漏了?我有...
你好,我制作了一个通信协议软件,我遇到了一个问题。问题是当我使用线程进行recv函数时,我从socket得到的缓冲区不见了。我试着用...
所以我正在编写一个具有1个主线程(我的main)的程序,该程序创建了一个主线程。第二个应该等待一个信号然后运行。主要等待当然会加入。我在理解时遇到问题...
我想实施一个交通信号灯(一个盒子)。我看到SO中有很多问题,但我觉得它们不是完整的代码。以下是完整的源代码。我正在为...
我在C中进行了有关线程,锁和条件变量的练习。我需要编写一个程序来获取数据,将其转换为链表,并开始3个线程,每个线程为...中的每个节点计算结果。]] >
我不太清楚这段代码要做什么。我对此代码有一些疑问。 watch_count线程打印的计数变量值是哪个?这是计数变量值...
因此,我刚刚发现,如果您不持有c ++ 11的锁,则发信号通知条件变量是合法的。似乎打开了一些讨厌的竞赛条件的门:std :: mutex m_mutex; std :: ...
[我在两种不同情况下使用互斥锁:-第一个示例:我将互斥锁与unique_lock一起使用,以确保线程不会同时访问同一资源-第二个示例:我扩展了第一个...
根据https://golang.org/pkg/sync/#Cond.Wait,必须将Wait()调用包装在for循环中,因为在第一次恢复时,c.L据称未锁定。这与句子相矛盾...
std :: condition_variable :: wait_until的工作方式
为什么此代码运行非常快? int main(){std :: condition_variable cv; std :: mutex mtx; std :: unique_lock lock(mtx); cv.wait_until(lock,std :: chrono :: system_clock :: ...
我正在尝试使用pthread,互斥锁和条件变量在C语言中实现餐厅哲学家的问题。它需要一个命令行参数来指定程序应运行多长时间。...
在pthread库中有取消点的概念。可能会阻止执行更长时间(或等待某些资源...)的大多数系统函数可以由pthread中止...