Visual C++ 线程简单示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13062895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:55:37  来源:igfitidea点击:

Visual C++ Threads Simple Example

c++multithreadingvisual-c++beginthread

提问by TechyHarry

I am trying to create a basic thread from main by passing a function to _beginthread. But My output is not getting completed.

我试图通过将函数传递给 _beginthread 来从 main 创建一个基本线程。但是我的输出没有完成。

I am getting the following output:

我得到以下输出:

Starting thread
48
Main ends
I

Could someone please clarify what is wrong in the following code?

有人可以澄清以下代码中有什么问题吗?

#include <iostream>
#include <process.h>
using namespace std;

void test(void *param)
{
    cout << "In thread function" << endl;
    Sleep(1000); // sleep for 1 second
    cout << "Thread function ends" << endl;
    _endthread();
}


int main()
{
    cout << "Starting thread" << endl;
    cout << _beginthread(test,0,NULL);
    cout << "Main ends" << endl;
    return 0;
}

回答by kuperspb

Because return from the main will stop any threads in your application. You need to wait untill thread will stop. Simplest solution with global var - very bad example to be honest. You need to use wait functions on thread's handle.

因为从 main 返回将停止应用程序中的任何线程。您需要等到线程停止。最简单的全局变量解决方案 - 老实说,这是一个非常糟糕的例子。您需要在线程的句柄上使用等待函数。

#include <iostream>
#include <process.h>
using namespace std;

bool threadFinished = false;

void test(void *param)
{
    cout << "In thread function" << endl;
    Sleep(1000); // sleep for 1 second
    cout << "Thread function ends" << endl;
    threadFinished = true;
    _endthread();
}


int main()
{
    cout << "Starting thread" << endl;

    cout << _beginthread(test,0,NULL);
    while(!threadFinished)
    {
        Sleep(10);
    }
    cout << "Main ends" << endl;
    return 0;
}

How to use Wait functions:

如何使用等待功能:

HANDLE hThread;
hThread = (HANDLE)_beginthread( test, 0, NULL);
WaitForSingleObject( hThread, INFINITE );

回答by rkosegi

You need to wait for thread to end using some synchronization primitives, or your program will call ExitProcess before thread finished his execution.

您需要使用一些同步原语等待线程结束,否则您的程序将在线程完成执行之前调用 ExitProcess。

You may read about synchronizationfirst to understand how to write multithreaded application.In your case you need single object wait functions.

您可以先阅读同步以了解如何编写多线程应用程序。在您的情况下,您需要单对象等待函数

See msdn example: http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx

请参阅 msdn 示例:http: //msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx

so, you main function should look something like this :

所以,你的主要功能应该是这样的:

int main()
{
    HANDLE hThread;
    cout << "Starting thread" << endl;
    cout << (hThread = (HANDLE)_beginthread(test,0,NULL));
    WaitForSingleObject( hThread, INFINITE );
    cout << "Main ends" << endl;
    return 0;
}

回答by Vivek Ananthan

You can use CreateThreadmethod.

您可以使用CreateThread方法。

Simple example of multithreading.

多线程的简单示例。

#include <Windows.h>
#include <iostream>

using namespace std; 

DWORD WINAPI thread1(__in LPVOID lpParameter) {
    while (1) {
        std::cout << " From Thread 1 \n";
        Sleep(3000);
    }

}

DWORD WINAPI thread2(__in LPVOID lpParameter) {
    while (1) {
        std::cout << " From thread 2\n"; 
        Sleep(1000);

    }
}

int main()
{

    DWORD threadID1, threadID2; 
    HANDLE h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread1, 0, 0,&threadID1);
    HANDLE h2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread2, 0, 0, &threadID2);

    getchar();
    return 0;
}

where thread 1 prints for every 3 secs and thread2 prints for every sec.

其中线程 1 每 3 秒打印一次,线程 2 每秒打印一次。

You can allocate stack size if your process is bigger.

如果您的进程更大,您可以分配堆栈大小。

StackSizeexplanation

堆栈大小说明