Reputation: 113
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\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;
}
Upvotes: 7
Views: 17539
Reputation: 1202
Here is a simple guide to WinAPI threads:
http://www.cs.rpi.edu/academics/courses/netprog/WindowsThreads.html (Archived)
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 provide 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++.
Upvotes: 4
Reputation: 1700
Here is a sample program for the Single Threading and Multi-threading for Win32API written in C.
SingleThread_Win.c
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include<Windows.h>
void countdown(int second)
{
int secs = 1;
time_t unix;
.....
.....
}
DWORD WINAPI funThread(void *x)
{
int c = (int*)x;
countdown(c);
return 0;
}
int main()
{
int limit;
printf("How much time would you like (In secs): ");
scanf("%d", &limit);
HANDLE myhandle;
DWORD threadId;
myhandle = CreateThread(NULL, 0, funThread, (void *)limit, 0, &threadId);
if (myhandle == NULL)
{
printf("Create Thread Failed. Error no: %d\n", GetLastError);
}
WaitForSingleObject(myhandle, INFINITE);
CloseHandle(myhandle);
system("PAUSE");
return 0;
}
MultiThreading_Win.c
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NUM_THREADS 5 // Define number of threads here or take as user input
#include<Windows.h>
void countdown(int second)
{
int secs = 1;
time_t unix;
.....
.....
}
DWORD WINAPI funThread(void *x)
{
int c = (int*)x;
countdown(c);
return 0;
}
int main()
{
int limit;
printf("How much time would you like (In secs): ");
scanf("%d", &limit);
//scanf("%d", &NUM_THREADS); //Take number of threads as Input
HANDLE *arrayThread;
arrayThread = (int*)malloc(NUM_THREADS * sizeof(int));
DWORD ThreadId;
for (int i = 0; i < NUM_THREADS; i++)
{
arrayThread[i] = CreateThread(NULL, 0, funThread, (void *)limit, 0, &ThreadId);
if (arrayThread[i] == NULL)
{
printf("Create Thread %d get failed. Error no: %d", i, GetLastError);
}
}
WaitForMultipleObjects(NUM_THREADS, arrayThread,TRUE,INFINITE);
DWORD lpExitCode;
BOOL result;
for (int i = 0; i < NUM_THREADS; i++)
{
CloseHandle(arrayThread[i]);
}
system("PAUSE");
return 0;
}
Upvotes: -1
Reputation: 62472
The tags suggest you're using Windows. The Win32 api has a CreateThread function that can be used to launch a new thread. However, when you're using the C runtime (which you are) then you should use the _beginthread function by including process.h
.
The reason to use _beginthread
is that it initializes thread specific state information that the C runtime requires to execute correctly within a thread.
Upvotes: 3
Reputation: 2108
The C standard library does not have the thread concept. A thread is usually a concept in OS. You can use win32api to handle threads just like you use pthread in linux(not so equal actually..)
To know more about win32api, please go to the MSDN.
How to create thread with win32api:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx
Upvotes: 2
Reputation: 3013
If you're familiar with pthreads and/or want a cross-platform codebase, you can use MinGW and pthreads-win32. I've recently used it in an application of mine and it seems to work great. If you're developing exclusively for Windows, its probably worth your time to learn the WinAPI threading stuff as redcomet suggested.
Upvotes: 3