C++ 在C+中执行多线程时出错+;

C++ 在C+中执行多线程时出错+;,c++,multithreading,winapi,C++,Multithreading,Winapi,我写这篇文章是为了写一个利用多线程的程序。我试图阅读有关多线程的内容,并能够使一个示例程序正常工作。然而,我遇到了一个小故障,我不确定到底出了什么问题。我正在发布示例程序,并将突出显示我遇到的问题 #include <Windows.h> #include <process.h> #include <iostream> #include <cassert> #include <ctime> #import "calc.dll" \ a

我写这篇文章是为了写一个利用多线程的程序。我试图阅读有关多线程的内容,并能够使一个示例程序正常工作。然而,我遇到了一个小故障,我不确定到底出了什么问题。我正在发布示例程序,并将突出显示我遇到的问题

#include <Windows.h>
#include <process.h>
#include <iostream>
#include <cassert>
#include <ctime>

#import "calc.dll" \
auto_rename no_namespace no_smart_pointers \
raw_native_types named_guids

using namespace std;

HANDLE  th_mutex;

struct  Arguments
{
    string  case_id;

    string  file;
};
//____________________________________

unsigned int __stdcall TestThread(void *args)
{
    int i   =   0,
        j   =   0;

    cout << "Inside thread" << endl;

    for (i = 0; i <= 10000; i++)
    {
        for (j = 0; j <= 100000; j++)
        {
        }

        if (i == 10000)
            cout << "The value of i inside the thread = " << i << endl;
    }

    return  EXIT_SUCCESS;
}
//____________________________________

unsigned int __stdcall Thread(void *args)
{
    CoInitialize(0);
    {
        Arguments   *input;

        input   =   (Arguments *) args;

        time_t  current_time;
        time(&current_time);
        cout << ctime(&current_time) << endl;

        IHeatExchangerNetwork   *hxNetwork  =   0;
        IDispatchPtr        theUnit     =   0;
        IHeatTransferUnit   *unit       =   0;

        VARIANT vAppend;

        _bstr_t filename,
            output;

        filename    =   input->file.c_str();
        filename    +=  input->case_id.c_str();
        filename    +=  ".dat";

        output      =   input->file.c_str();
        output      +=  input->case_id.c_str();
        output      +=  ".dbo";

        cout << filename << endl;
        cout << output << endl;

        HRESULT hr  =   CoCreateInstance(CLSID_HeatExchangerNetwork,
                             0,
                             CLSCTX_ALL,
                             DIID_IHeatExchangerNetwork,
                             reinterpret_cast<void**>(&hxNetwork));

        theUnit     =   hxNetwork->LoadHeatTransferUnit(filename, HxUnitTypeKey::HxUnitTypeCrossflow);

        hr  =   theUnit->QueryInterface(DIID_IHeatTransferUnit, reinterpret_cast<void**>(&unit));

        hxNetwork->Run(0, 0);

        vAppend.boolVal = false;
        unit->WriteDBOFile(output, OutputData, vAppend);

        time(&current_time);
        cout << ctime(&current_time) << endl;
    }

    CoUninitialize();

    return  EXIT_SUCCESS;
}
//____________________________________

