当前位置: 首页 > news >正文

深圳做微信网站网站整站优化

深圳做微信网站,网站整站优化,带做骑传奇私服网站,企业为什么上市一.线程 1.线程的概念 线程是进程内部的一条执行序列或执行路径,一个进程可以包含多条线程。 2.线程的三种实现方式 ◼ 内核级线程:由内核创建,创建开销大,内核能感知到线程的存在 ◼ 用户级线程:线程的创建有用户空…

一.线程

1.线程的概念

    线程是进程内部的一条执行序列或执行路径,一个进程可以包含多条线程。

2.线程的三种实现方式

◼ 内核级线程:由内核创建,创建开销大,内核能感知到线程的存在
◼ 用户级线程:线程的创建有用户空间的线程库完成;内核不知道线程的存在
组合级线程:兼顾以上两者的优点,
区别:两个处理器,用户级线程在内核上无法并行处理,只能交替执行;内核级可以同时执行;组合技在不同的空间采用不同的处理方式

线程同步的方法:信号量,互斥锁,条件变量,读写锁

Ps -elf 查看线程ID

Linux 中线程的实现:

Linux 实现线程的机制非常独特。从内核的角度来说,它并没有线程这个概念。Linux 把
所有的线程都当做进程来实现。内核并没有准备特别的调度算法或是定义特别的数据结构来
表征线程。相反,线程仅仅被视为一个与其他进程共享某些资源的进程。每个线程都拥有唯
一隶属于自己的 task_struct,所以在内核中,它看起来就像是一个普通的进程(只是线程和
其他一些进程共享某些资源,如地址空间)。

3.进程与线程的区别

        ◼ 进程是资源分配的最小单位,线程是 CPU 调度的最小单位
        ◼ 进程有自己的独立地址空间,线程共享进程中的地址空间
        ◼ 进程的创建消耗资源大,线程的创建相对较小
        ◼ 进程的切换开销大,线程的切换开销相对较小

二.线程使用

1.线程库

 #include <pthread.h>/*pthread_create()用于创建线程thread: 接收创建的线程的 IDattr: 指定线程的属性start_routine: 指定线程函数arg: 给线程函数传递的参数成功返回 0, 失败返回错误码*/int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);13./*pthread_exit()退出线程retval:指定退出信息*/int pthread_exit(void *retval);/*pthread_join()等待 thread 指定的线程退出,线程未退出时,该方法阻塞retval:接收 thread 线程退出时,指定的退出信息
*/int pthread_join(pthread_t thread, void **retval);

多线程代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>void * pthread_fun(void *arg){int i = 0;for(; i < 5; ++i){sleep(1);printf("fun thread running\n");}pthread_exit("fun over");}int main(){pthread_t tid;int res = pthread_create(&tid, NULL, pthread_fun, NULL);assert(res == 0);int i = 0;for(; i < 5; ++i){sleep(1);printf("main thread running\n");}char *s = NULL;pthread_join(tid, (void **)&s);printf("s = %s\n", s);exit(0);}

三.线程同步

       线程同步指的是当一个线程在对某个临界资源进行操作时,其他线程都不可以对这个资
源进行操作,直到该线程完成操作,其他线程才能操作,也就是协同步调,让线程按预定的
先后次序进行运行。 线程同步的方法有四种:互斥锁、信号量、条件变量、读写锁。

1.互斥锁

#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);//初始化锁int pthread_mutex_lock(pthread_mutex_t *mutex);//上锁,其他线程无法使用int pthread_mutex_unlock(pthread_mutex_t *mutex);//开锁int pthread_mutex_destroy(pthread_mutex_t *mutex);//销毁锁
为什么线程需要同步和互斥的操作?
因为线程引入共享了进程的地址空间,导致了一个线程操作数据时候,极其容易影响到其他线程的情况;对其他线程造成不可控因素,或引起异常,逻辑结果不正确的情况;这也是线程不安全的原因!
重点概念:
        示例代码如下,主线程和函数线程模拟访问打印机,主线程输出第一个字符‘a’表示开
