Skip to content

Instantly share code, notes, and snippets.

@ankurs
Created September 2, 2009 15:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ankurs/179778 to your computer and use it in GitHub Desktop.
Save ankurs/179778 to your computer and use it in GitHub Desktop.
A Simple Pthread example
#include<pthread.h>
#include<stdio.h>
// a simple pthread example
// compile with -lpthreads
// create the function to be executed as a thread
void *thread(void *ptr)
{
int type = (int) ptr;
fprintf(stderr,"Thread - %d\n",type);
return ptr;
}
int main(int argc, char **argv)
{
// create the thread objs
pthread_t thread1, thread2;
int thr = 1;
int thr2 = 2;
// start the threads
pthread_create(&thread1, NULL, *thread, (void *) thr);
pthread_create(&thread2, NULL, *thread, (void *) thr2);
// wait for threads to finish
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
@Xevion
Copy link

Xevion commented Sep 19, 2023

Dunno how I got here, but just a note; 64bit systems will want you to cast to a long on line 9. On 32 bit systems, this should compile fine, theoretically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment