8

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?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Siddiqui
  • 7,662
  • 17
  • 81
  • 129
  • 2
    What platform? And what do you mean, "automatically call"? Should the thread automatically call a function after a two-second timeout? Do you need this to be a separate thread, or would a timer in the same thread be sufficient? Try being a bit more specific in your question. – Brian Campbell Dec 29 '09 at 07:29
  • I m using visual studio 2008 and window platform for the development. And automatically call means that I want to recursively call the thread after every 2 seconds, just like NSTimer function in objective c. – Siddiqui Dec 29 '09 at 07:33

7 Answers7

19

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.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
  • 1
    The new C standard, C11 includes [multi-threading support](http://en.cppreference.com/w/c/thread) –  Apr 23 '13 at 11:39
4

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Kindly tell me some libraries which can make it possible. – Siddiqui Dec 29 '09 at 07:26
  • I suggest you specify the target platform requirements first - I could tell a library that is not suited for the platform of your interest. – sharptooth Dec 29 '09 at 07:29
  • Kindly tell us your platform, Arman...On Unix, you would normally use the POSIX pthread library; on Windows, you use the native Windows threading API. – Jonathan Leffler Dec 29 '09 at 07:29
2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alon
  • 4,862
  • 19
  • 27
2

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.

Community
  • 1
  • 1
vgru
  • 49,838
  • 16
  • 120
  • 201
1

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

Jay
  • 24,173
  • 25
  • 93
  • 141
0

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;
}
Ratnesh
  • 1,554
  • 3
  • 12
  • 28