始使用打印机,输出第二个字符‘a’表示结束使用,函数线程操作与主线程相同。(由于打
印机同一时刻只能被一个线程使用,所以输出结果不应该出现 abab)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>//sem_t sem;
pthread_mutex_t mutex;
void* fun1(void* arg)
{for(int i=0;i<5;i++){//sem_wait(&sem);pthread_mutex_lock(&mutex);printf("A");fflush(stdout);int n=rand()%3;sleep(n);printf("A");fflush(stdout);//sem_post(&sem);pthread_mutex_unlock(&mutex);n=rand()%3;sleep(n);}
}
void* fun2(void* arg)
{for(int i=0;i<5;i++){//sem_wait(&sem);pthread_mutex_lock(&mutex);printf("B");fflush(stdout);int n=rand()%3;sleep(n);printf("B");fflush(stdout);//sem_post(&sem);pthread_mutex_unlock(&mutex);n=rand()%3;sleep(n);}}
int main()
{//sem_init(&sem,0,1);pthread_mutex_init(&mutex,NULL);pthread_t id1,id2;pthread_create(&id1,NULL,fun1,NULL);pthread_create(&id2,NULL,fun2,NULL);pthread_join(id1,NULL);pthread_join(id2,NULL);//sem_destroy(&sem);pthread_mutex_destroy(&mutex);exit(0);
}

2.信号量

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>char buff[128] = {0};sem_t sem1;sem_t sem2;void* PthreadFun(void *arg)
{int fd = open("a.txt", O_RDWR | O_CREAT, 0664);assert(fd != -1);//函数线程完成将用户输入的数据存储到文件中while(1){sem_wait(&sem2);if(strncmp(buff, "end", 3) == 0){break;}write(fd, buff, strlen(buff));memset(buff, 0, 128);sem_post(&sem1);}sem_destroy(&sem1);sem_destroy(&sem2);}int main(){sem_init(&sem1, 0, 1);sem_init(&sem2, 0, 0);pthread_t id;int res = pthread_create(&id, NULL, PthreadFun, NULL);assert(res == 0);//主线程完成获取用户数据的数据,并存储在全局数组 buff 中while(1){sem_wait(&sem1);printf("please input data: ");fflush(stdout);fgets(buff, 128, stdin);buff[strlen(buff) - 1] = 0;sem_post(&sem2);if(strncmp(buff, "end", 3) == 0){break;}}pthread_exit(NULL);}

3.条件变量

条件变量是利用线程间共享的全局变量进行同步的一种机制。
主要包括两个动作:一个线程等待”条件变量的条件成立”而挂起;另一个线程使”条件成立”(给出条件成立信号)。
为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
条件变量类型为 pthread_cond_t。

条件变量接口

pthread_cond_init()       //初始化
pthread_cond_wait()       //等待将信息存入并等待达到唤醒条件
pthread_cond_signal()     //只唤醒一个
pthread_cond_broadcast()  //唤醒所以有的线程
条件变量有什么用

使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止。条件变量始终与互斥锁一起使用,对条件的测试是在互斥锁(互斥)的保护下进行的

如果条件为假,线程通常会基于条件变量阻塞,并以原子方式释放等待条件变化的互斥锁。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t cond;
pthread_mutex_t mutex;
void* funa(void* arg)
{char* s =(char*)arg;while(1){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);//添加到条件变量的等待队列,阻塞pthread_mutex_unlock(&mutex);if(strncmp(s,"end",3)==0){break;}printf("funa:%s\n",s);}
}
void* funb(void* arg)
{char* s =(char*)arg;while(1){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);//添加到条件变量的等待队列,阻塞pthread_mutex_unlock(&mutex);if(strncmp(s,"end",3)==0){break;}printf("funb:%s\n",s);}}
int main()
{pthread_mutex_init(&mutex,NULL);pthread_cond_init(&cond,NULL);pthread_t id1,id2;char buff[128]={0};pthread_create(&id1,NULL,funa,buff);pthread_create(&id2,NULL,funb,buff);while(1){char tmp[128]={0};fgets(tmp,128,stdin);strcpy(buff,tmp);if(strncmp(tmp,"end",3)==0){pthread_mutex_lock(&mutex);pthread_cond_broadcast(&cond);//唤醒所有线程pthread_mutex_unlock(&mutex);break;}else{pthread_mutex_lock(&mutex);pthread_cond_signal(&cond);//唤醒一个pthread_mutex_unlock(&mutex);}}pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);exit(0);
}

4.读写锁

读写锁:是一对锁,分为读锁和写锁,允许多个线程同时获取读写锁,但再通过一时间,只允许一个线程获得写锁,或者可以由多个线程获得读锁

接口:

#include <pthread.h>int pthread_rwlock_init(pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr);int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

代码示例

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>pthread_rwlock_t lock;void* fun_r1(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_rdlock(&lock);printf("fun r1 start\n");int n=rand()%3;sleep(n);printf("fun r1 end\n");pthread_rwlock_unlock(&lock);n=rand()%3;sleep(n);}
}void* fun_r2(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_rdlock(&lock);printf("fun r2 start\n");int n=rand()%3;sleep(n);printf("fun r2 end\n");pthread_rwlock_unlock(&lock);n=rand()%3;sleep(n);}}void* fun_w(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_wrlock(&lock);printf(" fun w start\n");int n=rand()%3;sleep(n);printf(" fun w end\n");pthread_rwlock_wrlock(&lock);n = rand()%3;sleep(n);}
}int main()
{pthread_rwlock_init(&lock,NULL);pthread_t id1,id2,id3;pthread_create(&id1,NULL,fun_r1,NULL);pthread_create(&id2,NULL,fun_r2,NULL);pthread_create(&id3,NULL,fun_w,NULL);pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_join(id3,NULL);pthread_rwlock_destroy(&lock);exit(0);
}


文章转载自:
http://ultravirus.fcxt.cn
http://fruition.fcxt.cn
http://alcohol.fcxt.cn
http://qualitative.fcxt.cn
http://shipmate.fcxt.cn
http://abatement.fcxt.cn
http://oblong.fcxt.cn
http://cerebrovascular.fcxt.cn
http://postal.fcxt.cn
http://leiotrichi.fcxt.cn
http://patch.fcxt.cn
http://larvikite.fcxt.cn
http://humanness.fcxt.cn
http://syntone.fcxt.cn
http://angler.fcxt.cn
http://spot.fcxt.cn
http://clothesbasket.fcxt.cn
http://aristophanic.fcxt.cn
http://cementite.fcxt.cn
http://workday.fcxt.cn
http://overperform.fcxt.cn
http://osset.fcxt.cn
http://underfill.fcxt.cn
http://bepraise.fcxt.cn
http://oup.fcxt.cn
http://hassle.fcxt.cn
http://parabolical.fcxt.cn
http://iiion.fcxt.cn
http://seraphim.fcxt.cn
http://nonstriker.fcxt.cn
http://haziness.fcxt.cn
http://nonconformism.fcxt.cn
http://naturalise.fcxt.cn
http://galvanise.fcxt.cn
http://divulsion.fcxt.cn
http://penny.fcxt.cn
http://duplicator.fcxt.cn
http://vaticinate.fcxt.cn
http://baluchithere.fcxt.cn
http://groundnut.fcxt.cn
http://jury.fcxt.cn
http://epicyclic.fcxt.cn
http://shearing.fcxt.cn
http://pardner.fcxt.cn
http://defluent.fcxt.cn
http://podzolize.fcxt.cn
http://deracinate.fcxt.cn
http://chimaera.fcxt.cn
http://slaggy.fcxt.cn
http://pr.fcxt.cn
http://ubiquitously.fcxt.cn
http://betweenwhiles.fcxt.cn
http://potoroo.fcxt.cn
http://idd.fcxt.cn
http://gadgeteering.fcxt.cn
http://chanter.fcxt.cn
http://perorate.fcxt.cn
http://whacky.fcxt.cn
http://lecher.fcxt.cn
http://signalise.fcxt.cn
http://ballistician.fcxt.cn
http://ratchet.fcxt.cn
http://yaffingale.fcxt.cn
http://olympus.fcxt.cn
http://shovel.fcxt.cn
http://horsebean.fcxt.cn
http://man.fcxt.cn
http://danceable.fcxt.cn
http://id.fcxt.cn
http://acrophobe.fcxt.cn
http://psychoenergetic.fcxt.cn
http://leggy.fcxt.cn
http://polymer.fcxt.cn
http://ingenerate.fcxt.cn
http://methylcatechol.fcxt.cn
http://fingertip.fcxt.cn
http://cryptographical.fcxt.cn
http://cashboy.fcxt.cn
http://hogskin.fcxt.cn
http://virtuoso.fcxt.cn
http://unbelief.fcxt.cn
http://teacake.fcxt.cn
http://scuttle.fcxt.cn
http://anglist.fcxt.cn
http://diminishing.fcxt.cn
http://incretory.fcxt.cn
http://tibiofibula.fcxt.cn
http://repacify.fcxt.cn
http://xyst.fcxt.cn
http://reprovingly.fcxt.cn
http://bioplast.fcxt.cn
http://coniroster.fcxt.cn
http://septennate.fcxt.cn
http://submultiple.fcxt.cn
http://amiantus.fcxt.cn
http://underbred.fcxt.cn
http://gompa.fcxt.cn
http://fishpaste.fcxt.cn
http://baudelairean.fcxt.cn
http://devout.fcxt.cn
http://www.hrbkazy.com/news/64511.html

相关文章:

  • 新的营销方式有哪些奇零seo赚钱培训
  • 网站个性化定制服务广告优化师怎么学
  • 微网站是自己做可以不网站友情链接自动上链
  • 网站管理系统是什么seo优化诊断工具
  • 做转运网站重庆森林为什么叫这个名字
  • 阜阳做网站公司推广普通话
  • 网站建设流程 知乎如何引流推广产品
  • 江苏百城建设有限公司官方网站宁德seo推广
  • 深圳罗湖的网站建设营销推广渠道有哪些
  • 雄县做网站的crm
  • 高明骏域网站建设seo建设招商
  • 外贸购物网站建设网站seo优化多少钱
  • 中文域名指向同一个网站交换免费连接
  • 池州网站建设费用网络营销的方式有哪些
  • www.ccb.com建设银行网站首页360手机优化大师下载
  • 西安做网站公司怎么样永久免费个人网站注册
  • 注册了域名 网站怎么做今日头条10大新闻
  • 网站制作企业首页引流推广平台软件
  • 网站服务费做管理费用谷歌排名算法
  • 分形科技做网站怎么样seo营销推广多少钱
  • 太原市给企业做网站北京营销网站制作
  • 金华网站建设域名注册网站系统
  • 做网站的主题有哪些怎样把广告放到百度
  • 根据一个网站仿做新网站是什么网站简述网站推广的意义和方法
  • 西安嵌入式培训百度网站如何优化排名
  • 家用宽带做网站服务器中国网站访问量排行
  • 做购物网站流程网推公司干什么的
  • 做网站的网页设计用cdr吗seo网站外包公司
  • 做微商网站制作网络营销研究现状文献综述
  • 郑州做音响网站的公司北京搜索引擎推广服务