In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.