Jump to content
Sign in to follow this  
mnajem

Multithreaded Programming In C

Recommended Posts

salam,

Haritu aku ada tanye pasal regexp+KMP. ..then borak2 ngan bos aku dia mcm tak prefer aku pursue.

instead dia suruh aku buat mulithreaded for KMP algorithm tu.

Dia ada sebut guna visual studio ada include multithreaded library.

aku ada usha2 gak pasal OpenMP etc.

tinggal 10 hari je lagi.. aku pun tak tau macam mana nak multithreadkan program ..

ada clue tak. (code yg aku ada sequential biasa ni banyak file..nanti sambung).

tq. :(

aku ada jumpe visual studio punya:

http://flinflon.brandonu.ca/dueck/2004/62306/ThreadWin32.htm

Edited by mnajem

Share this post


Link to post
Share on other sites

Sebelum gune CreateThread(), bace link ni dulu

http://www.microsoft.com/msj/1099/win32/win321099.aspx

http://www.microsoft.com/msj/0799/Win32/Win320799.aspx

Anyways, ni simple example on multi-threading...

Note: Ni bukan DOS program, ni Windows & aku assume ko familiar dgn Win32API

/*
    VC++6 :- sebelum compile, g kat

    Project -> Settings -> C++ -> Category -> Code Generation

        Debug:
            Pilih sama ada :
                Debug MultiThreaded
                Debug MultiThreaded DLL

        Release:
            Pilih sama ada :
                MultiThreaded
                MultiThreaded DLL
*/

/* *** Macro *** */
#define            WIN32_LEAN_AND_MEAN

/* *** Library *** */
#pragma comment (lib,"comctl32.lib")

/* *** Header *** */
#include <windows.h>
#include <commctrl.h>
#include <process.h>

/* *** Constant *** */
#define            USER_dialog_box     "Main Dialog Box"
#define            WIDTH               230
#define            HEIGHT              107

/* *** Command Identifier *** */
#define            THREADED            666
#define            NONTHREADED         667

/* *** User Defined *** */
typedef struct tagBEEP{
    DWORD uMaxFrequency;
    DWORD uDuration;
}BEEP, *LPBEEP;

/* *** Function Prototype *** */
long __stdcall         MAIN_WndProc          (HWND, UINT, WPARAM, LPARAM);
HFONT                  CreateFontEx          (HWND, int, int, const char *);
unsigned int __stdcall SpawnThread           (void *);
void                   BunyiSpeakerKatCasing (unsigned long, unsigned long);



/*----------------------------------------------------------------------------------------------*/
/* ***** WinMain ****                                                                           */
/*----------------------------------------------------------------------------------------------*/
int __stdcall WinMain (HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
{
    HWND                 hWnd;
    MSG                  msg;
    WNDCLASSEX           wcex;

    wcex.cbSize        = sizeof(WNDCLASSEX);
    wcex.style         = 0;
    wcex.lpfnWndProc   = MAIN_WndProc;
    wcex.cbClsExtra    = 0;
    wcex.cbWndExtra    = 0;
    wcex.hInstance     = hInstance;
    wcex.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH) COLOR_3DSHADOW;
    wcex.lpszMenuName  = 0;
    wcex.lpszClassName = USER_dialog_box;
    wcex.hIconSm       = NULL;


    if (!RegisterClassEx (&wcex)) return(FALSE);

    hWnd = CreateWindow (USER_dialog_box,
                        "Multi Threaded",
                        WS_SYSMENU|WS_CLIPCHILDREN,
                        GetSystemMetrics(SM_CXSCREEN)/2-WIDTH/2,
                        GetSystemMetrics(SM_CYSCREEN)/2-HEIGHT/2,
                        WIDTH,
                        HEIGHT,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);

    InitCommonControls();

    ShowWindow   (hWnd, nCmdShow);
    UpdateWindow (hWnd);


    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage (&msg);
        DispatchMessage  (&msg);
    }
    return msg.wParam;
return 0;
}



