【问题标题】:Pthread Wrapper for FortranFortran 的 Pthread 包装器
【发布时间】:2017-04-17 15:38:36
【问题描述】:

我是 Fortran 和 C++ 的新手,致力于将两个用 Fortran 和 C++ 编写的程序结合起来。

我正在尝试创建一个 pthread(detached) 包装器并从我的 Fortran 子例程中调用它并将一个 cpp 函数传递给它。我通过这个链接Calling a subroutine in FORTRAN without blocking the main program写了一些代码。

当我执行它时,我得到如下运行时错误。

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:

我使用以下命令编译

gfortran-mp-4.7 -c pthread_mod.f90 
g++-mp-4.7 -c -std=c++11 pcmodel.cpp 
gfortran-mp-4.7 -c  mainFort.F
gfortran-mp-4.7 pthreads_module.o pcmodel.o mainFort.o -o test -lstdc++

这是我可以重现错误的最少代码。

Pthreads_interface.h

extern "C" void pthread_create_opaque(pthread_t *threadptr, void *(**procptr)(void *), int *comerr){
  //   creates a new thread using an opaque pointer to the pthread_t structure
   pthread_attr_t  attr;
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
   *comerr = pthread_create(threadptr, &attr, (*procptr), NULL);
}

pthreads_module.f90

module pthreads_module
implicit none
 interface

   subroutine pthread_create_opaque (threadptr, procptr, comerr) bind(C,name="pthread_create_opaque")
        USE ISO_C_BINDING
        type(c_ptr) :: threadptr
        type(c_funptr),value :: procptr
        integer(c_int),intent(out) :: comerr
   end subroutine

   subroutine PCModel () bind (c,name="PCModel_")
         USE ISO_C_BINDING
   end subroutine PCModel

  end interface
 end module pthreads_module

mainFort.F

program test
 call BCL00
end program test

  SUBROUTINE BCL00
  use pthreads_module
  USE ISO_C_BINDING
  implicit none
  type(c_ptr) :: threadptr
  integer :: comerr
  call pthread_create_opaque(threadptr,
 &          c_funloc(PCModel),comerr)

  END

其中PCModel是要由pthread 执行的C++ 函数。

pcmodel.cpp

 #include <iostream>
 #include "pthreads_interface.h"
 using namespace std;

 void PCModel(){
      cout<<"PCModel is called"<<endl;
 }

 extern "C" void PCModel_(){
  PCModel();
 }

理想情况下,我的 Fortran 和 C++ 代码应该并行运行,一旦 Fortran 代码触发线程启动 C++ 函数(PCModel)

如果有人可以检查代码并帮助我,那就太好了。

【问题讨论】:

  • 是的,它没有声明,但我是在我的。
  • 听起来不错,会做的。
  • @Vladimir F 试过了。它说call pthread_create_opaque(c_loc(threadptr), 1 Error: Type mismatch in argument 'threadptr' at (1); passed REAL(4) to TYPE(c_ptr)
  • @Vladimir F 当我尝试在subroutine bcl00 中将threadptr 声明为type(c_ptr) :: threadptr 它说Error: Derived type 'c_ptr' at (1) is being used before it is defined
  • @Vladimir F 添加了重现错误的代码。

标签: c++ fortran pthreads mixed-programming


【解决方案1】:

在 Pthreads_interface.h 中,我将 procptr 的传递方式从 (*procptr) 更改为 procptr

extern "C" void pthread_create_opaque(pthread_t *threadptr, void *(*procptr)(void *), int *comerr){
 //   creates a new thread using an opaque pointer to the pthread_t structure
 pthread_attr_t  attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 *comerr = pthread_create(threadptr, &attr, procptr, NULL);
}

现在它在没有Segmentation fault 的情况下运行,主程序继续运行而不等待线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多