I downloaded pthread package from pthread. What should I do now to use it in DevC++?
Project properties -> in C/C++ Build -> Settings -> Tool Settings tab -> In GCC C Linker -> Libraries -> add "pthread" there and enjoy. Hope this help.
If the pthread. h file still can't be found by VS, you can try to include it manually, for example right-click your project > Properties… > Configuration Properties > VC++ Directories > Reference Directories > Edit > add a new line(path) which points to the folder that includes the pthread.
To compile C program with pthread. h library, you have to put -lpthread just after the compile command gcc thread. c -o thread, this command will tell to the compiler to execute program with pthread. h library.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With