Problems with pthread development in Linux

Source: Internet
Author: User

(1) threads are also resources. Use pthread_join and pthread_detach to recycle memory.

(2) mutex will form a queue for the threads waiting for the lock, and the spinlock will constantly query the lock, similar to the processing of nonblock.

(3) After pthread_cond_wait, the lock will be released for another thread to operate. After the operation, pthread_cond_sign will be used to wake up. pthread_cond_wait must be used with mutex.

(4) Whether the read/write lock can be used as appropriate

(5) Try to use pthread_cond_waittime to replace pthread_cond_wait, and pthread_cond_wait has problems when processing the exit signal.

(6) the code in the lock should be as short as possible to prevent I/O and other time-consuming operations.

(7) try to use a ring message queue or buffer

(8) Try to make the operations of each thread not dependent on each other and form Pipeline operations. Each thread implements different tasks.

(9) Reduce the stack size and increase the number of threads

(10) pthread_join is used to process threads that are in the join state. resources must be recycled by the pthread_join call thread.

Pthread_detach is used to process threads in non-join conditions. Resources are automatically reclaimed after the thread ends.

(11) Pay attention to the differences between exit and pthread_exit, especially in the main thread. Exit indicates the exit of the entire process. The management thread notifies all threads of exit.

Pthread_exit is only the exit of a single thread, not a process

(12) pthread_mutex must be initialized with pthread_mutex_init, and destroy

(13) Pay attention to the implementation of linuxthread

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.