Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created May 4, 2011 21:28
Show Gist options
  • Select an option

  • Save vaclavbohac/956073 to your computer and use it in GitHub Desktop.

Select an option

Save vaclavbohac/956073 to your computer and use it in GitHub Desktop.
Example of threads in c.
/**
* Hello thread program.
* Vaclav Bohac (c) 2011
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *hello(void* par)
{
int *ppar = (int *) par;
int thrid = ppar[0];
puts("Hello, from thread!");
pthread_exit((void *) -thrid);
}
int main(void)
{
int i, ret, numthreads = 2;
void *thread_status[numthreads];
int thread_param[numthreads][1];
pthread_t thread[numthreads];
// Set thread ids.
for (i = 0; i < numthreads; i++) {
thread_param[i][0] = i + 1;
}
puts("Hello, from main!");
// Create threads.
for (i = 0; i < numthreads; i++) {
ret = pthread_create(&thread[i], NULL, hello, thread_param[i]);
if (ret) {
perror("Error while creating thread");
}
}
// Wait for all threads.
for (i = 0; i < numthreads; i++) {
pthread_join(thread[i], &thread_status[i]);
}
return EXIT_SUCCESS;
}
# Vaclav Bohac (c) 2011
FLAGS = -D__USE_REENTRANT -lpthread
hthread: hello-thread.o
cc $(FLAGS) -o $@ $<
hello-thread.o: hello-thread.c
cc -o $@ -c $<
clean:
-rm *.o hthread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment