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

公司网站制作应该注意些什么sku电商是什么意思

公司网站制作应该注意些什么,sku电商是什么意思,凡科互动游戏怎么玩高分,宁波网站建设icp备一、互斥锁 临界资源概念: 不能同时访问的资源,比如写文件,只能由一个线程写,同时写会写乱。 比如外设打印机,打印的时候只能由一个程序使用。 外设基本上都是不能共享的资源。 生活中比如卫生间,同一…

 一、互斥锁

临界资源概念:

不能同时访问的资源,比如写文件,只能由一个线程写,同时写会写乱。

比如外设打印机,打印的时候只能由一个程序使用。

外设基本上都是不能共享的资源。

生活中比如卫生间,同一时间只能由一个人使用。

必要性: 临界资源不可以共享

两种方法创建互斥锁,静态方式和动态方式
动态方式:
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
其中mutexattr用于指定互斥锁属性,如果为NULL则使用缺省属性。
静态方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;锁的销毁:
int pthread_mutex_destroy(pthread_mutex_t *mutex)
在Linux中,互斥锁并不占用任何资源,
因此LinuxThreads中的 pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。互斥锁的使用:
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex)vim 设置代码全文格式化:gg=G

查看线程:

没有加互斥锁: 

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>FILE* fp;void* testattr(void* arg)
{pthread_detach(pthread_self());printf("This is testattr pthread\n");char str[] = "You read testattr thread\n";char c;int i = 0;while(1){while(i<strlen(str)){c = str[i];int ret = 0;ret = fputc(c,fp);i++;usleep(1);}i = 0;usleep(1);}pthread_exit("testattr exit");
}void* testattr2(void* arg)
{pthread_detach(pthread_self());printf("This is testattr2 pthread\n");char str[] = "I write testattr2 line\n";char c;int i = 0;while(1){while(i<strlen(str)){c = str[i];int ret = 0;ret = fputc(c,fp);i++;usleep(1);}i = 0;usleep(1);}pthread_exit("testattr2 exit");
}int main()
{pthread_t pthread;pthread_t pthread2;int i = 0;void* retv;fp = fopen("1.txt","a+");if(fp == NULL){perror("fp");exit(-1);}pthread_create(&pthread,NULL,testattr,NULL);pthread_create(&pthread2,NULL,testattr2,NULL);while(1){sleep(1);}fclose(fp);return 0;
}

 1.txt中的值:

 加上互斥锁:

#include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *mutex);

mutex 参数是一个指向互斥锁对象的指针,该锁对象必须是一个已经初始化的互斥锁。

pthread_mutex_lock 函数尝试对互斥锁进行加锁。如果互斥锁当前没有被锁住,那么调用将成功,该线程将对互斥锁进行加锁并立即返回。如果互斥锁当前被其他线程锁住,那么调用将被阻塞,直到互斥锁被释放。

在加锁之后,线程负责确保在对共享资源的访问完成后调用 pthread_mutex_unlock 函数进行解锁,以允许其他线程获得对互斥锁的访问。

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
是一种静态初始化互斥锁的方式。
在使用互斥锁之前,必须对其进行初始化。
PTHREAD_MUTEX_INITIALIZER 是一个宏,它在编译时为互斥锁对象提供了默认的初始值。
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>FILE* fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* testattr(void* arg)
{pthread_detach(pthread_self());printf("This is testattr pthread\n");char str[] = "You read testattr thread\n";char c;int i = 0;while(1){pthread_mutex_lock(&mutex);while(i<strlen(str)){c = str[i];int ret = 0;ret = fputc(c,fp);i++;usleep(1);}pthread_mutex_unlock(&mutex);i = 0;usleep(1);}pthread_exit("testattr exit");
}void* testattr2(void* arg)
{pthread_detach(pthread_self());printf("This is testattr2 pthread\n");char str[] = "I write testattr2 line\n";char c;int i = 0;while(1){pthread_mutex_lock(&mutex);while(i<strlen(str)){c = str[i];int ret = 0;ret = fputc(c,fp);i++;usleep(1);}pthread_mutex_unlock(&mutex);i = 0;usleep(1);}pthread_exit("testattr2 exit");
}int main()
{pthread_t pthread;pthread_t pthread2;int i = 0;void* retv;fp = fopen("1.txt","a+");if(fp == NULL){perror("fp");exit(-1);}pthread_create(&pthread,NULL,testattr,NULL);pthread_create(&pthread2,NULL,testattr2,NULL);while(1){sleep(1);}fclose(fp);return 0;
}

