simple multithreaded hello world
#include <unistd.h>
#include <pthread.h>
// A C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp) {
for (int i = 0; i < 10; i++) {
printf("Hello World! %d Printing from spawned thread\n", i);
sleep(1);
}
return NULL;
}
int main() {
// Create a new thread
pthread_t thread_id;
pthread_create(&thread_id, NULL, myThreadFun, NULL);
// Main thread
for (int i = 0; i < 10; i++) {
printf("Hello World! %d from the main thread\n", i);
sleep(2);
}
pthread_join(thread_id, NULL);
exit(0);
}