pthreads 相关问题

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

使用 pthread 的并发哈希映射的 C++ 实现

get方法中对pthread_mutex_lock的调用永远阻塞(在使用map的场景下,构造函数和然后对put的几次调用正确执行)。可能是什么原因...

回答 0 投票 0

pthread_cond_wait()如何与mutex解耦?

我一直在思考条件变量背后的逻辑,并且对与之相关的最常见问题感到满意。 基本上,如果我们做类似的事情: mutex.lock() whi...

回答 1 投票 0

C - 多处理线程实现 - 指针问题

我只需要编写一个代码来计算一个数组的一半的局部最大值..但是有一些问题(?) 我看到分段错误问题 #包括 #包括 我只需要写一个代码来计算一个数组的一半的局部最大值..但是有一些问题与 ponters (?) 我看到segmentation fault问题 #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <time.h> #include <stdlib.h> #define n 10 struct args { int index; int a[n]; }; void * max_locale(void * parameters) { struct args * pp = parameters; int max = pp->a[0]; int finalIndex = pp->index + n/2; for(int i=pp->index; i<finalIndex; i++) { if((pp->a[i]) > (pp->a[i+1])) { max = (pp->a[i]); } } return (void *)&max; } int main() { srand(time(NULL)); int array[n]; pthread_t thread1, thread2; struct args thread1_args, thread2_args; printf("-beginning of program-\n"); for(int i=0; i<n; i++) { array[i] = rand() % 101; printf("%d ", array[i]); } for(int i=0; i<n; i++) { thread1_args.a[i] = array[i]; thread2_args.a[i] = array[i]; } int * thread1_returnValue; int * thread2_returnValue; thread2_args.index = 0; thread2_args.index = n/2; printf("\n Find MAX... \n"); pthread_create(&thread1, NULL, &max_locale, &thread1_args); pthread_create(&thread2, NULL, &max_locale, &thread2_args); pthread_join(thread1, (void **) & thread1_returnValue); pthread_join(thread2, (void **) & thread2_returnValue); printf("Max 1:%d Max 2:%d \n", *thread1_returnValue, *thread2_returnValue); return 0; } 这是输出: -beginning of program- 98 28 73 5 10 100 23 63 25 60 Find MAX... zsh: segmentation fault ./15-4-23 我试图用 void * 函数返回 int, 如果我可以在pthred_create函数中传递不同的参数,那就容易多了

回答 0 投票 0

VS code 可以在 VirtualBox VM 中编译和运行 C 程序吗?

问题: 我必须制作使用 pthreads 的程序。为此,我在 VirtualBox 上有一个 Ubuntu VM,所以有人会说我已经准备好了。但我想知道我是否可以在仍然使用 POSIX 库的同时...

回答 1 投票 0

C:用数字 0 到 n -1 索引 pthreads

这是行不通的。它导致编号不是 0 索引的。 #define MY_THREAD_COUNT 6 void *runner(void *param) { int id = *(int*)param; printf("线程 %d “, ID); } ...

回答 0 投票 0

为什么每次我使用“cont”命令时,在 gdb 中运行时一个 CTRL-Z 会一直发出 SIGTSTP 信号?

我正在 Ubuntu Linux 上开发一个项目,当我使用 GDB 调试应用程序并按 CTRL + Z 中断时,我得到了预期的 SIGTSTP 和 GDB 中断。 但是当我在那之后使用 cont 时,我仍然......

回答 3 投票 0

在 c 中将多个线程加入一个线程

