A simple example of multithreading:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<pthread.h>void* PRINT_A (void*);void* Print_b (void*);intMain () {pthread_t t0; pthread_t T1; //Create Thread A if(Pthread_create (&t0, NULL, print_a, NULL) = =-1) {puts ("fail to create pthread t0"); Exit (1); } if(Pthread_create (&T1, NULL, print_b, NULL) = =-1) {puts ("fail to create pthread T1"); Exit (1); } //wait for thread to end void*result; if(Pthread_join (t0, &result) = =-1) {puts ("fail to recollect t0"); Exit (1); } if(Pthread_join (t1, &result) = =-1) {puts ("fail to recollect t1"); Exit (1); } return 0;}//Thread A methodvoid* PRINT_A (void*a) { for(inti =0; I <Ten; i++) {Sleep (1); Puts ("AA"); } returnNULL;}//Thread B methodvoid* Print_b (void*b) { for(intI=0;i< -; i++) {Sleep (1); Puts ("BB"); } returnNULL;}
Print:
Aabbaaaabb ...
A simple example of C-language multithreading