CMreturn CMthreadJobExecute (CMthreadTeam_p team, CMthreadJob_p job) { int ret, taskId; size_t threadId; void *status; pthread_attr_t thread_attr; if (team != (CMthreadTeam_p) NULL) { pthread_attr_init (&thread_attr); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); team->JobPtr = (void *) job; job->Group = 0; if (job->Sorted == false) { _CMthreadJobTaskSort (job); job->Sorted = true; } for (threadId = 0; threadId < team->ThreadNum; ++threadId) { if ((ret = pthread_create (&(team->Threads [threadId].Thread), &thread_attr,_CMthreadWork,(void *) (team->Threads + threadId))) != 0) { CMmsgPrint (CMmsgSysError,"Thread creation returned with error [%d] in %s:%d\n",ret,__FILE__,__LINE__); free (team->Threads); free (team); return (CMfailed); } } pthread_attr_destroy(&thread_attr); for (threadId = 0;threadId < team->ThreadNum;++threadId) pthread_join(team->Threads [threadId].Thread, &status); } else for (taskId = 0;taskId < job->TaskNum; ++taskId) job->UserFunc (job->CommonData, job->Data, taskId); return (CMsucceeded); }
CMreturn CMthreadJobExecute (CMthreadTeam_p team, CMthreadJob_p job) { int taskId; struct timeb tbs; long long startTime, localStart; ftime (&tbs); startTime = tbs.time * 1000 + tbs.millitm; if (job->Sorted == false) { _CMthreadJobTaskSort(job); ftime (&tbs); team->ExecTime += (tbs.time * 1000 + tbs.millitm - startTime); } ftime (&tbs); startTime = tbs.time * 1000 + tbs.millitm; if (team->ThreadNum < 1) { ftime (&tbs); localStart = tbs.time * 1000 + tbs.millitm; for (job->Group = 0; job->Group < job->GroupNum; job->Group++) { for (taskId = job->Groups[job->Group].Start; taskId < job->Groups[job->Group].End; ++taskId) job->UserFunc(0, job->SortedTasks[taskId]->Id, job->CommonData); } ftime (&tbs); team->Time += (tbs.time * 1000 + tbs.millitm - localStart); } else { for (job->Group = 0; job->Group < job->GroupNum; job->Group++) { if (job->Groups[job->Group].End - job->Groups[job->Group].Start < team->ThreadNum) { ftime (&tbs); localStart = tbs.time * 1000 + tbs.millitm; for (taskId = job->Groups[job->Group].Start; taskId < job->Groups[job->Group].End; ++taskId) job->UserFunc(0, job->SortedTasks[taskId]->Id, job->CommonData); ftime (&tbs); team->Time += (tbs.time * 1000 + tbs.millitm - localStart); } else { pthread_mutex_lock (&(team->SMutex)); job->Completed = 0; team->JobPtr = (void *) job; pthread_cond_broadcast (&(team->SCond)); pthread_mutex_unlock (&(team->SMutex)); pthread_cond_wait (&(team->MCond), &(team->MMutex)); } } } ftime (&tbs); team->ExecTime += (tbs.time * 1000 + tbs.millitm - startTime); return (CMsucceeded); }