بسم الله الرحمن الرحيم
بصراحة لا اعلم افضل طريقة للشرح في منتدى به الاف من الاعضاء
اكتب الكود و اعلق ام ماذا ...
ليتكم تساعدوني بالاسئلة - داخل اطار البرنامج -
الـMutlithread امر مهم جدا احيانا يكون الكمبيوتر مشغول بالقراءة من الكيبورد او التعامل مع الشاشة
او كارت الشبكات فبدلا من تنفيذ البرنامج بطريقة تسلسلية من الممكن ان تصممه بالعمل كعدة اجزاء
thread يعني خيط !!! المهم البرنامج يكون بعدة انفاس او ارواح من الممكن ان تنفذ بدون تسلسلية
البرنامج ليس مثالي و كتب بعرض تعليمي و من الممكن اجراء الكثير من التعديلات!!!
همي الاول السهولة و البساطة اليكم
الكود و ندير حلقة نقاشية تعليمية حوله ما رأيكم دام فضلكم - يا ترى الشاى على مين
// dos2.cpp : A good example of MultiThread Program with emphasis of pointer.////#include "stdafx.h"//#include <stdlib.h>#include <stdio.h>#include <process.h>#define ddum(a,b) a+bint addem(int);int factoria(int);int AddThree(int list[]);int DataAdd(int *list);int StrPrint(char *str);int side=4;int main(int argc, char* argv[]){// int arr[2];arr[1]=4;arr[2]=5;// float *ptr_y,yy[2]={3.0,9.2};int id1,id2,id3,id4,id5;int list[]={3,5,19},kll[]={8,1,2,3,4,5,6,7};int (*ptr)(char *str);char str[] = "*** Pointing to a function.";ptr = StrPrint;printf(" --** Hello World! **--\n");id1=_beginthread((void (*)(void *))addem, 0, (void *)4);id2=_beginthread((void (*)(void *))factoria, 0, (void *)4);id3=_beginthread((void (*)(void *))AddThree, 0, (void *) list);id4=_beginthread((void (*)(void *))DataAdd, 0, (void *) kll);id5=_beginthread((void (*)(void *))ptr, 0, (void *) str);addem(20);(*ptr)(str);if ((*ptr)(str)) printf("Done!\n");/* printf("Sum of %d and %d is %d",arr[1],arr[2],ddum(arr[1],arr[2]));printf("\npid1 = %d, pid2 = %d, pid3 = %d\n\n",id1,id2,id3);ptr_y = &yy[0];printf("ptr_y: address=0x%p, content=0x%p\n", &ptr_y, ptr_y);printf("*ptr_y +7 => %5.2f.......*(ptr_y + 1) => %5.2f\n", *ptr_y +7,*(ptr_y+1));printf("%f ..... %f\n\n",yy[0],yy[1]);AddThree(list);DataAdd(kll);*/ return 0;}int addem(int count){int i, sum;sum = 0;for (i=0; i<=count; ++i) {printf("I The value of I is %d\n", i);fflush(stdout);sum += i;}printf("I *** The sum is %d\n", sum);fflush(stdout);return 0;}int factoria(int don){int j, mul;mul=1;for(j=1;j<=don;j++) {printf("II The value of II is %d\n", j);fflush(stdout);mul *= j;}printf("II *** The mul is %d\n", mul);fflush(stdout);return 0;}int AddThree(int list[]){int i;int result =0;for (i=0; i<3; i++){printf("III The Value of III is %d\n",i);result += list;}printf("III *** The sum of the three integers is: %d\n", result);return 0;}int DataAdd(int *listt){int i;int sum =0,max=listt[0];// printf("max is %d\n",max);for (i=1; i<max; i++){printf("IVV The Value of IVV is %d\n",i);sum += listt;}printf("IVV *** The Sum is %d\n",sum);return 0;}int StrPrint(char *str){printf("%s\n", str);return 1;}الـOutput و بالطبع يختلف من جهاز بل على نفس الجهاز يختلف من ظرف لاخر
C'est la Multithread
--** Hello World! **-- I The value of I is 0 I The value of I is 0 II The value of II is 1 III The Value of III is 0 IVV The Value of IVV is 1 *** Pointing to a function. IVV The Value of IVV is 2 IVV The Value of IVV is 3 IVV The Value of IVV is 4 IVV The Value of IVV is 5 IVV The Value of IVV is 6 IVV The Value of IVV is 7 IVV *** The Sum is 28 III The Value of III is 1 I The value of I is 1 I The value of I is 2 I The value of I is 3 I The value of I is 4 I The value of I is 5 I The value of I is 6 I The value of I is 7 I The value of I is 8 I The value of I is 1 II The value of II is 2 III The Value of III is 2 III *** The sum of the three integers is: 27 II The value of II is 3 II The value of II is 4 II *** The mul is 24 I The value of I is 2 I The value of I is 3 I The value of I is 4 I *** The sum is 10 I The value of I is 9 I The value of I is 10 I The value of I is 11 I The value of I is 12 I The value of I is 13 I The value of I is 14 I The value of I is 15 I The value of I is 16 I The value of I is 17 I The value of I is 18 I The value of I is 19 I The value of I is 20 I *** The sum is 210 *** Pointing to a function. *** Pointing to a function. Done!
و الان اتركه لكم ثم نرجع له لما يشاء المناقشة