0

我正在写一个将利用多线程的程序。我尝试阅读有关多线程的信息,并且能够使示例程序正常工作。但是我遇到了一个小故障,我不确定出了什么问题。我正在发布示例程序,并将突出显示我遇到问题的地方。

#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;
}

我面临的问题是当我执行程序时,我得到以下输出

Before creating new thread
After creating threadInside thread

Thu Aug 09 10:56:58 2012
Thu Aug 09 10:56:58 2012


C:\Documents and Settings\User\My Documents\Test Cases\1\1.datC:\Documents and Settings\User
\My Documents\Test Cases\2\2.dat

C:\Documents and Settings\kcomandur\My Documents\Test Cases\1\1.dboC:\Documents and Settings\User
\My Documents\Test Cases\2\2.dbo

The value of i inside the thread = 10000
Thu Aug 09 10:57:08 2012

然后程序崩溃。我收到以下错误

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;
}

文件名变量具有值

C:\Documents and Settings\User\My Documents\Test Cases\2\2.dat

我尝试使用互斥锁并修改了线程函数

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 文件中的以下位置 _com_dispatch_method 崩溃

#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;
}

通过使用互斥锁,错误是由具有 HANDLE hnd3 的线程生成的。变量文件名具有值

C:\Documents and Settings\User\My Documents\Test Cases\2\2.dbo

同样通过使用互斥体,我能够越过以前的错误位置。

我不确定我是否真的需要一个互斥锁作为以下调用

hxNetwork->Run(0, 0);

适用于彼此毫无关联的两个不同文件。这也是程序中最耗时的部分,因此我想同时运行 2 个案例。

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

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

所以我想知道我需要做什么才能让两个运行都进行并能够输出 2 个文件

提前致谢

4

0 回答 0