[英]Simple threads example for Dev C++ using windows.h
我是第一次嘗試學習多線程,但是我看到的大多數示例都有一個thread.h
頭文件。 在Dev C ++中不存在。
我找到了這篇文章,說您可以使用windows.h
遺憾的是,它沒有提供任何示例程序。 有人可以提供一個示例程序,該程序在windows.h
或Dev C ++中廣泛使用的任何其他頭文件中使用線程函數嗎? 謝謝
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 main\n");
pthread_join(t2,NULL);
return 0;
}
那么windows.h
有一個名為CreateThread().
的函數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
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.