Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17608
  • 博文数量: 34
  • 博客积分: 990
  • 博客等级: 准尉
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-31 20:57
文章分类
文章存档

2012年(8)

2011年(26)

我的朋友
最近访客

分类: C/C++

2011-09-05 10:37:02


#include
#include
using namespace std;

//const int MAX_THREAD_NUMBER = (20);
//int nArr[MAX_THREAD_NUMBER] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
const int MAX_THREAD_NUMBER = (3);
int nArr[MAX_THREAD_NUMBER] = {0, 1, 2};

pthread_t hThreadArr[MAX_THREAD_NUMBER];


pthread_cond_t hEventArr[MAX_THREAD_NUMBER];
pthread_mutex_t hEventArrLock[MAX_THREAD_NUMBER];

pthread_cond_t hManager;
pthread_mutex_t hManagerLock;

pthread_cond_t hAction;
pthread_mutex_t hActionLock;

pthread_mutex_t cs;
//function declear
bool InitEvent();
bool StartThread();
bool Action();
bool ThreadManager();
void TellManager(int nThreadID);
void* ThreadProc(void* lpParameter);

//function declear end

bool InitEvent()
{
    bool bRet = true;
    hManager = PTHREAD_COND_INITIALIZER;
    bRet = (0 == pthread_mutex_init(&hManagerLock, NULL)) ? true:false;

    if(bRet)
    {
        hAction = PTHREAD_COND_INITIALIZER;
        bRet = (0 == pthread_mutex_init(&hActionLock, NULL)) ? true:false;
    }

    if(bRet)
    {
        bRet = (0 == pthread_mutex_init(&cs, NULL)) ? true:false;
    }

    for (int i = 0; i < MAX_THREAD_NUMBER && bRet; i++)
    {
        hEventArr[i] = PTHREAD_COND_INITIALIZER;
        bRet = (0 == pthread_mutex_init(&hEventArrLock[i], NULL)) ? true:false;
    }
    return bRet;
}

bool StartThread()
{
    bool bRet = true;
    for (int i = 0; i < MAX_THREAD_NUMBER; i++)
    {
        int err;
        err = pthread_create(&hThreadArr[i], NULL, ThreadProc, &nArr[i]);
        
    }
    return bRet;
}

bool Action()
{
    bool bSetSuccess = true;
    cout << "action" << endl;
    for (int i = 0; i < MAX_THREAD_NUMBER && bSetSuccess; i++)
    {
        pthread_mutex_lock(&hEventArrLock[i]);
        pthread_cond_signal(&hEventArr[i]);
        pthread_mutex_unlock(&hEventArrLock[i]);
    }
    return bSetSuccess;
}

bool ThreadManager()
{
    bool bRet = false;
    do
    {
        bRet = InitEvent();
        if (!bRet)
        {
            break;
        }

        bRet = StartThread();
        if (!bRet)
        {
            break;
        }

        //wait for 10 sig
        for (int i = 0; i < MAX_THREAD_NUMBER; i++)
        {
            pthread_mutex_lock(&hManagerLock);
            cout << "manager wait" << i << endl;
            pthread_cond_wait(&hManager, &hManagerLock);
            //here can do some code
            pthread_mutex_unlock(&hManagerLock);
            //
            pthread_mutex_unlock(&cs);
        }

        bRet = Action();
        if (!bRet)
        {
            break;
        }

    } while (false);
    return bRet;
}

void TellManager(int nThreadID)
{
    cout << "tell manager" << nThreadID << endl;
    pthread_mutex_lock(&cs);
    pthread_mutex_lock(&hManagerLock);
    pthread_cond_signal(&hManager);
    pthread_mutex_unlock(&hManagerLock);
    //pthread_mutex_unlock(&cs);
}

void* ThreadProc(void* lpParameter)
{
    int nThreadId = *(static_cast(lpParameter));
    cout << "ThreadProc" << nThreadId << endl;
    while(1)
    {
        TellManager(nThreadId);
        pthread_mutex_lock(&hEventArrLock[nThreadId]);
        cout << "ThreadProc wait" << nThreadId << endl;
        pthread_cond_wait(&hEventArr[nThreadId], &hEventArrLock[nThreadId]);
        //here can place read file code
        cout << nThreadId << endl;
        pthread_mutex_unlock(&hEventArrLock[nThreadId]);
    }
    return NULL;
}

int main()
{
    ThreadManager();
    while(1)
    {
        ;
    }
    return 0;
}
阅读(166) | 评论(0) | 转发(0) |
0

上一篇:九种Windows时间函数

下一篇:yue.cpp_Update

给主人留下些什么吧!~~