Lecture 11

From CS113

Threading Example

Example from the lecture in class:

#include<pthread.h>
#include<stdio.h>
#include<unistd.h>

void *entry(void *arg) {
    char *s = (char *)arg;

    while (1) {
        printf("Hello from %s: %p\n", s, &arg);
        sleep(1);
    }
}

int main(int argc) {
    pthread_t id1, id2; 

    pthread_create(&id1, NULL, entry, "Thread 1");
    pthread_create(&id2, NULL, entry, "Thread 2"); 

    while (1) {
        printf("Hello from main: %p\n", &argc);
        sleep(1);
    }
}
Personal tools
Navigation