05 December 2020

Posix Threads C11

simple example of Posix threads in C11, which doesn't have impl in gcc/clang. Remember to use -pthread as argument.

#include <pthread.h>
#include <stdio.h>
int *dowork(void *arg)
{
pthread_t mythreadid = pthread_self();
for (int i = 0; i < 5; i++) {
printf("Thread id: %lu, counter: %d, code: %s\n", mythreadid, i, (char *)arg);
}
return 0;
}
int main(void) {
pthread_t mythread;
// create a thread which executes a function
// former thrd_success
if (0 != pthread_create(&mythread, NULL, dowork, "Hello from a thread!")) {
printf("Could not create a thread.\n");
return 1;
}
// join a thread to the main thread
pthread_join(mythread, NULL);
}
view raw pthreads-p339.c hosted with ❤ by GitHub

03 December 2020

Beginning C 6th edition

I am proud to participate as co-author in Beginning C - From Beginner to Pro, 6th Edition, press 2020.

https://www.apress.com/de/book/9781484259757

Beginning C

From Beginner to Pro - 6th Edition

Authors: Gonzalez-Morris, German, Horton, Ivor
© 2020

 

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.