线程池-条件变量

main.c

#include "jobBase.h"
#include "job.h"
#include "myThreadPool.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

using namespace std;
using namespace ThreadPool;
using namespace MyJob;
int main()
{
   printf("main thread %lu\n", pthread_self());
   char* threadPoolName = "xinxin's myThreadPool";
   myThreadPool myFirstThreadPool(2, threadPoolName);

   int i = 1;

   for (;; ++i)
   {
      char jobName = 'a' + i;
      IMyJob* job = new Job(&jobName, i);
      if (NULL != job)
      {
         myFirstThreadPool.addJob(job);
         sleep(4);
      }
      else
      {
         printf("%d job is NULL\n", i);
      }
   }
   printf("get pending job num %d\n", myFirstThreadPool.getPendingJobNum());

   //myFirstThreadPool.shutdown();



   return 0;
}

jobBase.h

#ifndef _JOBBASE_H
#define _JOBBASE_H
#include <stddef.h>

namespace MyJob{

class IMyJob
{
public:
   IMyJob();
   IMyJob(int data);
   IMyJob(char* name, int data);
   virtual ~IMyJob();
   virtual void executeJob() = 0;
   virtual void cancelJob() = 0;
   void setData(int data);
   int getData();

protected:
   char* m_jobName;
   int m_data;
   bool m_isRunning;
};

}// namespace MyJob

#endif

jobBase.cpp

#include "jobBase.h"


namespace MyJob{

IMyJob::IMyJob()
   : m_jobName(NULL)
   , m_data(0)
   , m_isRunning(false)
{
}

IMyJob::IMyJob(int data) 
   : m_jobName(NULL)
   , m_data(data)
   , m_isRunning(false)
{
}

IMyJob::IMyJob(char* name, int data)
   : m_jobName(name)
   , m_data(data)
   , m_isRunning(false)
{
}

IMyJob::~IMyJob()
{
   m_jobName = NULL;
   m_data = 0;
}

void IMyJob::setData(int data)
{
   m_data = data;
}

int IMyJob::getData()
{
   return m_data;
}
}// namespace MyJob

job.h

#ifndef JOB_H
#define JOB_H

#include "jobBase.h"

namespace MyJob{

class Job: public IMyJob
{
public:
   Job(char* name, int data);
   virtual ~Job();
   virtual void executeJob();
   virtual void cancelJob();
};

}//namespace MyJob

#endif

job.cpp

#include "job.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

namespace MyJob{

Job::Job(char* name, int data)
   : IMyJob(name, data)
{
}

Job::~Job()
{
   m_data = 0;
}

void Job::executeJob()
{
   m_isRunning = true;
   printf("this is thread %lu, job %d\n", pthread_self(), m_data);
   sleep(10);
   printf("this is thread %lu, job %d done\n", pthread_self(), m_data);
}

//event to cancel job
void Job::cancelJob()
{
}

}//namespace MyJob

myThreadPool.h

#ifndef myThreadPool_H
#define myThreadPool_H

#include "jobBase.h"
#include <vector>
#include <queue>
#include <pthread.h>
#include <stdio.h>
using namespace std;
using namespace MyJob;

namespace ThreadPool{

class myThreadPool
{
public:
   myThreadPool(int threadNum, char* name);
   virtual ~myThreadPool();
   bool createThreads(int m_threadNum);
   static void* threadFunction(void* threadData);
   bool addJob(IMyJob* job);
   bool cancelJob(IMyJob* job);
   bool shutdown();
   int getPendingJobNum();

private:
   queue<IMyJob*> m_JobList;
   int m_threadNum;
   char* m_nameOfmyThreadPool;
   vector<pthread_t> m_threadList;
   pthread_mutex_t m_mutex;
   pthread_cond_t m_condition;
   bool m_shutdown;
};

}// namespace ThreadPool
#endif

myThreadPool.cpp

#include "myThreadPool.h"
#include <pthread.h>
#include <stddef.h>

