如何在C for Windows中创建多线程? [英] How can i create a multithread in C for windows?
问题描述
我不知道如何在C中创建线程,我看到了有关pthread.h库的评论,但后来我听说它仅适用于Linux OS,我有一个计时器功能,我想用该功能,但是如果有人可以为我提供带有线程的简单代码,或者告诉我需要放置什么内容以及函数的参数,我既不知道我需要使用的库也不是编写代码的语法.
I dont know how can I make threads in C, i saw a review about pthread.h library but then i heard that its only for Linux OS, i have a function that its a timer, i want to created a thread with that function but i dont know either the library i need to use and syntax's to write the code, if someone could provide me a simple code with threads, or tell me what things i need to put and the parameter of the functions.
这里是我创建的功能,它在用户申请的特定时间进行倒计时: 我需要使用该函数创建线程.
Here its the function i create that countdown the specific time the user apply: i need to make a thread with that function.
功能(倒数):
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void countdown(int second)
{
int secs = 1;
time_t unix;
struct tm * timeinfo;
time(&unix);
timeinfo = localtime(&unix);
int t1 = timeinfo->tm_sec;
int t2 = timeinfo->tm_sec;
int i = 0;
while(1 == 1)
{
time(&unix);
timeinfo = localtime(&unix);
if((t1 + i) == timeinfo->tm_sec)
{
system("cls");
printf("Time left %d\n", timeinfo->tm_sec - t2 - second);
i++;
}
if(timeinfo->tm_sec >= (t1 + second))
{
system("cls");
puts("Your time its done");
break;
}
}
}
int main()
{
int limit;
printf("How much time would you like (In secs): ");
scanf("%d", &limit);
countdown(limit);
system("PAUSE");
return 0;
}
推荐答案
这是winapi线程的简单指南
http://www.cs.rpi.edu/academics/courses/netprog /WindowsThreads.html
Here is a simple guide to winapi threads
http://www.cs.rpi.edu/academics/courses/netprog/WindowsThreads.html
话虽这么说,C是一种简约语言,没有像Java这样的内置线程(也没有大量额外的库).它被认为是在此基础上构建的一种通用语言.在类Unix系统上,除了ANSI/ISO标准(属于posix标准的一部分)之外,还有系统范围的标准c库,pthread是posix-threads. Windows C库是MS用自己的方式做事的.您可以使用提供多操作系统支持的框架,例如glib或qt(Windows,Linux,其他* nix).还有一些端口和兼容性层,可在Windows内部使用posix工具.除了库之外,Cigwin还为您提供了完整的posix环境,MinGW编译器允许您在win32层之上使用posix函数的端口.
That being said, C is a minimalistic language, does not have built-in threading like java (nor the enormous extra libraries). It was meant as a general language to build on top of it. On Unix-like systems there are system wide standard c libraries beyond the ANSI/ISO standard, which are part of the posix standard, the pthreads are posix-threads. Windows C libraries are MS makes things their own way. You can use frameworks that provides multi-OS support like glib or qt (Windows, Linux,other *nix). There are also ports and compatibility layers to use posix facilities inside windows. Cigwin gives you a full posix environment besides the libraries, The MinGW compiler allows you to use ports of posix functions on top of win32 layers.
像glib和qt这样的框架最好使您的代码保持多平台,它们隐藏了操作系统的细节,从而允许使用更通用的方法.
Frameworks like glib and qt are best to keep your code multi-platform, they hide the OS specifics, allowing a more general approach.
glib是用于GUI开发的GTK库的一部分,而QT也是GUI开发框架,但使用C ++.
glib is part of the GTK libraries for GUI development and QT is also a GUI development framework but in C++.
这篇关于如何在C for Windows中创建多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!