Skip to content

Instantly share code, notes, and snippets.

@shurane
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurane/2d91aac499758c11e679 to your computer and use it in GitHub Desktop.
Save shurane/2d91aac499758c11e679 to your computer and use it in GitHub Desktop.
C examples for Jeremy K.

Try running this:

clang -o argv argv.c
./argv abc def ghi jkl 123 456

clang -o pthread pthread.c -lpthread
./pthread
#include <stdio.h>
int main(int argc, char **argv){
printf("argc %d\n", argc);
for (int i=0; i<10; i++){
if (argc > i){
printf("argv[%d]:= %s\n", i, argv[i]);
}
}
}
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
// based off of
// https://computing.llnl.gov/tutorials/pthreads/samples/hello.c
// TODO start threads randomly
void *thread_code(void *threadid){
printf("Thread started: #%ld!\n", (long) threadid);
pthread_exit(NULL);
}
int main(int argc, char **argv){
long t;
int rc;
pthread_t threads[NUM_THREADS];
for(t=0;t<NUM_THREADS;t++){
printf("In main: creating thread #%ld\n", t);
rc = pthread_create(&threads[t], NULL, thread_code, (void *) t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment