Reputation: 7840
I want to create a thread in C so that the thread automatically call after two seconds. I am using Visual Studio and Windows platform for development.
How do I get started?
Upvotes: 8
Views: 5826
Reputation: 1700
Here is a sample program for Single Threading and Multithreading in Win32API written in C will work on Visual studio.
SingleThread.c
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
DWORD WINAPI funHello(void *x)
{
int c = (int*)x;
printf("\n Thread No: %d\n",c);
Sleep(2000);
return 0;
}
int main()
{
HANDLE myhandle;
DWORD threadId;
int c = 1;
myhandle = CreateThread(NULL, 0, funHello, (void *)c, 0, &threadId);
if (myhandle == NULL)
{
printf("Create Thread Failed. Error no: %d\n", GetLastError);
}
WaitForSingleObject(myhandle, INFINITE);
printf("\n Main Hello...\n");
CloseHandle(myhandle);
return 0;
}
MultiThreading.c
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#define NUM_THREADS 2 // Define Number of threads here or take as user Input,
// In yourquestion it is 2
DWORD WINAPI funHello(void *x)
{
int c = (int*)x;
printf("\n Thread No: %d\n",c);
Sleep(2000);
return 0;
}
int main()
{
HANDLE *arrayThread;
arrayThread = (int*)malloc(NUM_THREADS * sizeof(int));
DWORD ThreadId;
for (int i = 0; i < NUM_THREADS; i++)
{
arrayThread[i] = CreateThread(NULL, 0, funHello, (void *)i, 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]);
}
printf("\n Hello Main..");
return 0;
}
Upvotes: 0
Reputation: 24905
Please refer to MSDN for VC8. Refer to the createThread() help there. That should give you sufficient information.
For checking online, please go the link below:
http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx
Upvotes: 1
Reputation: 51292
You can check this link for different ways to do it: Windows threading: _beginthread vs _beginthreadex vs CreateThread C++
For cross-platform code, you can also check the Boost library or Intel Threading Building Blocks.
Upvotes: 2
Reputation: 170549
There's nothing in standard C that could help you. You need to use some library or platform-dependent features. Don't forget that many platforms simply don't have threads - only full-weight processes.
On Windows use CreateThread(). You'll need Microsoft SDK to compile your code using this and other Win32 functions.
Upvotes: 2
Reputation: 4952
C doesn't have built in threading facilities; you will have to use your OS services to create a thread.
For windows use CreateThread function.
Upvotes: 2
Reputation: 92894
Multithreading in C is platform dependent. You need to use external libraries corresponding to different platforms.
Read about:
Multithreading in C, POSIX style and Multithreading with C and Win32
Upvotes: 4
Reputation: 76611
You are going to need to use OS specific libraries to do threading. On Posix, you will want to look into pthreads (and specifically pthread_create). On Windows, you'll want CreateThread or _beginthreadex.
Upvotes: 19