Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

multithreading - How can i create a multithread in C for windows?

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.

Function (Countdown):

#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
", 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a simple guide to winapi threads
http://www.cs.rpi.edu/academics/courses/netprog/WindowsThreads.html

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.

Frameworks like glib and qt are best to keep your code multi-platform, they hide the OS specifics, allowing a more general approach.

glib is part of the GTK libraries for GUI development and QT is also a GUI development framework but in C++.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...