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

Unix Advanced Environment programming-Chapter 11 Thread Learning

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Piecemeal knowledge.

1. The type of thread id--- process id is pid_t, and the type of thread id is pthread_t

Compare two thread id:

# include

Int pthread_equal (pthread_t tid1, pthread_t tid2)

Get your own thread id:

Pthread_t pthread_self (void)

two。 Thread creation

Int pthread_create (pthread_t * restrict tidp, const pthread_attr_t * restrict attr

Void * (* start_rtn) (void *), void * restrict arg)

Here is an example of the print thread id:

# include

# include

# include

# include

# include

# include

Pthread_t ntid

Void printids (const char * s) {

Pid_t pid

Pthread_t tid

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 ("new thread:")

Return ((void*) 0)

}

Int main () {

Int err = 0

Err = pthread_create (& ntid, NULL, thr_fn, NULL)

/ / err = pthread_create (& ntid, NULL, thr_fn, NULL)

If (erratically ordered 0) {

Printf ("% s\ n", strerror (err))

}

Printids ("main thread:")

Sleep (1)

Exit (0)

}

Compiled with ubuntu,eclipse, prompts that pthread_create. The header file is defined, but there is no connection thread library. Eclipse needs to be set up. Select Project-Properties-c, c++build--settings---gcc c linker---libraries, and add the library pthread. Compile and run again. Ok .

Main thread: pid 2164 tid 3079145152 (0xb78806c0)

New thread: pid 2164 tid 3079142256 (0xb787fb70)

3. Thread termination

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 *) 1)

}

Int main () {

Int err

Pthread_t tid1, tid2

Void * tret

Err = pthread_create (& tid1, NULL, thr_fn1, NULL)

If (err! = 0) {

Printf ("can't create thread 1 err% s\ n", strerror (err))

}

Err = pthread_create (& tid2, NULL, thr_fn2, NULL)

If (erratically ordered 0) {

Printf ("can't create thread 2VR% s\ n", strerror (err))

}

Err = pthread_join (tid1, & tret)

If (erratically ordered 0) {

Printf ("can't join with thread 1 err% s\ n", strerror (err))

}

Printf ("thread 1 exit code% d\ n", (int) tret)

Err = pthread_join (tid2, & tret)

If (erratically ordered 0) {

Printf ("can't join eith thread 2VR% s\ n", strerror (err))

}

Printf ("thread 2 exit code% d\ n", (int) tret)

Exit (0)

}

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