MPI_发送到运行在同一进程上的多个POSIX线程
MPI_Send to multiple POSIX threads running on the same process
我在进程#0上启动n个POSIX线程,以侦听来自进程#0…#n的传入调用。然而,我的代码不起作用,而是得到了一个segfault。我认为问题可能是因为缓冲区重叠。我是C++新手。你能提出一个解决方案吗?
void *listener(void *arg) {
...
int status_switch;
while (true) {
MPI_Recv(&status_switch, 1, MPI_INT, fd->id, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
...
}
}
int main(int argc, char * argv[])
{
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if (world_rank == root)
{
int nthreads = world_size;
threads=(pthread_t *)malloc(nthreads*sizeof(threads));
fd=(struct_t *)malloc(sizeof(struct_t)*nthreads);
//Start listeners for each node on node0
for (int i = 0; i < world_size; i++) {
fd[i].id=i;
pthread_create(&threads[i], NULL, listener, (void *)(fd+i) );
}
}
int status_switch;
status_switch = 0;
MPI_Send(&status_switch, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
...
}
我不熟悉MPI,但您似乎在这里分配了错误的空间:
threads = (pthread_t *) malloc(nthreads*sizeof(threads));
你的代码很可能因此而出错。
如果threads
是pthread_t *
,则需要为nthreads*sizeof(*threads)
分配足够的空间来容纳pthread_t
的nthreads
实例。
此外,您不应该强制转换malloc
的结果。
相关文章:
- 异常属于C++中的线程还是进程
- 将更高的优先级设置为 boost::asio 线程处理进程
- 从不同进程中的另一个线程挂起/恢复线程或进程
- 多线程:线程或进程.h - C++
- BOOST线程:线程还是进程
- 将进程的执行从线程1转移到线程2
- std::async 如果线程是从 DLL 创建的,则会阻止进程退出?
- 优化吞吐量:多线程与多进程
- 多个线程/进程是否可以在不同步的情况下同时从/写入文件的非重叠区域?
- 使用 CommonAPI 编写服务器时C++服务器进程中创建了多少个线程?
- 睡眠线程或进程的指令是否在线程/进程休眠时继续执行?
- 多线程中的 std::basicstring 的运行时分段错误C++点燃进程
- 在 Linux 中从单独的单线程进程生成多线程进程时出现问题
- 当其中一个线程在 C++ 中使用 pthread 被杀死时,我如何保持进程的存活
- 异步是否总是在C++中使用另一个线程/内核/进程
- boost消息队列线程安全和进程安全吗?
- 当进程结束时,处于Sleep()中间的线程会发生什么
- 确定进程的输入线程 ID
- rusage的进程/线程的Cpu时间不可能
- 获取c++ Builder中执行进程/线程的ID