C++多线程编程
来源:互联网 发布:尼特族 知乎 编辑:程序博客网 时间:2024/06/06 09:58
C++多线程编程
对全局数据线程同步
#ifndef THREAD_H#define THREAD_H#include <pthread.h>class Thread{public: Thread(); int start(); int stop(); virtual void* run(); bool join(); const pthread_t& getID()const { return ntid; } virtual ~Thread(){};private: Thread(const Thread&); static void* threadproc(void*); pthread_t ntid;};
MyThread.h
#include <pthread.h>#include "Thread.h"Thread::Thread(){}void* Thread::run(){}int Thread::start(){return pthread_create(&this->ntid,0,threadproc,static_cast<void *>(this)); }int Thread::stop(){return pthread_cancel(this->getID());}bool Thread::join(){return pthread_join(this->getID(),0);}void* Thread::threadproc(void* a_param){Thread* pthread = static_cast<Thread *>(a_param);return pthread->run();
新建MyThread类,继承Thread
MyThread.h
#ifndef MYTHREAD_H#define MYTHREAD_H#include <string>#include "Thread.h"class MyThread:public Thread{public:MyThread(const int n,const char* aName):mCount(n),mName(aName){};virtual void* run();int setCount(const int n);int setName(const char* aName);private: int mCount; std::string mName;};
MyThread.cpp
#include <string>#include <stdio.h>#include <boost/thread/thread.hpp>#include "MyThread.h"void* MyThread::run() { int sum = this->mCount*10; for(int i = 0;i < sum ;i++) { printf("%2d,the name is %s;\t\n",i,this->mName.c_str()); }}int MyThread::setCount(const int n){ this->mCount = n; return 0;}int MyThread::setName(const char* aName){ std::string aString(aName); this->mName = aString; return 0;
main.cpp
#include <stdio.h>#include "MyThread.h"int main(){ MyThread t1(15,"Thread 1"); MyThread t2(12,"Thread 2"); t1.start(); t2.start(); sleep(1); return 0;}
添加互斥锁
修改MyThread.cpp
#include <string>#include <stdio.h>#include <boost/thread/thread.hpp>#include "MyThread.h"pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//添加互斥量void* MyThread::run() { pthread_mutex_lock(&mutex); //加锁 int sum = this->mCount*10; for(int i = 0;i < sum ;i++) { printf("%2d,the name is %s;\t\n",i,this->mName.c_str()); pthread_mutex_unlock(&mutex); //解锁}int MyThread::setCount(const int n){ this->mCount = n; return 0;}int MyThread::setName(const char* aName){ std::string aString(aName); this->mName = aString; return 0;}
对私有数据线程同步
C++封装的概念使不同对象之间的私有数据不会交错,但是在遇见多线程的情况下,这些并不表明私有数据不需要加锁,因为可能涉及到类中不同的方法在同时访问或者修改数据。
main.cpp文件修改
#include <stdio.h>#include "MyThread.h"int main(){ MyThread t1(150,"Thread 1"); //MyThread t2(12,"Thread 2"); t1.start(); t1.setCount(12); //t2.start(); sleep(1); return 0; }
MyThread.cpp文件中run函数进行修改
#include <string>#include <stdio.h>#include <boost/thread/thread.hpp>#include "MyThread.h"void* MyThread::run() { for(int i = 0;i < this->mCount*10000 ;i++){printf("%2d,the name is %s;\t\n",i,this->mName.c_str());}}int MyThread::setCount(const int n){ this->mCount = n; return 0;}int MyThread::setName(const char* aName){ std::string aString(aName); this->mName = aString; return 0;}
为了解决上述问题,我们对MyThread.h MyThread.cpp以及main.cpp进行修改
MyThread.h
#ifndef MYTHREAD_H#define MYTHREAD_H#include <string>#include "Thread.h"class MyThread:public Thread{public:MyThread(const int n,const char* aName):mCount(n),mName(aName){ pthread_mutex_init(&mutex,NULL);}virtual void* run();int setCount(const int n);int setName(const char* aName);private: int mCount; std::string mName; pthread_mutex_t mutex;};
MyThread.cpp
#include <string>#include <stdio.h>#include <boost/thread/thread.hpp>#include "MyThread.h"void* MyThread::run() { pthread_mutex_lock(&mutex); //int sum = this->mCount*10; for(int i = 0;i < this->mCount*10000 ;i++) { printf("%2d,the name is %s;\t\n",i,this->mName.c_str()); } pthread_mutex_unlock(&mutex);}int MyThread::setCount(const int n){ pthread_mutex_lock(&mutex); this->mCount = n; pthread_mutex_unlock(&mutex); return 0;}int MyThread::setName(const char* aName){ std::string aString(aName); this->mName = aString;}
main.cpp
#include <stdio.h>#include "MyThread.h"int main(){ MyThread t1(150,"Thread 1"); t1.start(); sleep(1); t1.setCount(12); t1.start(); return 0;}
C++多线程编程注意事项
- 线程调用到函数在一个类中,那必须将该函数声明为静态函数函数。因为静态成员函数属于静态全局区,线程可以共享这个区域,故可以各自调用。
- 线程调用过程中如何传入参数
- 互斥锁的实现,全局变量互斥,私有成员变量互斥
0 0
- c语言多线程编程
- C#:多线程编程探索
- linux c 多线程编程
- Linux C多线程编程
- C/C++ 多线程编程
- C语言多线程编程
- Linux C 多线程编程
- linux-C编程-多线程
- Linux c 多线程编程
- C多线程编程
- 【C++】:C++多线程编程
- Linux C多线程编程
- C/C++多线程编程
- Linux C 多线程编程
- [Linux C编程]多线程
- C多线程编程-互斥量
- C多线程编程
- [Linux C编程]多线程编程
- 各种语言中字符串转换为数字 数字转换为字符串
- 当JDK的安装目录包含空格时,如何在命令行编译代码
- Ubuntu Linux 下 Ffmpeg 及 Mencoder 安装使用小结
- android:visibility="gone"
- iOS插件化研究之一——JavaScriptCore
- C++多线程编程
- [www.infoshare.cc]解决5037端口被占据问题
- java中形参与实参
- Jacoco是一个Java代码覆盖率统计工具
- 不求甚解
- Android app设计须知
- Linux网络应用编程之VLAN(Packet Tracer仿真)
- 操作xml数据
- sizeof与类,继承,virtual的种种(整理)