Search code examples
windows-vistapthreads

How to use pthread library in DevC++?


I downloaded pthread package from pthread. What should I do now to use it in DevC++?


Solution

    1. Download pthreads devpak Download
    2. Install it in Dev C++
    3. Create new Project in Dev C++
    4. After that go to Project menu -> Project Option -> In that select "Parameter Tab"
    5. Select "add Library or object" option
    6. Select "libpthreadGC2.a" file from installation directory of Dev c++ It will be in LIB directory.
    7. Press ok
    8. Now test following sample code ready for running..

    Sample Code :

    #include <iostream>
    #include <pthread.h>
    using namespace std;
    void * fun_thread1(void *data)
    {
        for(int i=0;i<100;i++)
        { 
            cout<<endl<<"In Thread 1"<<endl;
        }     
    }
    void * fun_thread2(void *data)
    {
        for(int i=0;i<100;i++)
        { 
            cout<<endl<<"In Thread 2"<<endl;
        }     
    }
    int main(int argc, char *argv[])
    {
        int status;
        // creating thread objects
        pthread_t thrd_1;
        pthread_t thrd_2;
        // create thread
        pthread_create(&thrd_1,NULL,fun_thread1,(void *)0);
        pthread_create(&thrd_2,NULL,fun_thread2,(void *)0);    
        pthread_join(thrd_1, (void **)&status);
        pthread_join(thrd_2, (void **)&status);
        system("PAUSE");
        return EXIT_SUCCESS;
    }