Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Linux multithreading programming

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve Linux multithreaded programming". In daily operation, I believe many people have doubts about how to achieve Linux multithreaded programming. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to achieve Linux multithreaded programming". Next, please follow the editor to study!

1. Multithreading VS multiprocess

Threads have many advantages over processes. In Linux system, starting a new process must be assigned an independent address space, and a large number of data tables must be established to maintain code segments and data. Multiple threads running in a process use the same address space between them. In this way, threads under the same process share data space, the data can be used with each other, and the switching between threads is faster, which can make more efficient use of CPU.

II. Program design

[note] the dynamic library libpthread.a should be loaded when the header file is compiled, using-lpthread

1. Create a thread

2. Waiting thread

3. Close the thread

4. Exit cleanup

1. Create a thread

The code is as follows:

Int pthread_create (pthread_t * tidp, const pthread_attr_t * attr, void * (* start_rtn) (void), void * arg)

Tidp is the thread id, which is the value assigned by the function, so pass an address of pthread_t.

Attr thread property, which is usually empty.

Start_rtn is the function to be executed by the thread. The return value is a null pointer, and the argument is the following * arg.

Returns 0 if successful, otherwise returns the error number.

Example:

The code is as follows:

# include

# include

Void * func1 (void * arg) {/ / original function declaration

Int i

For (iSuppli)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report