namespace ThreadPool{

myThreadPool::myThreadPool(int threadNum, char* name)
   : m_threadNum(threadNum)
   , m_nameOfmyThreadPool(name)
   , m_mutex(PTHREAD_MUTEX_INITIALIZER)
   , m_condition(PTHREAD_COND_INITIALIZER)
   , m_shutdown(false)
{
   printf("this %s myThreadPool is strating...\n", name);
   if(!createThreads(m_threadNum))
   {
      printf("create threads error\n");
   }
}

myThreadPool::~myThreadPool()
{
   printf("this %s myThreadPool is destroying...\n", m_nameOfmyThreadPool );
   pthread_mutex_destroy(&m_mutex);
   pthread_cond_destroy(&m_condition);
}

bool myThreadPool::createThreads(int m_threadNum)
{
   m_threadList.resize(m_threadNum);
   for(int i = 0; i < m_threadNum; ++i)
   {
      pthread_create(&m_threadList[i], NULL, &threadFunction, (void *)this);
   }
   return true;
}

void* myThreadPool::threadFunction(void* threadData)
{
   myThreadPool *pThreadPool = (myThreadPool *)threadData;
   if (NULL == pThreadPool)
   {
      printf("empty threadPool ptr\n");
      return (void*)0;
   }
   pthread_t tid = pthread_self();
   printf("this is thread: %lu running\n", tid);
   while(!pThreadPool->m_shutdown)
   {
      pthread_mutex_lock(&(pThreadPool->m_mutex));
      while (pThreadPool->m_JobList.empty() && !pThreadPool->m_shutdown)
      {
         pthread_cond_wait(&(pThreadPool->m_condition), &(pThreadPool->m_mutex));
      }

      MyJob::IMyJob* runningJob = pThreadPool->m_JobList.front();
      if (runningJob && !pThreadPool->m_shutdown)
      {
         pThreadPool->m_JobList.pop();
         pthread_mutex_unlock(&(pThreadPool->m_mutex));
         printf("still %ld jobs\n", pThreadPool->m_JobList.size());
         runningJob->executeJob();
      }
      else
      {
         printf("joblist is empty\n");
         pthread_mutex_unlock(&(pThreadPool->m_mutex));
      }



      if (pThreadPool->m_shutdown)
      {
         printf("this thread: %lu will exit\n", tid);
         pthread_exit(NULL);
      }
   }
   return (void*)0;
}

bool myThreadPool::addJob(IMyJob* job)
{
   if (NULL == job)
   {
      printf("the job is wrong\n");
      return false;
   }

   pthread_mutex_lock(&m_mutex);
   printf("add job %d\n", job->getData());
   m_JobList.push(job);
   pthread_mutex_unlock(&m_mutex);
   pthread_cond_signal(&m_condition);

   return true;
}

bool myThreadPool::cancelJob(MyJob::IMyJob* job)
{
   return false;
}

bool myThreadPool::shutdown()
{
   if (false == m_shutdown)
   {
      m_shutdown = true;
      printf("wait for shutdowning\n");
   }

   pthread_cond_broadcast(&m_condition);
   for (int i = 0; i < m_threadNum; ++i)
   {
      pthread_join(m_threadList[i], NULL);
   }

   return true;
}

int myThreadPool::getPendingJobNum()
{
   return m_JobList.size();
}

}// namespace ThreadPool

在写和调试的过程中发现了很多问题:

1.写代码方面,需要注意:
中英文符号不要混用;
变量名或函数名的拼写;
函数返回值的检查;
函数参数类型的检查;
头文件是否添加;
静态成员函数只能访问静态成员变量;
静态成员变量需要全部在声明对应的源文件里初始化;
C++中的静态成员函数可以转换为C语言里的函数指针, 若静态成员函数想访问非静态成员变量,可以传入this指针给该静态成员函数;
学习参考:
https://www.cnblogs.com/wkfvawl/p/10834549.html

2.对条件变量及死锁的理解:
在运行时,一开始发生了只有一个线程在run job的情况;
后来发现是加锁的范围太大,导致其他线程抢不到锁;
改小了加锁的力度后,解决了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值