【发布时间】:2012-01-16 19:41:09
【问题描述】:
我不知道如何在 C 中创建线程,我看到了关于 pthread.h 库的评论,但后来我听说它仅适用于 Linux 操作系统,我有一个计时器功能,我想用它创建一个线程该函数,但我不知道我需要使用的库和编写代码的语法,如果有人可以为我提供一个带线程的简单代码,或者告诉我我需要放置什么以及函数的参数。
这是我创建的倒计时用户应用的特定时间的功能: 我需要用那个函数创建一个线程。
功能(倒计时):
#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;
}
【问题讨论】:
-
嗨。您可以使用 win32 api 中的 CreateThread
标签: c multithreading winapi timer