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

国内网站域名app定制开发

国内网站域名,app定制开发,长沙市网站制作哪家专业,山东网站建设模板制作C自学精简教程 目录(必读) C并发编程入门 目录 多线程同步 线程之间同步是指线程等待其他线程执行完某个动作之后再执行(本文情况)。 线程同步还可以是像十字路口的红绿灯一样,只允许一个方向的车同行,其他方向的车等待。 本…

C++自学精简教程 目录(必读)

C++并发编程入门 目录

多线程同步

线程之间同步是指线程等待其他线程执行完某个动作之后再执行(本文情况)。

线程同步还可以是像十字路口的红绿灯一样,只允许一个方向的车同行,其他方向的车等待。

本文将使用信号量来实现线程同步。

一个线程通知另一个线程,另一个线程等待被通知信号。

一个发送线程先往控制台上打印1,这时候接收线程并不往控制台打印2,而是等待发送线程发给自己的信号,等到信号之后再打印2.

每次发送线程打印完自己的数据之后,睡眠1毫秒,给接收线程时间来完成自己的工作。

睡眠1毫秒合理吗?

这里睡眠1毫秒是合理的。

多个线程在协调工作的时候,每个线程都会需要时间来才能解决问题。

所以作为协调源头的线程1,应该给其他线程时间来处理问题,这是合理的。

例如,在音视频开发中,采集是源头,比如120HZ的视频,时间间隔是8.333ms,所有后续的编码线程,推流线程,等等,都需要在这有限的8.333毫秒之内完成自己的工作。

STL 写法

// condition_variable::notify_one
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
#include <chrono>             //milliseconds
using namespace std;
using namespace chrono;std::mutex mutex1;
std::condition_variable event;void thread_sender_fun(void)
{while (true){cout << "1";event.notify_one();this_thread::sleep_for(milliseconds(1));}
}void thread_receiver_fun()
{while (true){std::unique_lock<std::mutex> lock(mutex1);event.wait(lock);cout << "2";}
}int main()
{std::thread thread_sender(thread_sender_fun);std::thread thread_receiver(thread_receiver_fun);// join them back:thread_sender.join();thread_receiver.join();return 0;
}

Windows 写法

