/** * Very simple example of starting threads * Also, a gotch when passing param into the thread * * @author gtowell * Created: May 2021 * **/ #include #include #include #include // The function to be executed by all threads void * myThreadFun(void *vargp) { int *ti = vargp; printf("started thread %d\n", *ti); sleep(2); printf("finishing thread %d\n", *ti); } int main(int argc, char const *argv[]) { int i; int N = atoi(argv[1]); // Let us create N threads pthread_t threads[N]; for (i = 0; i < N; i++) { pthread_create(threads+i, NULL, myThreadFun, &i); } for (int i = 0; i < N; i++) { pthread_join(threads[i], NULL); printf("Joined %d\n", i); } pthread_exit(NULL); return 0; }