C++并发类成员函数调用(练习1)

一般类成员函数开线程格式 std::thread t1(&类名::函数,&实例化对象,参数....) ||std::thread t1(std::bind(&&类名::函数,&实例化对象,参数....))

 1 #include <iostream>
 2 #include <mutex>
 3 #include <thread>
 4 #include <vector>
 5 #include <string>
 6 #include <condition_variable>
 7 #include <future>
 8
 9
10 template<typename T>
11 class vector_safe
12 {
13 public:
14     vector_safe():ready(false),proccess(false){}
15 public:
16     void push(T& value)
17     {
18         std::unique_lock<std::mutex> m_guard(mut);
19         vec.push_back(value);
20         ready=true;
21         m_guard.unlock();
22         condit_va.notify_one();
23     }
24     void get_and_pop(T& value)
25     {
26         std::unique_lock<std::mutex> m_guard(mut);
27         condit_va.wait(m_guard,[this](){return ready;});//wait-------线程完毕数据;
28
29         value=vec.front();
30         vec.pop_back();
31         proccess=true;
32          m_guard.unlock();
33
34     }
35 private:
36     std::vector<T> vec;
37     std::mutex mut;
38     std::condition_variable condit_va;
39     bool ready;
40     bool proccess;
41
42 };
43 vector_safe<std::string> obj;
44
45 int main()
46 {
47
48         std::string str1("this is dream");
49         std::string str2("没覆盖?");
50     std::thread t1(std::bind(&vector_safe<std::string>::push,&obj,std::ref(str1)));
51     std::thread t2(std::bind(&vector_safe<std::string>::get_and_pop,&obj,std::ref(str2)));
52         t1.join();
53         t2.join();
54         std::cout<<str2<<std::endl;
55         return 0;
56 }
运行结果:
xcode 7.2
this is dream
Program ended with exit code: 0
时间: 2024-08-10 17:01:35

C++并发类成员函数调用(练习1)的相关文章

C++语言学习(十四)——C++类成员函数调用分析

C++语言学习(十四)--C++类成员函数调用分析 一.C++成员函数 1.C++成员函数的编译 C++中的函数在编译时会根据命名空间.类.参数签名等信息进行重新命名,形成新的函数名.函数重命名的过程通过一个特殊的Name Mangling(名字编码)算法来实现.Name Mangling算法是一种可逆的算法,既可以通过现有函数名计算出新函数名,也可以通过新函数名逆向推导出原有函数名.Name Mangling算法可以确保新函数名的唯一性,只要命名空间.所属的类.参数签名等有一个不同,那么产生的

类成员函数调用delete this会发生什么呢?

有如下代码 class myClass { public: myClass(){}; ~myClass(){}; void foo() { delete this; } }; int main() { myClass * aa = new myClass(); aa->foo(); return 0; } 会发生什么呢? 在类的成员函数中能不能调用delete this?答案是肯定的,能调用,而且很多老一点的库都有这种代码.假设这个成员函数名字叫release,而delete this就在这个r

派生类成员函数调用时 error C2248: 无法访问 protected 成员 的排雷之路

工作需要自定义一个控件,结果调用成员函数总是报错!如下图:理论上继承关系和函数权限public都没有问题,可是只要一使用自定义的成员函数就会报error C2248: 无法访问 protected 成员 ,查了很久资料,总算试出了解决方案,就是加强制声明public:即在成员函数声明时强制在前面加public:,对于上例就是在自定义SetTextEx前面加.如图:原因分析:语法上没有问题就可能是编译器编译时默认给继承类的成员函数都默认加了protected 的属性,虽然是放在public区域,可

C++类成员空间分配和虚函数表

最近在自学python,看到继承和类,就顺便复习了C++的类和继承等方面的知识. 先看Base基类 class Base { private: virtual void display() { cout<<"Base display()"<<endl; } void say(){ cout<<"Base say()"<<endl; } public: virtual void func(){cout <<

使用类作用域操作符进行成员函数调用问题

#include "stdafx.h" #include <iostream> using namespace std; class base { public: int func() { return 100; } private: }; class derived:public base { public: int func() { int tem; tem=base::func(); //这样做是可以的, 显式调用基类中的方法(不然无限递归调用) cout<&l

基类与派生类的指针和成员函数调用原理

基类与派生类的指针和成员函数调用原理 1.如果以一个基础类指针指向一个衍生类对象(派生类对象),那么经由该指针只能访问基础类定义的函数(静态联翩) 2.如果以一个衍生类指针指向一个基础类对象,必须先做强制转型动作(explicit cast),这种做法很危险,也不符合生活习惯,在程序设计上也会给程序员带来困扰.(一般不会这么去定义) 3.如果基础类和衍生类定义了相同名称的成员函数(非虚函数),那么通过对象指针调用成员函数时,到底调用哪个函数要根据指针的类型(基类指针or派生类指针)来确定,而不是

并发编程: c++11 thread(Func, Args...)利用类成员函数创建线程

c++11是VS2012后支持的新标准,为并发编程提供了方便的std::thread. 使用示例: #include <thread> void thread_func(int arg1, int arg2, float* arg3){ arg3 = (arg1*1.0)/(arg1 + arg2); cout << "arg1 / (arg1 + arg2) = " << arg3 << endl; return; } void mai

C++ 获取类成员函数地址方法 浅析

C语言中可以用函数地址直接调用函数: void print () { printf ("function print"); } typdef void (*fun)(); fun f = print; f(); C++中类非静态成员函数必须通过实例去调用,C++中类成员函数调用: class test { public: void print () { printf ("function print"); } }; 我们同样可以通过定义函数指针来调用如下: type

类成员函数可以为回调函数吗

关于类成员函数是否可以成为回调函数,我们首先需要明确几个定义,1. 什么是回调函数 2. 为什么要使用回调函数 3. 调用普通类成员函数和调用回调函数有什么区别 什么是回调函数? 简而言之,回调函数就是一个通过函数指针调用的函数,如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数 为什么要使用回调函数? 因为可以把调用者与被调用者分开.调用者不关心谁是被调用者,所有它需知道的,只是存在一个具有某种特定原型.某些限制条件(如返回值为int