【问题标题】:How to return few string from threads and concat it in C++/CLI如何从线程返回几个字符串并在 C++/CLI 中连接它
【发布时间】:2015-04-08 07:59:54
【问题描述】:

如何从线程返回几个字符串并将其链接到一个字符串? 我使用 CLI/C++,Windows 窗体中的线程。此代码应将消息从用户划分为 n(nThreads) 个文本,并在每个线程中对消息进行加密。 最后,它必须将所有结果合并为一个。

其实我是这样做的:

public: ref class ThreadExample
{
public:
    static String^ inputString;
    static String^ outputString;
    static array<String^>^ arrayOfThreads = gcnew array <String^>(nThreads);
    static int iterator;

    static void ThreadEncipher()
    {
        string input, output;
        MarshalString(inputString, input);
        output = CaesarCipher::encipher(input);
        outputString = gcnew String(output.c_str());
        arrayOfThreads[iterator] = outputString;
    }

我使用线程的函数:

        array<String^>^ ThreadEncipherFuncCpp(int nThreads, string str2){
            array<String^>^ arrayOfThreads = gcnew array <String^>(nThreads);
            string loopSubstring;
            messageLength = str2.length();
            int numberOfSubstring = messageLength / nThreads;
            int isModulo = messageLength % nThreads;
            array<Thread^>^ xThread = gcnew array < Thread^ >(nThreads);
            int j;
            //loop dividing text to threads
            for (int i = 0; i < nThreads; i++)
            {
                j = i;
                if (i == 0 && numberOfSubstring != 0)
                    loopSubstring = str2.substr(0, numberOfSubstring);
                else if ((i == nThreads - 1) && numberOfSubstring != 0){
                    if (isModulo != 0)
                        loopSubstring = str2.substr(numberOfSubstring*i, numberOfSubstring + isModulo);
                    else
                        loopSubstring = str2.substr(numberOfSubstring*i, numberOfSubstring);
                }
                else if (numberOfSubstring == 0){
                    loopSubstring = str2.substr(0, isModulo);
                    i = nThreads - 1;
                }
                else
                    loopSubstring = str2.substr(numberOfSubstring*i, numberOfSubstring);


                xThread[i] = gcnew Thread(gcnew ThreadStart(&ThreadExample::ThreadEncipher));
            }

            auto start = chrono::system_clock::now();
            for (int i = 0; i < nThreads; i++){
                ThreadExample::iterator = i;
                ThreadExample::inputString = gcnew String(loopSubstring.c_str());
                xThread[i]->Start();
            }
            for (int i = 0; i < nThreads; i++){
                xThread[i]->Join();
            }
            auto elapsed = chrono::system_clock::now() - start;
            long long milliseconds = chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
            cppTimer = milliseconds;
            arrayOfThreads = ThreadExample::arrayOfThreads;
            delete xThread;
            return arrayOfThreads;
        }

【问题讨论】:

  • 你的ThreadExample 类不应该有任何静态成员。 1) 用自己的ThreadExample 对象分叉每个线程。 2) 保留对那些ThreadExample 对象的引用。 3) 在所有线程完成后(在-&gt;Join() 之后),从那些ThreadExample 对象中提取数据。
  • 当您执行该代码时发生了什么?请不要让我们猜测。

标签: multithreading winforms c++-cli


【解决方案1】:

我在这里猜测一下,说程序运行没有错误,但你的输出是空白的。

输出为空白的原因是静态类初始化程序。这比您想象的要早执行:只要您以任何方式引用该类,静态初始化程序就会运行。因此,当您尝试执行ThreadExample::inputString = "Some example text. Some example text2."; 时,静态类初始化程序已经运行,并且您的线程数组已设置。

要解决此问题,请将代码从静态初始化程序中移出,并移至创建线程的方法中。

另外,关于 C++/CLI 的更一般说明:如果您正在尝试学习 C++,请不要使用 C++/CLI。 C++/CLI 与 C++ 相同。 C++/CLI 具有 C++ 的所有复杂性,C# 的所有复杂性,以及它自己的一些复杂性,以便很好地衡量。当需要将 .Net 代码连接到 C++ 代码时,应该使用它,而不是作为主要的开发语言。

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 2016-10-20
    • 2014-12-04
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2023-03-06
    • 2018-02-25
    相关资源
    最近更新 更多