乙 基本上主线程创建 4 个线程 1 ,2 ,3 ,4 和 1、2、3 必须等待 4 结束,而 main 等待线程 1 2 3。 代码如下: #include #include 乙 基本上主线程创建了 4 个线程 1 ,2 ,3 ,4 和 1、2、3 必须等待 4 结束,而 main 等待线程 1 2 3。 代码如下: #include<stdio.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h> pthread_t id[4]; void *func1(void *arg) { printf("i am the %ith thread\n",(int)arg); pthread_join(id[3],NULL); printf("end of %ith thread\n",(int)arg); pthread_exit(NULL); } void *func2(void *arg) { printf("i am the 4th thread\n"); sleep(5); printf("end of 4th thread\n"); pthread_exit(NULL); } int main() { printf("start of main thread\n"); pthread_create(&id[3],NULL,func2,NULL); for(int i=0;i<3;i++) { pthread_create(&id[i],NULL,func1,(void *)i); } for(int i=0;i<3;i++) { pthread_join(id[i],NULL); } printf("end of main\n"); return 0; } 跑步时我得到这样的东西:` 主线程开始 我是第0个线程 我是第 4 个线程 我是第二个线程 我是第一个线程 4线程结束 第0个线程结束 请原谅这些可怕的信息,因为我匆忙将它们从法语翻译过来。 理论上线程 1 2 3 应该等待 4 然后自己完成,但每次尝试只有一个线程在 4 之后完成,然后整个执行被无限期地阻塞。 我尝试确保第 4 个线程总是首先从 pthread_attr_t 开始,并设置第 4 个高的调度优先级,但不幸的是,这没有用。 我尝试查找有关 pthread_join 的一些详细信息,如果将多个线程加入一个线程是不安全的或不允许的,但我没有找到任何建议。 任何帮助将不胜感激提前谢谢你。 来自pthread_join()手册页: 如果多个线程同时尝试加入同一个线程,结果是不确定的。 如果需要在多个线程中等待线程终止,创建相应的标志包装到条件变量。在退出之前终止线程或在另一个线程中加入线程后广播条件变量。 补充说明: 始终检查系统函数的返回值。 pthread_exit(NULL) 在线程函数的末尾是多余的。只需返回 NULL。 从 int 到 void* 的转换可能会溢出,编译器不喜欢这样。我们可以使用 intptr_t 代替 int 但是在 intptr_t 中没有 printf() 的转换说明符,因此我们无论如何都应该转换。我们可以使用 intptr_t 的中间转换来让编译器开心。 程序可能是这样的: #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <string.h> static pthread_t id[4]; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int terminated = 0; static void *func1(void *arg) { printf("i am the %ith thread\n",(int)(intptr_t)arg); // Wait until 4th thread termination flag is set pthread_mutex_lock(&mtx); while (!terminated) { pthread_cond_wait(&cond, &mtx); } pthread_mutex_unlock(&mtx); printf("end of %ith thread\n",(int)(intptr_t)arg); return NULL; } static void *func2(void *) { printf("i am the 4th thread\n"); sleep(5); printf("end of 4th thread\n"); // Set termination flag and broadcast state change pthread_mutex_lock(&mtx); terminated = 1; pthread_mutex_unlock(&mtx); pthread_cond_broadcast(&cond); return NULL; } int main() { printf("start of main thread\n"); int ret; ret = pthread_create(&id[3],NULL,func2,NULL); if (ret) { fprintf(stderr, "pthread_create(id[3]): %s\n", strerror(ret)); return 1; } for(int i=0;i<3;i++) { ret = pthread_create(&id[i],NULL,func1,(void *)(intptr_t)i); if (ret) { fprintf(stderr, "pthread_create(id[%i]): %s\n", i, strerror(ret)); return 1; } } // Wait for all 4 threads for(int i=0;i<4;i++) { int ret = pthread_join(id[i],NULL); if (ret != 0) { fprintf(stderr, "pthread_join(id[%i]): %s\n", i, strerror(ret)); } } printf("end of main\n"); return 0; } UPD:有更简单的方法:将 pthread_join() 调用包装到由互斥锁保护的临界区: static pthread_t id[4]; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static int terminated = 0; static void *func1(void *arg) { printf("i am the %ith thread\n",(int)(intptr_t)arg); // Wait until 4th thread termination flag is set pthread_mutex_lock(&mtx); if (!terminated) { int ret = pthread_join(id[3],NULL); if (ret != 0) { fprintf(stderr, "pthread_join(id[3]): %s\n", strerror(ret)); } terminated = 1; } pthread_mutex_unlock(&mtx); printf("end of %ith thread\n",(int)(intptr_t)arg); return NULL; } static void *func2(void *) { printf("i am the 4th thread\n"); sleep(5); printf("end of 4th thread\n"); return NULL; } 在这种情况下,您不需要在main()中加入第4个线程: for(int i=0;i<3;i++) { int ret = pthread_join(id[i],NULL); ...

回答 1 投票 0

为什么MinGW-w64浮点精度依赖于winpthreads版本?

我使用 MinGW-w64 g++ 编译器 10.2 和 10.3。我自己使用 https://github.com/niXman/mingw-builds 构建了两者。 Windows 上的 g++ 有一个奇怪的地方:应用程序的主线程将...

回答 3 投票 0

无法将“long int”转换为“void(*)()”__depend in std_threads.h [关闭]

我正在尝试使用命令 cmake 编译 C++ 代码库。和做。但是,不断出现以下错误: 在此处输入图像描述 我对c++的库不太熟悉...

回答 0 投票 0

为什么 c++ 线程是可移动的而不是可复制的?

正如题名所说,为什么C++线程(std::thread和pthread)是可移动的而不是可复制的?如果我们让它可复制,会有什么后果?

回答 2 投票 0

在退出线程之前在本地指针上调用 free() 会导致无效读取

我正在从事一个编程项目,以更好地理解线程之间的同步。我正在逐行解析文本文件(使用多个线程)以读取网站名称(stackoverflo ...

回答 0 投票 0

获取逻辑线程ID号

我想将一个简单的一维数组拆分成多个块,每个线程都在一个块上运行。代码看起来像 void* thread_func(void* arg) { 无符号 start_index = *((int*) arg); 无符号

回答 1 投票 0

使用结构作为线程参数调用 pthread_create 时出现额外字符

我观察到,当我将一个结构作为参数传递给线程时,该结构的字符串成员在线程内长了几个字符。 所以基本上,在 main 中,s-dev 的长度为 12 个字符。一个...

回答 1 投票 0

停止所有 C++ 线程

我有一个调用 std::getline(std::cin,input) 的线程和另一个每 x 分钟唤醒一次并检查状态的线程。如果状态为真,我的整个 C++ 应用程序需要终止/关闭...

回答 1 投票 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

将 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

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