int main()
{
    DWORD   retval;

    Arguments   args[2];

    args[0].case_id =   "1";
    args[0].file    =   "C:\\Documents and Settings\\User\\My Documents\\Test Cases\\1\\";

    args[1].case_id =   "2";
    args[1].file    =   "C:\\Documents and Settings\\User\\My Documents\\Test Cases\\2\\";

    th_mutex    =   CreateMutex(NULL, FALSE, NULL);

    if (th_mutex == NULL) 
    {
        printf("CreateMutex error: %d\n", GetLastError());
        return 1;
    }

//  ================================
//  Basic testing of calling threads
//  ================================
    HANDLE  hnd1;

    cout << "Before creating new thread" << endl;

    hnd1    =   (HANDLE) _beginthreadex(NULL, 0, &TestThread, NULL, 0, 0);

    cout << "After creating thread" << endl;

//  ====================================
//  Calling Calc routine through threads
//  ====================================
    HANDLE  hnd2,
        hnd3;

    hnd2    =   (HANDLE) _beginthreadex(NULL,
                                        0, 
                                        &Thread,
                                        (void *)&args[0],
                                        0,
                                        0);
    hnd3    =   (HANDLE) _beginthreadex(NULL,
                                        0,
                                        &Thread,
                                        (void *)&args[1],
                                        0,
                                        0);

    WaitForSingleObject(hnd1, INFINITE);
    WaitForSingleObject(hnd2, INFINITE);
    WaitForSingleObject(hnd3, INFINITE);

    GetExitCodeThread(hnd1, &retval); // Gets the return value from the function
    CloseHandle(hnd1);
    CloseHandle(hnd2);
    CloseHandle(hnd3);

    return EXIT_SUCCESS;
}
然后程序崩溃。我得到以下错误

Debug Error!
Program: ...MultiThreading.exe
R6010
-abort() has been called
(Press Retry to debug the application)
当我重试时,中断发生在生成的.tli文件中的以下位置,从_com_dispatch_method开始

#pragma implementation_key(723)
inline IDispatch * IHeatExchangerNetwork::LoadHeatTransferUnit ( BSTR filename, enum HxUnitTypeKey unitType ) {
    IDispatch * _result = 0;
    _com_dispatch_method(this, 0x20, DISPATCH_METHOD, VT_DISPATCH, (void*)&_result, 
        L"\x0008\x0003", filename, unitType);
    return _result;
}
filename变量的值为

C:\Documents and Settings\User\My Documents\Test Cases\2\2.dat
C:\Documents and Settings\User\My Documents\Test Cases\2\2.dbo
我尝试使用互斥并修改了线程函数

unsigned int __stdcall Thread(void *args)
{
    CoInitialize(0);
    {
        WaitForSingleObject(th_mutex, INFINITE);

        // Same content as original Thread function. 
        // Not writing it again to minimize space

        ReleaseMutex(th_mutex);
    }

    CoUninitialize();

    return  EXIT_SUCCESS;
}
//____________________________________
这次程序在.tli文件中的以下位置\u com\u dispatch\u方法崩溃

#pragma implementation_key(966)
inline long IHeatTransferUnit::WriteDBOFile ( BSTR filename, short io, const VARIANT & vAppend ) {
    long _result = 0;
    _com_dispatch_method(this, 0x32, DISPATCH_METHOD, VT_I4, (void*)&_result, 
        L"\x0008\x0002\x080c", filename, io, &vAppend);
    return _result;
}
通过使用互斥锁,错误由具有句柄hnd3的线程生成。变量filename的值为

C:\Documents and Settings\User\My Documents\Test Cases\2\2.dat
C:\Documents and Settings\User\My Documents\Test Cases\2\2.dbo
同样,通过使用互斥锁,我能够越过先前的错误位置

我不确定我是否真的需要一个互斥,如下调用

hxNetwork->Run(0, 0);
处理两个彼此没有任何关联的不同文件。这也是程序中最耗时的部分,因此我想同时运行两个案例

通过像我那样使用互斥,它运行第一种情况,然后运行第二种情况

此外,我无法控制calc.dll。它是第三方软件,我不确定是否支持多线程

因此,我想知道我需要做什么,以使运行继续进行,也能够输出2个文件


提前感谢

您需要在
hxNetwork->Run(0,0)上使用互斥锁
如果此调用以某种方式在内部修改
hxNetwork
对象状态,并且此对象不允许这种情况(例如,它的
Run
方法没有自己的锁定)。此外,如果多个线程同时更改此指针变量本身,您将需要互斥锁。就calc.dll而言,您将希望联系他们的开发人员,以便了解当您的应用程序运行到他们的组件时,您在应用程序中需要如何/什么样的同步。