搜狐三季度营收多少,网站关键词优化多少钱,dedecms网站版权信息,wordpress saeC自学精简教程 目录(必读)
C并发编程入门 目录
多线程同步
线程之间同步是指线程等待其他线程执行完某个动作之后再执行#xff08;本文情况#xff09;。
线程同步还可以是像十字路口的红绿灯一样#xff0c;只允许一个方向的车同行#xff0c;其他方向的车等待。
本…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_lockstd::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://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn