
多线程
多线程项目经历与学习笔记
清欢_小铭
手机厂从事嵌入式软件开发,985小硕。
嵌入式Linux、围绕C/C++、单片机裸机、RTOS。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
多线程+链表模拟RR调度算法
程序还有点小bug mian.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <sys/types.h> #include <sys/time.h> #include <stdlib.h.原创 2021-12-19 22:08:21 · 526 阅读 · 0 评论 -
多线程实现字符串翻转
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> //https://www.cnblogs.com/chenjx85/p/10574752.html int rows=0,idx=1,len; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; void* my_func(void* args) {..原创 2022-05-13 22:20:21 · 169 阅读 · 0 评论 -
c++多线程——简单线程池
安全队列 #include <thread> #include <iostream> #include <atomic> #include <functional> #include <vector> #include "tsafe_queue.h" using namespace std; class join_t { vector<thread>& threads; public: explicit join原创 2021-10-02 12:37:42 · 154 阅读 · 0 评论 -
c++多线程——基于锁和条件变量的前程安全队列
实现了一个模板类。 #pragma once #include <exception> #include <thread> #include <mutex> #include <queue> #include <utility> #include <condition_variable> #include <memory> using namespace std; template<typename T> cla原创 2021-10-02 11:52:19 · 130 阅读 · 0 评论 -
基于栈的快排模板
线程安全栈 promise的用法 list::splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list。 boost线程中的yield方法:可以将本线程的CPU时间片放弃,并允许其他线程运行。 如果你想直接告诉编译器 T::const_iterator 是类型而不是变量,只需用 typename修饰。 c++ partition #include <iostream> #include <list> #include <future>原创 2021-09-30 18:52:54 · 97 阅读 · 0 评论 -
c++多线程——同步并发
条件变量 c++库提供两个条件变量的实现,std::condition_variable和std::condition_variable_any,二者均在<condition_variable>头中,二者需要和互斥元一起工作,前者要和mutex,后者和任意符合互斥元标准的对象。 当 std::condition_variable对象的wait 函数被调用的时候,它使用 std::unique_lock(通过 std::mutex) 来锁住当前线程。当前线程会一直被阻塞,直到另外一个线程在相同的原创 2021-09-29 20:12:35 · 133 阅读 · 0 评论 -
c++多线程——数据共享
互斥元 在读写数据前锁定,读写后解锁。 #include <mutex> std::mutex m1; m1.lock(); m1.unlock(); 这样做有点麻烦,不符合面向对象的风格和RAII(资源获取就是初始化)的思路。c++、提供了std::lock_guard模板,在构造时会锁定传入的互斥元,析构时解锁相应的互斥元。 #include <mutex> std::mutex m1; void add(){ std::lock_guard<std::mutex &g原创 2021-09-29 19:36:17 · 651 阅读 · 0 评论 -
c++多线程——线程启动
线程创建 基础版本 需要构造一个std::tread对象,传入参数为任何可调用对象。 #include <iostream> #include <thread> using namespace std; void do_sth() { cout << "start do_sth"; } int main() { std::cout << "Hello World!\n"; thread t1(do_sth); return 0原创 2021-09-29 10:10:56 · 1718 阅读 · 0 评论 -
多线程依次接收信息并发送
写的不成功,各位有更好的方法请留言 // https://blog.youkuaiyun.com/c_base_jin/article/details/89741247 #include <Windows.h> #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex,原创 2021-05-23 22:02:57 · 145 阅读 · 0 评论 -
多线程矩阵乘法运算 c++
问题 第一题,第二问一个4m×4m的方阵与一个4×1的列向量相乘,要求开4个线程运算。矩阵用随机数填充。 代码 四个线程要访问的数据没有重合,不需要加锁,加锁反而就不是并行处理了。 线程初始化后,要等待四个线程都运行结束,计算才全部结束。 #include <iostream> #include <thread> #include <mutex> #include <random> using namespace std; constexpr aut原创 2021-05-23 20:09:13 · 835 阅读 · 0 评论 -
模拟任务调度算法 C语言 【留学生作业】
需求 用四种方法模拟进程调度,分别是: FCFS (Non-preemptive)—— 先来先处理(非抢占) Shortest Job First (Non-preemptive)——最短任务优先(非) Shortest Remaining Time First (Preemptive)——剩余最短优先(抢占) Round Robin : Time Quantum = 2 (Preemptive)——时间片轮转(抢) 已经给出主函数,main.c #include <stdio.h> #in原创 2021-05-18 10:12:06 · 676 阅读 · 0 评论 -
多线程模拟渡河 C语言 Linux
题目 Imagine that you need to transfer N people from one riverbank to another using a boat that holds a maximum of 3 people. Write a program that would model this situation. Each person should be represented as a thread(use Pthread library) and the boat as a原创 2021-03-19 22:23:06 · 318 阅读 · 0 评论