/*----------------------------------------------------------------------------------------------*/
/* **** MAIN_WndProc ****                                                                       */
/*----------------------------------------------------------------------------------------------*/
long __stdcall MAIN_WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HFONT          hFont;
    static HWND           Threaded;
    static HWND           NonThreaded;
    static HANDLE         hThread;
    static unsigned       ThreadID;
    static BEEP           beep;

    switch (uMsg) 
    {
  /****************************************************/
        case WM_CLOSE:
        {
            DeleteObject (hFont);
            DestroyWindow (hWnd);
            break;
        }
  /****************************************************/
        case WM_COMMAND:
        {
            switch (HIWORD(wParam))
            {
                case BN_CLICKED:
                {
                    switch (LOWORD(wParam))
                    {
                        case THREADED:
                        {
                            hThread = (HANDLE) _beginthreadex (NULL,
                                                               0,
                                                               &SpawnThread,
                                                               &beep,
                                                               0,
                                                               &ThreadID);
                            CloseHandle (hThread);
                            break;
                        }
                        case NONTHREADED:
                        {
                            BunyiSpeakerKatCasing (0xFF, 10);
                            break;
                        }
                    }
                    break;
                }
            }
            break;
        }
  /****************************************************/
        case WM_CREATE:
        {
            hFont = CreateFontEx (hWnd, 8, FW_NORMAL, "Tahoma");

            Threaded = CreateWindow ("Button",
                                     "Call Beep() with Thread",
                                     WS_CHILD|WS_VISIBLE,
                                     10, 10, 200, 23,
                                     hWnd,
                                    (HMENU) THREADED,
                                     GetModuleHandle (0),
                                     NULL);
            NonThreaded = CreateWindow ("Button",
                                        "Call Beep() without Thread",
                                        WS_CHILD|WS_VISIBLE,
                                        10, 40, 200, 23,
                                        hWnd,
                                       (HMENU) NONTHREADED,
                                        GetModuleHandle (0),
                                        NULL);
            SendMessage (Threaded,    WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
            SendMessage (NonThreaded, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));

            beep.uDuration     = 0xff;
            beep.uMaxFrequency = 10;
            break;
        }
  /****************************************************/
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
  /****************************************************/
        default:
        {
            return DefWindowProc (hWnd, uMsg, wParam, lParam);
        }
  /****************************************************/
    }
    return 0;
}



/*----------------------------------------------------------------------------------------------*/
/* **** CreateFontEx ****                                                                       */
/*----------------------------------------------------------------------------------------------*/
HFONT CreateFontEx (HWND hWnd, int iPointSize, int iFontWeight, LPCSTR FontName)
{
    HDC hDC;
    int iHeight;

    hDC     =  GetDC (hWnd);
    iHeight = -MulDiv (iPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    ReleaseDC (hWnd, hDC);

    return CreateFontA (iHeight, 0, 0, 0, iFontWeight, 0, 0, 0, 0, 0, 0, 0, 0, FontName);
}



/*----------------------------------------------------------------------------------------------*/
/* **** SpawnThread ****                                                                        */
/*----------------------------------------------------------------------------------------------*/
unsigned int __stdcall SpawnThread (void* ParameterAddress)
{
    BunyiSpeakerKatCasing (((LPBEEP)ParameterAddress)->uDuration,
                           ((LPBEEP)ParameterAddress)->uMaxFrequency);

    _endthreadex (0);
    return 0;
}



/*----------------------------------------------------------------------------------------------*/
/* **** BunyiSpeakerKatCasing ****                                                              */
/*----------------------------------------------------------------------------------------------*/
void BunyiSpeakerKatCasing (unsigned long uMaxFreq, unsigned long uDuration)
{
    unsigned long uLoop = 37;

    while (uLoop ^ uMaxFreq)
    {
        Beep (uLoop, uDuration);
        uLoop++;
    }
}

Edited by Stranger

Share this post


Link to post
Share on other sites

nak buat multithreaded program takde la susah sangat, just a little bit trivial. secara asasnya, awak kena buat satu function yg akan di jalankan dalam thread lain. function tersebut boleh di panggil banyak kali dan dijalankan secara serentak. kalau nak guna windows platform, boleh pakai CreateThread

contoh boleh tengok kat sini. http://www.codersource.net/win32_multithreading.html

kalau nak contoh dari snippets aku pun boleh tapi kena sign NDA. ehehehe.j/k.

Share this post


Link to post
Share on other sites

erm.. aku tengok header baru perasan threading unix ngan windows lain.

kalau dalam unix korang biasa buat tak (linux)...

Share this post


Link to post
Share on other sites

AFAIK, linux takde specific API utk multithreading programming, tapi boleh guna fork() utk create child process, dan berjenis2 threading model mcm posix thread, linuxthread, ntpl.

aku tak biasa guna *nix as development platform. kenot help much la.

mungkin tutorial ni boleh membantu - http://www.yolinux.com/TUTORIALS/LinuxTuto...sixThreads.html

Edited by zeph

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...