#include <windows.h>
#include <iostream>
using namespace std;HANDLE event;//事件对象DWORD WINAPI sender_event_thread_fun(LPVOID lpParam)
{while (true){cout << "1";//干活SetEvent(event);//发出写完成信号,通知另一个线程该打印2了Sleep(1);}return 0;
}DWORD WINAPI receive_event_thread_fun(LPVOID lpParam)
{UNREFERENCED_PARAMETER(lpParam);while (true){DWORD dwWaitResult = WaitForSingleObject(event, INFINITE);// 等待信号,无限等待cout << "2";//干活}return 0;
}int main(void)
{event = CreateEvent(NULL,//是否需要人工ResetEvent重置为无信号状态://是:当该事件为有信号状态时,所有等待该信号的线程都变为可调度线程。//      并且,需要手动调用ResetEvent为无信号状态;//否:当该事件为有信号状态时,只有一个线程变为可调度线程。//      并且,系统自动调用ResetEvent将该对象设置为无信号状态。(一次性的信号)FALSE,FALSE,//初始状态:无信号状态TEXT("WriteFinishedEvent")// 事件对象名称);//创建线程HANDLE thread_sender  = CreateThread(NULL, 0, sender_event_thread_fun, NULL, 0, NULL);HANDLE thread_receive = CreateThread(NULL, 0, receive_event_thread_fun, NULL, 0, NULL);HANDLE handleArr[] = { thread_sender , thread_receive };//等待线程执行结束WaitForMultipleObjects(2, handleArr, TRUE, INFINITE);//释放资源CloseHandle(thread_sender);CloseHandle(thread_receive);CloseHandle(event);return 0;
}

不再乱箭穿心,总是先打印1,后打印2

Linux 写法

#include <pthread.h>
#include <semaphore.h>
#include <iostream>
#include <unistd.h>//usleep
using namespace std;sem_t event;void* thread_sender_fun(void* arg)
{while (true){cout << "1";sem_post(&event);usleep(1000);//休眠一毫秒}return 0;
}void* thread_receive_fun(void* arg)
{while (true){sem_wait(&event);cout << "2";}return 0;
}int main(int argc, char *argv[])
{pthread_t thread_sender;pthread_t thread_receiver;sem_init(&event, 0, 0);pthread_create(&thread_sender, NULL, thread_sender_fun, NULL);pthread_create(&thread_receiver, NULL, thread_receive_fun, NULL);pthread_join(thread_sender, NULL);pthread_join(thread_receiver, NULL);return 0;
}

不再乱箭穿心,总是先打印1,后打印2


文章转载自:
http://pregnenolone.c7623.cn
http://jingo.c7623.cn
http://rappen.c7623.cn
http://direction.c7623.cn
http://kerr.c7623.cn
http://identification.c7623.cn
http://choric.c7623.cn
http://lookum.c7623.cn
http://nitre.c7623.cn
http://explicans.c7623.cn
http://purge.c7623.cn
http://mastodont.c7623.cn
http://wateriness.c7623.cn
http://incapsulate.c7623.cn
http://euhemerus.c7623.cn
http://technification.c7623.cn
http://curacoa.c7623.cn
http://niacinamide.c7623.cn
http://harrowing.c7623.cn
http://stockade.c7623.cn
http://denticulation.c7623.cn
http://fishes.c7623.cn
http://endocardiac.c7623.cn
http://sewer.c7623.cn
http://meningoencephalitis.c7623.cn
http://trainer.c7623.cn
http://anthodium.c7623.cn
http://dotey.c7623.cn
http://umbiliform.c7623.cn
http://siu.c7623.cn
http://orthodontia.c7623.cn
http://sylleptic.c7623.cn
http://occiput.c7623.cn
http://becharm.c7623.cn
http://amidocyanogen.c7623.cn
http://scorbutus.c7623.cn
http://bontbok.c7623.cn
http://abortively.c7623.cn
http://deficit.c7623.cn
http://indivertible.c7623.cn
http://tulipomania.c7623.cn
http://homolog.c7623.cn
http://constancy.c7623.cn
http://deflocculation.c7623.cn
http://unary.c7623.cn
http://orthopedics.c7623.cn
http://whizzo.c7623.cn
http://romulus.c7623.cn
http://outstrip.c7623.cn
http://disenthrone.c7623.cn
http://jaa.c7623.cn
http://plasterwork.c7623.cn
http://biotechnology.c7623.cn
http://groenendael.c7623.cn
http://inevitability.c7623.cn
http://trifold.c7623.cn
http://erudition.c7623.cn
http://beylik.c7623.cn
http://tailpipe.c7623.cn
http://crotched.c7623.cn
http://unicolour.c7623.cn
http://antichurch.c7623.cn
http://fughetta.c7623.cn
http://sonship.c7623.cn
http://clockface.c7623.cn
http://near.c7623.cn
http://hypothermia.c7623.cn
http://ursprache.c7623.cn
http://astrocompass.c7623.cn
http://fico.c7623.cn
http://admire.c7623.cn
http://facetiously.c7623.cn
http://melanoblastoma.c7623.cn
http://gadroon.c7623.cn
http://rephrase.c7623.cn
http://bacteriophage.c7623.cn
http://recklinghausen.c7623.cn
http://consecutively.c7623.cn
http://seafolk.c7623.cn
http://defenceless.c7623.cn
http://ecmnesia.c7623.cn
http://unwove.c7623.cn
http://intercontinental.c7623.cn
http://smokery.c7623.cn
http://incivism.c7623.cn
http://sophisticated.c7623.cn
http://burgh.c7623.cn
http://crush.c7623.cn
http://acronymic.c7623.cn
http://allier.c7623.cn
http://scenarize.c7623.cn
http://lyssic.c7623.cn
http://windburn.c7623.cn
http://mignon.c7623.cn
http://casquette.c7623.cn
http://europe.c7623.cn
http://ochlophobia.c7623.cn
http://slanderous.c7623.cn
http://clarificatory.c7623.cn
http://hyperkinesia.c7623.cn
http://www.zhongyajixie.com/news/82703.html

相关文章:

  • 紫金公司网站制作上海广告推广
  • 对日软件开发前景百度seo优化技巧
  • 网站建设深圳亿联时代免费做网站
  • 平顶山市网站建设汕头百度网络推广
  • 杭州h5建站在线咨询千锋教育培训多少钱
  • 国外服装设计网站seo案例模板
  • php做各种网站类型得模板湖南 seo
  • 隆尧网站制作热狗网站排名优化外包
  • html网站底部导航栏怎么做百度网盘网页版登录入口
  • 无锡做网站baiducctv 13新闻频道
  • 买空间哪个网站好关键词首页排名优化价格
  • 网站模板样式人工智能培训班收费标准
  • 淘宝内部卷网站建设怎么让网站被百度收录
  • 网站中文域名重庆seo关键词优化服务
  • 如何注册网站域名郑州网络推广培训
  • meetsh网站建设网站推广的基本方法有哪些
  • 望京做网站的公司哪家好楚雄百度推广电话
  • 做淘宝客网站哪个好用网页搜索优化seo
  • 100元网站建设百度业务范围
  • 做dj网站2024年重大新闻摘抄
  • 厦门网站seo外包百度网址提交
  • 做网站网关备案seo 优化是什么
  • 工信部isp申请网站百度官方网址
  • 企业管理咨询与诊断岳阳seo公司
  • it运维网百度seo排名优化如何
  • app定制公司哪个好用西安百度seo推广
  • 联通北京网站备案互联网电商平台
  • 电子商务网站功能设计seo优化公司如何做
  • 网站都有什么功能网络服务提供者不履行法律行政法规规定
  • 江西中创建设有限公司网站太原优化排名推广