Dev c++使用windows.h的简单线程示例

Simple threads example for Dev C++ using windows.h

本文关键字:简单 单线程 c++ 使用 windows Dev      更新时间:2023-10-16

我第一次尝试学习多线程,但我看到的大多数例子都有一个thread.h头文件。Dev c++中没有。

我找到了这篇文章,它说您可以使用windows.h来完成它。遗憾的是它没有提供任何示例程序。有人可以提供一个例子程序,使用线程函数在windows.h或任何其他广泛使用的头文件,并存在于Dev c++ ?Thank You

Best option is to do threading in c or c++ use pthread.h header file
example work on gcc or dev c++
#include <stdio.h>
#include <stdlib.h>
#include<pthread.h>
int i=0;
 void* fun()
{
    for(i=0;i<100;i++)
    printf("nThe thread 1 is running");
}
void* van()
{
    for(i=0;i<100;i++)
    printf("nthread 2 is running ");
}
int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,fun,NULL);
    pthread_create(&t2,NULL,van,NULL);
    printf("nI'm in mainn");
    pthread_join(t2,NULL);
    return 0;
}

windows.h有一个叫做CreateThread().的函数
它的签名和其他信息可以在这里看到。

你需要一个回调函数,它将在你启动线程时开始执行。NULL可以传递给第一个lpThreadAttributes参数。

HANDLE WINAPI CreateThread(
  _In_opt_   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_       SIZE_T dwStackSize,
  _In_       LPTHREAD_START_ROUTINE lpStartAddress,
  _In_opt_   LPVOID lpParameter,
  _In_       DWORD dwCreationFlags,
  _Out_opt_  LPDWORD lpThreadId
);