【问题标题】:The right way for multithreading in c++ with using of boost library使用boost库在c ++中进行多线程处理的正确方法
【发布时间】:2016-03-21 19:31:36
【问题描述】:

我有一个 C++ dll,我使用 boost library 来实现多线程,然后我想在 C# 程序中使用这个 dll。
我做到了,我的程序按预期运行,我的系统没有任何问题。 当我使用Advanced Installer 创建安装程序并将其作为新程序安装在我的系统中时(我已经在其上开发了应用程序),一切都很好,没有任何问题。但是当我在其他系统上安装这个程序并运行它时,一开始程序会正常运行,但是当它想创建线程并运行它们时,程序就会停止。
我在 C++ dll 中的部分代码如下:

struct ThreadParams
{
    int thetaStart,
        thetaEnd,
        rStart;
    vector<string> files;
}thParams;
const int NUM_OF_THREADS = 5;
extern "C"
{
    __declspec(dllexport) int __stdcall Start(const char *path)
    {
        thParams.files = listFilesInDirectory(path);
        int step = thParams.files.size()/NUM_OF_THREADS;
        thParams.rStart = 1;
        thParams.thetaStart = 0;
        thParams.thetaEnd = 360;

        boost::thread_group tgroup;
        FILE *output_text;
        char *buffer = new char[128];
        for (int i = 0; i < NUM_OF_THREADS; i++)
        {   
            sprintf(buffer,"out%d.txt",i+1);
            output_text = fopen(buffer,"wt");

            int start  = i*step;                
            int end  = (thParams.files.size() - (start+step)) >= step ? (start+step) : thParams.files.size();
            tgroup.create_thread(boost::bind(ThreadProcess,start,end,output_text,i+1));
        }
        tgroup.join_all();
        _fcloseall();
        delete buffer;
        buffer = NULL;
    }
}
void ThreadProcess(int start, int end, FILE *out, int threadID)
{
    for(int i = start; i < end; ++i)
    {
        fprintf(out,"%s\n",thParams.files[i].c_str());
    }
}

谁能帮我解决这个问题,好吗?
提前致谢。

【问题讨论】:

标签: c# c++ multithreading boost-thread concurrent-programming


【解决方案1】:

Boost.thread 是为数不多的不是仅标头的 boost 库之一,它们需要一个已编译的库。很可能,您将代码与 boost.thread 的动态版本相关联。

您的解决方案:

  1. 与 boost.thread 的静态版本链接
  2. 在您的发行版中包含动态库。

【讨论】:

  • 感谢@SergeyA 的回答。我所做的是在项目“属性页”-> 配置属性-> 附加目录和“附加包含目录”和“附加库目录”,并且我已将这些文件添加到“C/C++ -> 常规 -> 包含目录”和“链接器 -> 常规 -> 库目录”中。请你解释一下我还应该做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多