C + + multithreaded programming on Linux

Source: Internet
Author: User

POSIX multithreaded model Pthread.h function:

pthread_attr_t attr; Thread attribute struct, parameter pthread_attr_init (&attr) added when creating thread; Initialize  pthread_attr_setdetachstate (&attr, pthread_create_joinable);//Set you want to specify a thread attribute parameter, This parameter indicates that the thread can join, join work//can indicate that the main program can wait until the end of the thread to do something, implement the main program and thread synchronization function  pthread_t tid1, Tid2;//Save two threads idint ret = Pthread_ Create (&TID1, &attr, Say_hello1, (void*) &index1); Create Thread 1ret = Pthread_create (&tid2, &attr, Say_hello2, (void*) &index2);  Create Thread 2pthread_join (TID1, NULL); Connect two threads  pthread_join (Tid2, NULL);

Functions used by mutexes:

pthread_mutex_t Sum_mutex; Mutex lock Pthread_mutex_init (&sum_mutex, NULL); Initialize the lock Pthread_mutex_lock (&sum_mutex); Occupy the lock//do something here. Pthread_mutex_unlock (&sum_mutex); Release lock Pthread_mutex_destroy (&sum_mutex); Sign out of a lock


The function used for the semaphore:

pthread_cond_t Tasks_cond; Conditional signal Variable pthread_cond_init (&tasks_cond, NULL); Initialize the conditional signal variable pthread_cond_signal (&tasks_cond); The condition satisfies, sends the signal pthread_cond_wait (&tasks_cond, &tasks_mutex); Wait for Signal Pthread_cond_destroy (&tasks_cond); Sign out of conditional signal variables

The above is the C + + multithreaded programming on Linux content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.