freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內容

基于多核系統(tǒng)的編程技術第一節(jié)并行程序設計流程第二節(jié)線-資料下載頁

2025-07-18 18:45本頁面
  

【正文】 ) pthread_join(thdHandle[i],NULL)。 printf(Press any key to exit..//)。 i = getchar()。 return(0)。 } 線程的交互-合并與分離 ? 合并- joining –線程間達到同步的一種方法。 ? 分離- detaching –在線程不需要與其它線程交互時,脫離。 合并操作的注意點: ?通知操作系統(tǒng)阻塞調用該函數(shù)的線程直到指定的線程退出。 ?若要等待多個線程,只需簡單的合并所有線程即可。 ?兩個線程不能去合并同一個線程。 ?當某線程已經(jīng)被合并后,任何其它線程都不能對其進行合并操作。 ?在線程被創(chuàng)建的同時,其屬性就說明了這個線程是否可合并。 如果可合并,才可以對該線程做合并操作。 如果是聲明脫離的,則永遠不能被合并。 交互函數(shù) pthread_join (threadid,status) pthread_detach (threadid,status) pthread_attr_setdetachstate (attr,detachstate) pthread_attr_getdetachstate (attr,detachstate) include include include define NUM_THREADS 3 void *BusyWork(void *null){ int i。 double result=。 for (i=0。 i1000000。 i++) { result = result + (double)random()。 } printf(Thread result = %e\n,result)。 pthread_exit((void *) 0)。 } int main(int argc, char *argv[]){ pthread_t thread[NUM_THREADS]。 pthread_attr_t attr。 int rc, t, status。 /* Initialize and set thread detached attribute */ pthread_attr_init(amp。attr)。 pthread_attr_setdetachstate(amp。attr, PTHREAD_CREATE_JOINABLE)。 for(t=0。tNUM_THREADS。t++) { printf(Creating thread %d\n, t)。 rc = pthread_create(amp。thread[t], amp。attr, BusyWork, NULL)。 if (rc) { printf(ERROR。 return code from pthread_create() is %d\n, rc)。 exit(1)。 } } /* Free attribute and wait for the other threads */ pthread_attr_destroy(amp。attr)。 for(t=0。tNUM_THREADS。t++) { rc = pthread_join(thread[t], (void **)amp。status)。 if (rc) { printf(ERROR return code from pthread_join() is %d\n, rc)。 exit(1)。 } printf(Completed join with thread %d status= %d\n,t, status)。 } pthread_exit(NULL)。 } 合并舉例 1 Creating thread 0 Creating thread 1 Creating thread 2 Thread result = +15 Thread result = +15 Thread result = +15 Completed join with thread 0 status= 0 Completed join with thread 1 status= 0 Completed join with thread 2 status= 0 輸出 線程堆棧大小的設置 pthread_attr_getstacksize (attr, stacksize) pthread_attr_setstacksize (attr, stacksize) pthread_attr_getstackaddr (attr, stackaddr) pthread_attr_setstackaddr (attr, stackaddr) 函數(shù) include include define NTHREADS 4 define N 1000 define MEGEXTRA 1000000 pthread_attr_t attr。 void *dowork(void *threadid) { double A[N][N]。 int i,j。 size_t mystacksize。 pthread_attr_getstacksize (amp。attr, amp。mystacksize)。 printf(Thread %d: stack size = %d bytes \n, threadid, mystacksize)。 for (i=0。 iN。 i++) for (j=0。 jN。 j++) A[i][j] = ((i*j)/) + (Ni)。 pthread_exit(NULL)。 } int main(int argc, char *argv[]) { pthread_t threads[NTHREADS]。 size_t stacksize。 int rc, t。 pthread_attr_init(amp。attr)。 pthread_attr_getstacksize (amp。attr, amp。stacksize)。 printf(Default stack size = %d\n, stacksize)。 stacksize = sizeof(double)*N*N+MEGEXTRA。 printf(Amount of stack needed per thread = %d\n,stacksize)。 pthread_attr_setstacksize (amp。attr, stacksize)。 printf(Creating threads with stack size = %d bytes\n,stacksize)。 for(t=0。 tNTHREADS。 t++){ rc = pthread_create(amp。threads[t], amp。attr, dowork, (void *)t)。 if (rc){ printf(ERROR。 return code from pthread_create() is %d\n, rc)。 exit(1)。 } } printf(Created %d threads.\n, t)。 pthread_exit(NULL)。 } 堆棧設置舉例 ③ 線程同步 ? PThread庫提供了與 Win32和 .NET框架中具有相同功能的互斥量 (mutex)機制。 ? PThread中互斥量的狀態(tài): –已加鎖 –未加鎖 pthread_mutex_lock(amp。aMutex)。 //....被保護的代碼 for(int i=0。i3。i++) printf(Thread number is %d\n,*((int*))num)。 pthread_mutex_unlock(amp。aMutex)。 Pthreads中互斥量的使用 ? 創(chuàng)建與銷毀 ? 對互斥量的鎖和解鎖操作 ? 問題: 當有多個線程都在等待一個被鎖的互斥量時,在互斥量被釋放時,哪個線程將獲得該互斥量? pthread_mutex_init (mutex,attr) pthread_mutex_destroy (mutex) pthread_mutexattr_init (attr) pthread_mutexattr_destroy (attr) pthread_mutex_lock (mutex) pthread_mutex_trylock (mutex) pthread_mutex_unlock (mutex) 除非設置了線程優(yōu)先級,否則將交由系統(tǒng)來決定,一般是隨機的。 include include include typedef struct { double *a。 double *b。 double sum。 int veclen。 } DOTDATA。 define NUMTHRDS 4 define VECLEN 100 DOTDATA dotstr。 pthread_t callThd[NUMTHRDS]。 //全局變量 pthread_mutex_t mutexsum。 //全局變量 void *dotprod(void *arg){ int i, start, end, offset, len 。 double mysum, *x, *y。 offset = (int)arg。 len = 。 start = offset*len。 end = start + len。 x = 。 y = 。 mysum = 0。 for (i=start。 iend 。 i++) { mysum += (x[i] * y[i])。 } pthread_mutex_lock (amp。mutexsum)。 += mysum。 pthread_mutex_unlock (amp。mutexsum)。 pthread_exit((void*) 0)。 } Int main (int argc, char *argv[ ]){ int i。 double *a, *b。 int status。 pthread_attr_t attr。 a = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double))。 b = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double))。 for (i=0。 iVECLEN*NUMTHRDS。 i++) { a[i]=。b[i]=a[i]。 } = VECLEN。 = a。 = b。 =0。 pthread_mutex_init(amp。mutexsum, NULL)。 pthread_attr_init(amp。attr)。 pthread_attr_setdetachstate(amp。attr, PTHREAD_CREATE_JOINABLE)。 for(i=0。 iNUMTHRDS。 i++) { pthread_create( amp。callThd[i], amp。attr, dotprod, (void *)i)。 } pthread_attr_destroy(amp。attr)。 for(i=0。 iNUMTHRDS。 i++){ pthread_join( callThd[i], (void **)amp。status)。 } /* After joining, print out the results and cleanup */ printf (Sum = %f \n, )。 free (a)。 free (b)。 pthread_mutex_destroy(amp。mutexsum)。 pthread_exit(NULL)。 } 互斥量使用舉例 注:這是并行的互斥量的使用舉例。在課程網(wǎng)站下載的代碼中有串行的例子,同學們自己比較一下 。 ④ 激發(fā)
點擊復制文檔內容
醫(yī)療健康相關推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1