Example #1
0
bool Thread::start()
{	
	int res;
	if(point != NULL){
//#ifdef WIN32
//		DWORD dwThreadId, dwThrdParam = 1; 
//
//		HANDLE pThread = CreateThread( 
//			NULL,                        // no security attributes 
//			0,                           // use default stack size  
//			(unsigned long (WINAPI*)(void*))point, // thread function 
//			0,					 // argument to thread function 
//			0,                           // use default creation flags 
//			&dwThreadId);                // returns the thread identifier
//#else
		res = AMThreadCreate(&tid, NULL,point,param);
//#endif
	}
	else 
		res= AMThreadCreate(&tid, NULL, target->get(), NULL);
	if (res == 0)
	{
		isLive = true;
		return true;
	}
	else
	{
		return false;
	}
}
Example #2
0
static AMInt32 _AMRunTask(_AMTaskMgr* _taskMgr, AMBool isUseThisThread)
{
    if(_taskMgr == AMNULL)
        return -1;
    else
    {
        AMInt32 index = 0;
        _AMTask* task = AMNULL;
        _AMTask* nextRunTask = AMNULL;
        AMThreadMutexLock(&_taskMgr->mMutex);
        //find a unrun task.
        task = _taskMgr->mTasks;
        while(task != AMNULL)
        {
            if(task->mStatus == 0)
            {
                nextRunTask = task;
                break;
            }
            task = task->mNext;
        }
        if(nextRunTask != AMNULL && isUseThisThread == AMFALSE)
        {
            //find a empty thread.
            for(index = 0; index < _taskMgr->mConcurNum; index++)
            {
                if(_taskMgr->mThreadsFlag[index] == 1)
                {
                    AMThreadWait(_taskMgr->mThreads[index], AMNULL);
                    _taskMgr->mThreads[index] = AMNULL;
                    _taskMgr->mThreadsFlag[index] = 0;
                }

                if(_taskMgr->mThreadsFlag[index] == 0)
                {
                    nextRunTask->mStatus = 1;
                    _taskMgr->mThreadsFlag[index] = 2;
					nextRunTask->mThreadIndex = index;
                    AMPrintf("Create Thread\n");
                    AMThreadCreate(&_taskMgr->mThreads[index], AMNULL, _AMRunTaskThreadRun, nextRunTask);
                    break;
                }
            }
        }
        else if(nextRunTask != AMNULL && isUseThisThread == AMTRUE)
        {
            nextRunTask->mStatus = 1;
            AMThreadMutexUnlock(&_taskMgr->mMutex);
            AMPrintf("Re use Thread\n");
            task->mProc(task->mProcArg);
            AMThreadMutexLock(&_taskMgr->mMutex);
            task->mStatus = 2;
            AMThreadMutexUnlock(&_taskMgr->mMutex);
            AMTaskMgrRemove((AMTaskMgr*)_taskMgr, (AMTask*)task);
            return 1;
        }
        AMThreadMutexUnlock(&_taskMgr->mMutex);
        return 0;
    }
}