【问题标题】:How do I check if my multi-threading program works properly in C++?如何检查我的多线程程序在 C++ 中是否正常工作?
【发布时间】:2015-09-27 23:26:38
【问题描述】:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#define MAX_THREADS 1

#include <windows.h>
#include <iostream>

using namespace std;


DWORD WINAPI printNumbe(LPVOID);

// We need an array of Handles to threads
HANDLE hThreads[MAX_THREADS];
//...an array of thread id's
DWORD id[MAX_THREADS];
//And a waiter 
DWORD waiter;

DWORD WINAPI printNumber(LPVOID n)
{

int num = (int)n;

for (int i = 0; i <= num; i++)
{
    cout << "Hey there!" << endl;
}

return (DWORD)n;
}

//get ready, because here's where all the REAL magic happens
int main(int argc, char* argv[])
{

int number;

cout << "Please enter a number:" << endl;
cin >> number;

//here is where we call the CreateThread Win32 API Function that actually
//creates and begins execution of thread
//please read your help files for what each parameter does on
//your Operating system.

//Here's some basics:
//Parameter 0: Lookup, 1: Stack Size, 2: The function to run with this   thread, 3: Any parameter that you want to pass to thread
//function, 4: Lookup , 5: Once thread is created, an id is put in this variable passed in

hThreads[0] = CreateThread(NULL, 0, printNumber, (LPVOID)number, NULL, &id[0]);

//now that all three threads are created and running, we need to stop the primary thread
// which is this program itself - remember that once "main" returns, our program exits
//so that our threads have time to finish. To do this, we do what is called "Blocking"
//we're going to make main just stop and wait until all three threads are done
//this is done easily with the next line of code. please read the help file about the specific API call
//"WaitForMultipleObjects"

waiter = WaitForMultipleObjects(MAX_THREADS, hThreads, TRUE, INFINITE);

//after all three threads have finished their task, "main" resumes and we're now ready 
//to close the handles of the threads. This is just a bit of clean up work.
//Use the CloseHandle (API) function to do this.

for (int i = 0; i < MAX_THREADS; i++)
{
    CloseHandle(hThreads[i]);
}

system("PAUSE");
return 0;

}

大家好。最近我开始在我的大学学习操作系统课程。 我有机会了解线程和多线程。我的教授给了我一张幻灯片,里面有一些关于如何在 C++ 中启动多线程程序的示例代码。

所以我决定以他的代码为基础,并决定对其进行一些调整,以便我能更好地理解它。

请忽略我制作的所有这些 cmets(大多数 cmets 不适用于此程序,这些 cmets 基本上都在幻灯片中,我只是将其留在那里作为参考)。

所以我进行了调整以产生“嘿!” “x”次取决于用户输入“x”的数字。正如你所看到的,我让它在 printNumber 函数中打印(对不起这个名字,因为我的主要任务是打印素数,所以请原谅我)。

所以程序运行良好,并准确地产生“嘿那里!”多次。

但是问题来了。由于我的教授希望我使用多线程,我如何在 C++ 中验证自己程序是否以多线程运行?

这个程序看起来像是在打印“Hey there!”连续(就像在单个线程下)并且我无法判断是否已将多线程应用于程序。

请记住,我对这些语法并不熟悉,这是我第一次在 C++ 中使用 WINAPI,也是第一次进行线程编程。

非常感谢。

【问题讨论】:

  • printNumber中打印GetCurrentThreadId,每个线程都有一个单独的id。
  • 非常感谢。我已经包含在 main 和 printNumber 函数中,它们碰巧有不同的 Id,我相信这意味着它们在不同的线程上运行。

标签: c++ multithreading


【解决方案1】:

GetCurrentThreadId 将返回当前线程的唯一 ID,如果您在线程函数中打印此 ID,而 main 您应该会发现它返回不同的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 2012-09-01
    • 2014-09-27
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2019-05-17
    相关资源
    最近更新 更多