LINUX multi-thread programming example 6

Source: Internet
Author: User
// Create multiple threads # include <stdio. h> # include <string. h> # include <stdlib. h> # include <pthread. h> # include <unistd. h> pthread_t ntid; void printids (const char * s) {pid_tpid; pthread_ttid; pid = getpid (); tid = pthread_self (); printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID, (unsigned INT) tid, (unsigned INT) TID);} void * thr_fn (void * Arg) {printids (ARG); return NULL;} int main (void) {int err; err = pthread_create (& ntid , Null, thr_fn, "New thread:"); If (Err! = 0) {fprintf (stderr, "can't create thread: % s \ n", strerror (ERR) ;;} printids ("main thread :"); sleep (1); Return 0 ;}

//pthread_join#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>void *thr_fn1(void *arg){printf("thread 1 returning\n");return (void*)1;}void *thr_fn2(void *arg){printf("thread 2 exiting\n");pthread_exit((void*)2);}void *thr_fn3(void *arg){while(1){printf("thread 3 writing\n");sleep(1);}}int main(void){pthread_t  tid;void *tret;pthread_create(&tid, NULL, thr_fn1, NULL);pthread_join(tid, &tret);printf("thread 1 exit code %d\n", (int)tret);pthread_create(&tid, NULL, thr_fn2, NULL);pthread_join(tid, &tret);printf("thread 2 exit code %d\n", (int)tret);pthread_create(&tid, NULL, thr_fn3, NULL);sleep(3);pthread_cancel(tid);pthread_join(tid, &tret);printf("thread 3 exit code %d\n", (int)tret);return 0;}

// Demonstrate mutual interference when two threads operate on global variables # include <stdio. h> # include <stdlib. h> # include <pthread. h> # define nloop 5000int counter; void * doit (void *); int main (void) {pthread_t tida, tidb; pthread_create (& tida, null, & doit, null ); pthread_create (& tidb, null, & doit, null); pthread_join (tida, null); pthread_join (tidb, null); Return 0;} void * doit (void * vptr) {int I, Val; for (I = 0; I <nloop; I ++) {val = counter; printf ("% x: % d \ n ", (unsigned INT) pthread_self (), Val + 1); counter = Val + 1;} return NULL ;}

// Demonstrate mutual interference when two threads operate on global variables. Use mutex to solve this problem # include <stdio. h> # include <stdlib. h> # include <pthread. h> # define nloop 5000 intcounter; pthread_mutex_tcounter_mutex = pthread_mutex_initializer; // mutex lock initialization void * doit (void *); int main (void) {pthread_t tida, tidb; pthread_create (& tida, null, & doit, null); pthread_create (& tidb, null, & doit, null); pthread_join (tida, null); pthread_join (tidb, null ); return 0;} void * doit (void * vptr) {int I, Val; for (I = 0; I <nloop; I ++) {pthread_mutex_lock (& counter_mutex ); // obtain the lock val = counter; printf ("% x: % d \ n", (unsigned INT) pthread_self (), Val + 1); counter = Val + 1; pthread_mutex_unlock (& counter_mutex); // unlock} return NULL ;}

// Pthread_cond, producer and consumer model # include <stdlib. h> # include <pthread. h> # include <stdio. h> struct MSG {struct MSG * Next; int num;}; struct MSG * head; pthread_cond_t has_product = pthread_cond_initializer; pthread_mutex_t lock = consumer; void * Consumer (void * P) {struct MSG * MP; For (;) {pthread_mutex_lock (& lock); While (Head = NULL) {pthread_cond_wait (& has_product, & lock ); // The thread calls pthread_cond_wait on a condition variable // block the wait. This function performs the following three steps: // 1. release mutex // 2. blocking wait // 3. when awakened, obtain mutex again and return} MP = head; head = MP-> next; pthread_mutex_unlock (& lock); printf ("consume-% d \ n ", MP-> num); free (MP); sleep (RAND () % 5);} return NULL;} void * producer (void * P) {struct MSG * MP; for (;) {MP = malloc (sizeof (struct MSG); MP-> num = rand () % 1000 + 1; printf ("produce + % d \ n", MP-> num); pthread_mutex_lock (& lock); MP-> next = head; head = mp; pthread_mutex_unlock (& lock ); pthread_cond_signal (& has_product); // The thread can call pthread_cond_signal to wake up another thread sleep (RAND () % 5) waiting on a certain condition variable;} int main () {pthread_t PID, CID; pthread_create (& pid, null, producer, null); pthread_create (& cid, null, consumer, null); pthread_join (PID, null ); pthread_join (CID, null); Return 0 ;}

// SEM, producer and consumer model # include <stdlib. h> # include <pthread. h> # include <stdio. h> # include <semaphore. h> # definenum5sem_t blank_number, product_number; int queue [num]; pthread_cond_t has_product = queue; pthread_mutex_t lock = queue; void * Consumer (void * P) {int n = 0; for (;) {sem_wait (& product_number); printf ("consume-% d \ n", queue [N]); queue [N] = 0; sem_post (& blank_number); n = (n + 1) % num; sleep (RAND () % 5);} return NULL;} void * producer (void * Arg) {int n = 0; For (;) {sem_wait (& blank_number); queue [N] = rand () % 1000 + 1; printf ("produce + % d \ n", queue [N]); sem_post (& product_number); n = (n + 1) % num; sleep (RAND () % 5) ;}} int main () {pthread_t PID, CID; sem_init (& blank_number, 0, num); sem_init (& product_number, 0, 0 ); pthread_create (& pid, null, producer, null); pthread_create (& cid, null, consumer, null); pthread_join (PID, null); pthread_join (CID, null ); sem_destroy (& blank_number); sem_destroy (& product_number); Return 0 ;}

References: http://www.eefocus.com/html/09-11/26160910394Yba.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.