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++多线程编程注意事项
- 线程调用到函数在一个类中,那必须将该函数声明为静态函数函数。因为静态成员函数属于静态全局区,线程可以共享这个区域,故可以各自调用。
- 线程调用过程中如何传入参数
- 互斥锁的实现,全局变量互斥,私有成员变量互斥