pthreads 相关问题

Pthreads(POSIX Threads)是一个标准化的基于C的API,用于创建和操作线程。它目前由POSIX.1-2008(IEEE Std 1003.1,2013 Edition / The Open Group Base Specifications Issue 7)定义。

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

我需要一些帮助来理解如何在 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

将 std::mutex 放在共享内存中是个好主意吗?

我想设计一个共享内存来在多个进程之间传递数据。 在多线程中,我需要 pthread_mutex_t 来投影临界区。 所以我想在...中分配一个 pthread_mutex_t

回答 0 投票 0

线程作业未完全完成

直到最近(Vulkan)我才真正对多线程做过很多事情,所以我不太精通它。 我这里有一个小测试程序: https://github.com/seishuku/ThreadPool_TEST 它应该只是...

回答 0 投票 0

无法在正确的地方加入线程

在我的游戏中有两个(与这个问题相关的)线程(都是可连接的),一个是玩家,一个是射击。创建线程播放器时,会调用启动例程函数,其中...

回答 0 投票 0

pthread 在线程函数出现障碍后停止连接

我在 pthread Tutorial - Peter Chapin 的 pthread 教程之后进一步研究 pthread barrier,3.2 Barriers pg 11 经历了在线程函数中使用两个障碍,f ...

回答 3 投票 0

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

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

回答 1 投票 0

大量请求后线程池后端出现管道错误

我的问题与这篇文章类似,但我听从了建议,但我仍然遇到堆栈溢出和管道损坏错误 Getting Segmentation Fault in C when using sleep with Pthreads 我正在修改一个...

回答 1 投票 0

将多个参数传递给 pthread

对于 pthreads 库,使用结构或位字段将参数传递给线程会更好吗?在将参数传递给函数时,我想在我的程序中使用更少的内存。 #include ...

回答 1 投票 0

如何在不冻结的情况下显示cv::imshow("Live", frame) 和cv::imshow("ROI", frame_roi)?

我的目标是显示cv::imshow("Live") 和cv::imshow("ROI")。问题是一段时间后窗口没有更新。虽然主函数循环仍然起作用。 我的

回答 0 投票 0

如何在使用 cmake 编译时正确获取 -pthread 标志?

关于如何使用 cmake 执行线程的问题已经得到解答,但没有一个有示例,而且我尝试的方法不起作用。 因此,我将带有目录和测试的 cmake 项目进行了修改

回答 0 投票 0

为什么 mvprintw 只能在 getch 之后工作?

mvprintw/mvaddch (pdcurses) 仅在 getch() 之后打印文本(即使它没有以任何方式连接并且 getch 在分离线程中)。 // MAIN WHILE(对 debag 的强烈简化) 做 { ...

回答 0 投票 0

函数如何在 c 中返回左值?

我读了 The Linux Programming Interface 29-2 Threads 和 errno 下一个: 在 Linux 上,线程特定的 errno 以与大多数其他 UNIX 实现类似的方式实现:errno 被定义为...

回答 1 投票 0

在谷歌基准测试中生成/加入线程时的核心转储

我在尝试生成然后加入 google 基准测试中的线程时得到核心转储。这与我尝试创建的线程数无关。 重现问题的代码: 无效

回答 0 投票 0

我怎样才能使用并行归约方法来组合 c 中的部分和

我必须在 C 中使用并行归约方法进行部分求和,但我对此一无所知。所以,我需要社区的指导来实现这一目标。 我需要实现的目标:例如,

回答 2 投票 0

编译成功,但没有控制台输出

我正在尝试在 Windows 上使用 LLVM。 我已经安装了 llvm-mingw(https://github.com/mstorsjo/llvm-mingw/releases/tag/20220906),它就像一个魅力。 一切正常,但是 pthread 和 openmp....

回答 0 投票 0

如何在CLion的C项目中使用pthread头文件,在windows中使用MinGW。

我想在C项目中加入#include 在我使用CLion的项目中,我不能直接使用它。有什么具体的方法可以将pthread加入到C项目中吗?

回答 1 投票 0

在Linux中可以从自身内部更改线程的名称吗?[重复]

从这个答案中,我可以看到至少在MacOS上是可以做到的。我想也许这样可以做到:pthread_setname_np(pthread_self(), "NEW_NAME"); 这并不可行,因为pthread_self()返回...

回答 1 投票 -1

将Pthread转换为进程fork()

通过下面这段基本的pthread代码,用什么方法将pthread_create转换为fork(),并实现类似的结果。#include #include #include

回答 1 投票 0

C语言中的Pthreads,第二个线程无法执行。

我有这样一段代码,从一个文件中读入两个int数字。并将其存储在一个缓冲区[]中,以便在第二个函数中取用。我不知道我在第一个函数中的停止条件是否 ...

回答 1 投票 0

Pthread [closed]

Sorry for my English. I'm use QT Creator. I'm just starting to study threads) And this is my first serious problem. I do not understand why I cannot get the exact value of my string from a thread ...

回答 1 投票 1

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