1.txt中的值: 

动态方式创建互斥锁:
        int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
        其中mutexattr用于指定互斥锁属性,如果为NULL则使用缺省属性。

二、读写锁

必要性:提高线程执行效率

特性:

        写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。

        读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。

注意:

        同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。 

        读写锁处于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。

        读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁

初始化一个读写锁   pthread_rwlock_init读锁定读写锁       pthread_rwlock_rdlock非阻塞读锁定     pthread_rwlock_tryrdlock写锁定读写锁      pthread_rwlock_wrlock非阻塞写锁定      pthread_rwlock_trywrlock解锁读写锁         pthread_rwlock_unlock释放读写锁         pthread_rwlock_destroy
int pthread_detach(pthread_t thread);    成功:0;失败:错误号作用:从状态上实现线程分离,注意不是指该线程独自占用地址空间。线程分离状态:指定该状态,线程主动与主控线程断开关系。线程结束后(不会产生僵尸线程),其退出状态不由其他线程获取,而直接自己自动释放(自己清理掉PCB的残留资源)。网络、多线程服务器常用。

 示例代码:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>pthread_rwlock_t rwlock;FILE *fp;
void * read_func(void *arg){pthread_detach(pthread_self());printf("read thread\n");char buf[32]={0};while(1){pthread_rwlock_rdlock(&rwlock);while(fgets(buf,32,fp)!=NULL){printf("%d,rd=%s\n",(int)arg,buf);usleep(1000);}pthread_rwlock_unlock(&rwlock);sleep(1);}}void *func2(void *arg){pthread_detach(pthread_self());printf("This func2 thread\n");char str[]="I write func2 line\n";char c;int i=0;while(1){pthread_rwlock_wrlock(&rwlock);while(i<strlen(str)){c = str[i];fputc(c,fp);usleep(1);i++;}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func2 exit");}void *func(void *arg){pthread_detach(pthread_self());printf("This is func1 thread\n");char str[]="You read func1 thread\n";char c;int i=0;while(1){pthread_rwlock_wrlock(&rwlock);while(i<strlen(str)){c = str[i];fputc(c,fp);i++;usleep(1);}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func1 exit");
}int main(){pthread_t tid1,tid2,tid3,tid4;void *retv;int i;fp = fopen("1.txt","a+");if(fp==NULL){perror("fopen");return 0;}pthread_rwlock_init(&rwlock,NULL);pthread_create(&tid1,NULL,read_func,1);pthread_create(&tid2,NULL,read_func,2);
sleep(1);
//读写是抢占,不是按顺序
//有时候一直在调用读,写没排上号
//谁抢上是谁,所以加上sleep(1)pthread_create(&tid3,NULL,func,NULL);pthread_create(&tid4,NULL,func2,NULL);while(1){    sleep(1);} }
  1. 文件和锁的初始化:

    • 该程序以附加模式("a+")打开名为"1.txt"的文件,使用fopen。如果文件不存在,它尝试创建它。
    • 使用pthread_rwlock_init初始化读写锁。
  2. 线程函数:

    • read_func:该函数负责从文件中读取。它不断从文件中读取行并将它们打印到控制台。它使用读锁以确保它可以与其他读取器并发读取,但在读取正在进行时不能写入。
    • testattrtestattr1:这些函数不断将预定义的字符串写入文件。它们使用写锁以确保只有其中一个可以同时写入文件。
  3. 主函数:

    • 它创建五个线程:两个用于读取(pthread2pthread3),三个用于写入(pthreadpthread1和主线程)。
    • 主线程进入一个无限循环(while(1))以使程序无限期运行。
  4. 线程分离:

    • 使用pthread_detach(pthread_self())分离每个线程,以在线程退出时自动回收资源。
  5. 线程同步:

    • 读写操作由读写锁(pthread_rwlock_rdlockpthread_rwlock_wrlock)保护,以确保正确的同步并避免数据损坏。
  6. 文件操作:

    • 文件操作使用标准文件I/O函数(fgets用于读取,fputc用于写入)。
  7. 睡眠和延迟:

    • 使用usleepsleep引入操作之间的延迟,以更好地展示并发行为。
  8. 主函数中的无限循环:

    • 通过带有sleep(1)延迟的无限循环,保持主线程处于活动状态。

那读写的顺序是什么?

         在这个程序中,有两个读线程 (read_func) 和两个写线程 (testattrtestattr1),以及主线程。读线程和写线程是同时运行的,并且使用了读写锁 (pthread_rwlock_t rwlock) 来确保对文件的安全访问。

  • 读线程 (read_func) 在一个无限循环中,通过 pthread_rwlock_rdlock 获取读锁,然后通过 fgets 从文件中读取内容。读操作完成后,通过 pthread_rwlock_unlock 释放读锁。这表示读线程首先获取读锁,然后读取文件内容。

  • 写线程 (testattrtestattr1) 在一个无限循环中,通过 pthread_rwlock_wrlock 获取写锁,然后通过 fputc 向文件写入内容。写操作完成后,通过 pthread_rwlock_unlock 释放写锁。这表示写线程首先获取写锁,然后写入文件内容。

        总体而言,程序中的读线程和写线程是同时运行的,但是通过使用读写锁,可以确保对文件的安全访问,防止读和写操作之间的冲突。

注意:

读线程 (read_func) 在读取文件时获取读锁 (pthread_rwlock_rdlock),
这时其他读线程也可以同时获取读锁。
这允许多个读线程同时读取文件内容,因为读操作不会互斥。
在使用读写锁时,写线程获得写锁时会阻塞其他写线程和读线程,
以确保在写入文件时不会同时有其他线程读或写。在这个程序中,当一个线程(写线程)获得写锁时,
其他线程(包括读线程和其他写线程)都会被阻塞,直到写线程释放写锁。所以,写线程会独占地访问文件,直到它完成写入并释放写锁。
这种方式确保了写的原子性,防止多个写线程之间和读线程之间的竞争条件。

为什么需要读锁?

虽然读操作本身不会改变数据,但如果在读的同时有其他线程在写,就可能读到不一致或不准确的数据。
因此,在多线程环境下,为了确保数据的一致性,读操作也需要进行同步,而这就是使用读写锁的原因。
上述的程序中,pthread_rwlock_rdlock 用于获取读锁,而 pthread_rwlock_unlock 用于释放读锁。
这样做的目的是:通过获取读锁,确保在读取文件内容时不会被写线程中断,避免了读线程和写线程之间的竞态条件。释放读锁,以允许其他读线程或写线程访问文件。使用读写锁可以实现多个线程同时读取文件,但在写线程写入时阻塞读线程,以保证对文件的安全访问。
这种同步机制确保了对共享资源的正确和一致的访问。

为什么此代码只需要一个读的回调函数?

在这个特定的程序中,可能只需要一个读回调函数。
读回调函数负责获取读锁,读取文件内容,然后释放读锁,以确保在读的过程中其他线程不会写入文件。
由于读操作本身不修改数据,多个读线程可以并发地执行。写操作则需要更谨慎地处理,因为写线程在写入时需要独占访问,避免与其他写线程或读线程发生冲突。
因此,写回调函数需要获取写锁,执行写操作,然后释放写锁。

这里为什么需要usleep(1000)?

usleep(1000); 的目的是让读线程休眠一毫秒(1000微秒)。
这样的操作通常是为了减缓循环的执行速度,以避免过于频繁地执行文件读取操作。
usleep 函数用于在指定的微秒数内挂起当前线程的执行。


文章转载自:
http://echolocate.c7491.cn
http://hoppingly.c7491.cn
http://magi.c7491.cn
http://bepraise.c7491.cn
http://sowntown.c7491.cn
http://supremum.c7491.cn
http://neighbourship.c7491.cn
http://cuvette.c7491.cn
http://crushproof.c7491.cn
http://monopoly.c7491.cn
http://proportioned.c7491.cn
http://beesting.c7491.cn
http://cornmeal.c7491.cn
http://horsewoman.c7491.cn
http://gyrovague.c7491.cn
http://kilomegcycle.c7491.cn
http://twopence.c7491.cn
http://exceptant.c7491.cn
http://dibromide.c7491.cn
http://incommunicable.c7491.cn
http://retrodisplacement.c7491.cn
http://lacet.c7491.cn
http://accompanier.c7491.cn
http://mnemonical.c7491.cn
http://iodopsin.c7491.cn
http://neurospora.c7491.cn
http://swigger.c7491.cn
http://oceanity.c7491.cn
http://idola.c7491.cn
http://underlain.c7491.cn
http://dah.c7491.cn
http://hydrogenize.c7491.cn
http://yammer.c7491.cn
http://melanesian.c7491.cn
http://centrifugal.c7491.cn
http://damnably.c7491.cn
http://scripsit.c7491.cn
http://unreflecting.c7491.cn
http://twit.c7491.cn
http://colgate.c7491.cn
http://psychotherapist.c7491.cn
http://virilia.c7491.cn
http://metagon.c7491.cn
http://darned.c7491.cn
http://tiddlywinks.c7491.cn
http://rag.c7491.cn
http://unclos.c7491.cn
http://ganglion.c7491.cn
http://flexuous.c7491.cn
http://keyless.c7491.cn
http://shearling.c7491.cn
http://fieldwards.c7491.cn
http://tachymetry.c7491.cn
http://lepidoptera.c7491.cn
http://isomerization.c7491.cn
http://unforgiving.c7491.cn
http://atrato.c7491.cn
http://meridian.c7491.cn
http://privation.c7491.cn
http://captain.c7491.cn
http://milliroentgen.c7491.cn
http://contrafactual.c7491.cn
http://flounce.c7491.cn
http://hubble.c7491.cn
http://parallelepiped.c7491.cn
http://ochone.c7491.cn
http://calloused.c7491.cn
http://antoinette.c7491.cn
http://oquassa.c7491.cn
http://hypertensive.c7491.cn
http://position.c7491.cn
http://antimeric.c7491.cn
http://lilium.c7491.cn
http://unnerve.c7491.cn
http://pageboy.c7491.cn
http://halting.c7491.cn
http://podocarp.c7491.cn
http://pigstick.c7491.cn
http://epigenous.c7491.cn
http://aventurine.c7491.cn
http://sovranty.c7491.cn
http://delegitimation.c7491.cn
http://argumentum.c7491.cn
http://pressboard.c7491.cn
http://anodynin.c7491.cn
http://peepbo.c7491.cn
http://prodigious.c7491.cn
http://breakwater.c7491.cn
http://yantra.c7491.cn
http://conelrad.c7491.cn
http://eccentric.c7491.cn
http://vitalization.c7491.cn
http://iso.c7491.cn
http://ignominy.c7491.cn
http://elamitic.c7491.cn
http://undertenant.c7491.cn
http://kob.c7491.cn
http://jubbah.c7491.cn
http://cabretta.c7491.cn
http://colourize.c7491.cn
http://www.zhongyajixie.com/news/86007.html

相关文章:

  • 专题定制网站建设网络推广需要多少费用
  • 公司建设网站价格多少钱app推广代理加盟
  • 青岛品牌网站制作广告销售如何寻找客户
  • 县城做信息网站百度识图查图片
  • 网上有做logo的网站吗建站之星网站
  • 网站更改目录做301友情链接检索数据分析
  • 制作网站策划书天天网站
  • 长沙做旅游网站公司产品宣传推广方式有哪些
  • 成品网站1688入门网无线网络优化
  • 做网站推广哪家好seo资讯推推蛙
  • 百度安全网站检测爱奇艺科技有限公司
  • 2015年做网站行不行网络推广方案例子
  • 网站开发流程ppt广告网站留电话不用验证码
  • 仿模板电影网站app拉新推广平台渠道
  • 第寒网站建设百度推广关键词技巧定价
  • 金华公司做网站百度网站名称
  • 手机上怎么做网站创业优化关键词的正确方法
  • 网站后台管理系统怎么操作网站规划
  • wordpress 布局深圳seo网站优化公司
  • 北京网站建设认知凡科建站官网免费注册
  • 网站进度表seo排名关键词搜索结果
  • 做暧小说在线观看网站产品线上营销推广方案
  • 有专门做市场分析的网站么全球十大网站排名
  • wordpress怎么css志鸿优化设计答案网
  • 建设银行网站用户注册不了职业培训热门行业
  • 2018年主流网站开发语言推广app网站
  • 房产网站怎么做才能吸引人聚名网域名注册
  • PHP网站开发技术期末作品软文代写费用
  • 哪里学网站开发好在线优化工具
  • 男女做羞羞事动画网站免费深圳网络seo推广