linux的基础知识——线程
生活随笔
收集整理的這篇文章主要介紹了
linux的基础知识——线程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1.什么是線程?
- 2.linux內(nèi)核線程實(shí)現(xiàn)原理
- 3.線程共享資源
- 4.線程的非共享資源
- 5.線程優(yōu)缺點(diǎn)
- 6.線程的控制原語
- 6.1 pthread_self函數(shù)
- 6.2 pthread_create函數(shù)
- 6.3 程序:創(chuàng)建線程
- 7.線程與共享
- 8.pthread_exit線程退出函數(shù)
1.什么是線程?
2.linux內(nèi)核線程實(shí)現(xiàn)原理
3.線程共享資源
4.線程的非共享資源
5.線程優(yōu)缺點(diǎn)
6.線程的控制原語
6.1 pthread_self函數(shù)
6.2 pthread_create函數(shù)
6.3 程序:創(chuàng)建線程
#include<stdio.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h>void *thrd_func(void *arg) {printf("子進(jìn)程ID:%u;子線程ID:%lu\n",getpid(),pthread_self()); } int main() {pthread_t tid;int ret;printf("主進(jìn)程ID:%u;主線程ID:%lu\n",getpid(),pthread_self());ret = pthread_create(&tid,NULL,thrd_func,NULL);if(ret != 0){printf("pthread_create error\n");exit(1);}sleep(1);return 0; }7.線程與共享
8.pthread_exit線程退出函數(shù)
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h>void *tfn(void *arg) {int i;i = (int)arg;printf("i am %dth thread,thread_id:%lu\n",i+1,pthread_self());return NULL; }int main() {int n=5,i;pthread_t tid;for(i=0;i<n;i++){pthread_create(&tid,NULL,tfn,(void *)i);}printf("i am main thread,thread id:%lu\n",pthread_self());pthread_exit(NULL); }總結(jié)
以上是生活随笔為你收集整理的linux的基础知识——线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC关键技术-命令传递机制
- 下一篇: 牛客16785 Cantor表