开发者

Threading in C

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?


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.


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


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.


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.


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.


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