20160226.CPPシステム詳細(0036日)

106804 ワード

プログラムセグメント(01):01.マルチスレッドc+02.マルチスレッド操作c内容概要:マルチスレッド
///01.   .c
#include 
#include 
#include 
#include 

//01.        :
//  1."  "      
//  2.MessageBox();  :
//        "  "           
//                    
//02.MessageBox();      :
//  1.        
//  2.                ,   
//                 ,        
// :MessageBox();    TEXT();       
//            (              )
void thTaskFun(void * pArr)
{
    MessageBox(0, TEXT("1"), TEXT("1"), 0);
}

//03.        (_beginthread();)     :
//  1.   :process.h
//  2.  :_beginthread(arg1, arg2, arg3);
//  3.  :
//      arg1:    (  )      (  )
//      arg2:    ,0      (                      )
//      arg3:      ,             (        )
// :        ,           !+    
int main01(void)
{
    MessageBox(0, "2", "2", 0);//         

    _beginthread(thTaskFun, 0, NULL);//             
    _beginthread(thTaskFun, 0, NULL);
    _beginthread(thTaskFun, 0, NULL);
    _beginthread(thTaskFun,  0, NULL);

    system("pause");
}
///02.     .c
#include 
#include 
#include 
#include 

//01.      :
//      :  (  )+  (  )
// :_beginthread();          ,CreateThread();        
void print(void * p)
{
    int i = 0;
    while (1)
    {
        printf("%d 
"
, ++i); Sleep(1000); } } //02. : // // 1. , // 2. // : // 1. , // 2.CPU // :Debug , , // , // : // // : , , // ( , ) // : , , // //03. : // MessageBox(); // system("pause"); int main02(void) { _beginthread(print, 0, NULL); system("pause");//(Debug ) , system("pause"); system("pause"); system("pause"); } //01. : ( )& ( ) //02. :Sleep(); , //03. :_beginthread(run,0,NULL); // run: // 0: // NULL: //04. : // MessageBox(); // system("pause"); //05. & [ ] // : <=> CPU // : CPu CPU , CPU // , // : , //06. : // 1. // 2. ( ) // 3.CPU //07. : // 1. , // 2. , // : //08. & ? // 1. // 2. CPU // 3. [ & ] //09. : // --> --> --> //10. : // 1. :PC&Phone // 2. CPU CPU , , // , , // [CPU ] // 3. CPU , // 4. : // CPU , // , CPU , CPU // 5. CPU [ ] // 6. , CPU // CPU --> //11. & : // : & // : & --> ( CPU ) // :CPU -->

プログラムクリップ(02):01.beginthread.c+02.CreateThread.cコンテンツ概要:マルチスレッド競合問題
///01._beginthread.c
#include  
#include 
#include 
#include 
#include 

//01.   :CRITICAL_SECTION
//  1.             
//  2.            64
//  3.                   :
//                      !(    )
// :       ,          64
//                 
// :              ,   Windows.h
//             
// :C                  ,CPP  
//                   
static CRITICAL_SECTION cs;

//02.    :              
//                         
// :            ,          
//    ,              !
static int num = 0;

//03.             :
//  1.   :100                    
//      100*100=10000,         10000
//  2.   :              ,       
//         ,       ,        -1,    -1
// :       ,              ,   
//                 (     )
// :       ,   N           :
//    ,(N-1)     "  "   -->
//            "  "     -->
//             ,            CPU    
//    ,1     "  "   
// :                     
//                (          )
static void myFun(void * p)
{
    for (int i = 0; i < 100; ++i)
    {
        EnterCriticalSection(&cs);//     
        ++num;
        LeaveCriticalSection(&cs);//     
        Sleep(3);//               (        )
    }
}

//04.           ?
//  1.    
//  2.    
//  3.    
//  4.    
//05.HANDLE    :
//  1.  Windows.h     
//  2.    ,  void *,             
//                   ,         
//06.           ,            
//                    
//07.             :
//    :WaitForMutipleObjects(arg1, arg2, arg3, arg4);
//    :arg1:    +arg2:    +arg3:  |    +arg4:    
//08.          :
//  1.    :    ,    
//  2.    :    ,     
//09.    :
//     :          
//           ,                ,     ,     
//                    
//     :                   
int main01(void)
{
    InitializeCriticalSection(&cs);
    time_t start, end;
    time(&start);
    HANDLE thArr[100] = { 0 };
    for (int i = 0; i < 100; ++i)
    {//                 ,                  
        thArr[i] = _beginthread(myFun, 0, NULL);
        //thArr[i] = CreateThread(NULL, 0, myFun, NULL, 0, NULL);
        //                   ,         ,           
        //    ,             ,      ,    ,       ,    
        //     ,           
    }
    WaitForMultipleObjects(100, thArr, TRUE, INFINITE);
    time(&end);
    printf("difftime = %lf 
"
, difftime(end, start)); printf("num = %d
"
, num); DeleteCriticalSection(&cs); system("pause"); } //01. : & // 1. , // 2. : // , //02. : // , , , // , , , ! //03. : // 1. : !-> , // 2. //04. : // 1.#include // 2. -> ( )-> ( )-> -> // time_t start,end; // time(&start);// // time(&end);// // difftime(end,start);// // %lf// //05. : // 1. .cpp // 2. : // : // CRITICAL_SECTION cs;// " " // :LONG LockCount;LONG RecursionCount;[Lock + ] // : C++ C // : , CPP // : // EnterCriticalSection(&cs); // : // LeaveCriticalSection(&cs); // : // : , , // Windows.h ( ) // DeleteCriticalSection(&cs); //06. : // 1. , , --> : // , , // 2. : //07.CreateThread(); _beginthread(); // CreateThread(); HANDLE // _beginthead(); int // process.h , , , // CreateThead(); [ , ] //08. : //
///02.CreateThread.c
#include 
#include 
#include 
#include 

//01.  "   "     :
//    :             
CRITICAL_SECTION cs;

//02.  "   "     :
//                      
int num = 0;

//03._beginthread();        
//void myFun(void * p)
//{
//  for (int i = 0; i < 100; ++i)
//  {
//      EnterCriticalSection(&cs);
//      ++num;
//      LeaveCriticalSection(&cs);
//  }
//}

//04.CreateThread();        
//  DWORD:unsigned long
//  WINAPI:              [        ]
DWORD WINAPI myFun(void * p)
{
    EnterCriticalSection(&cs);
    for (int i = 0; i < 100; ++i)
    {
        ++num;
    }
    LeaveCriticalSection(&cs);
    return 0;
}

int main02(void)
{
    InitializeCriticalSection(&cs);
    time_t start, end;
    time(&start);
    HANDLE thArr[100] = { 0 };
    for (int i = 0; i < 100; ++i)
    {
        thArr[i] = CreateThread(NULL, 0, myFun, NULL, 0, NULL);
        //WaitForSingleObject(thArr[i], INFINITE);
    }
    WaitForMultipleObjects(100, thArr, TRUE, INFINITE);
    time(&end);
    printf("difftime = %lf 
"
, difftime(end, start)); printf("num = %d
"
, num); DeleteCriticalSection(&cs); system("pause"); } //01. : // CRITICAL_SECTION: " ", " " // CreateThread();[ &HANDLE] //02. CreateThread(); Windows , , // :DWORD WINAPI // :CreateThread(NULL,0,myfun,NULL,0,NULL); // NULL: , // 0: : // myfun: // NULL: // 0: // NULL: //03.

プログラムクリップ(03):beginthread.c内容概要:スレッドプール
#include 
#include 
#include 
#include 

//01.         :
//    :CreateThread();
//    :_beginthread();
//02.         :
//    :
//      endthread();
//      exitthread();
//    :
//      terminate();
//03.           :
//    :SuspendThread(hd);
//    :ResumeThread(hd);

//04.CreateThread();            :
//  DWORD:unsigned long
DWORD WINAPI myFun(void * p)
{
    int i = 0;
    while (++i)
    {
        printf("%d 
"
, i); if (i > 8000) _endthread(); Sleep(1000); } return 0; } //05. : // 1. : // , // 2. : // : , " " , // , int main01(void) { HANDLE th = CreateThread(NULL, 0, myFun, NULL, 0, NULL); system("pause"); SuspendThread(th);// system("pause"); ResumeThread(th);// system("pause"); //ExitThread(0);// TerminateThread(th, 0);// //_endthread(); //system("pause"); system("pause"); } //01. : // --> --> --> --> //02. : & // //03.CreateThead(); // 1.DWORD WINAPI&return 0; // 2.HANDLE hd=CreateThead(NULL,0,fun,NULL,0,NULL); // NULL: // 0: , 0 , , // fun: // NULL: // 0: // NULL: //04. : //05. , IDE : // 1. // 2. [ ] // 3. : // , // , , // , //06. : // :_endthread();+exitthread(); // :terminatethread(); // :CloseThreadPool();// //07. : // SuspendThread();+ResumeThead(); //08. : // :return // : // _endThread();// // ExitThread(0);// // : // TerminateThead(hd,0): // 0 //09. //10.

プログラムクリップ(04):01.beginthread.c+02.CreateThead.c内容概要:スレッドプール
///01._beginthread.c
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 

//01.                       :
//  1.             ,        
//  2.      (   )-->      -->    
void run(void * p)
{
    char str[10] = { 0 };
    sprintf(str, "  %d", *(int *)p);
    printf("%s 
"
, str); } //02. : // 1._beginthead(run,0,&a[i]); // (1). , // (2). : // run: // 0: // &a[i]: --> // // 2._beingthead(); int // _beginthread(); CreateThread(); // ( int , HANDLE ) int main01(void) { int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 10; ++i) { int id = _beginthread(run, 0, &arr[i]); WaitForSingleObject(id, 300); } system("pause"); }
///02.CreateThead.c
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

DWORD WINAPI myFun01(void * p)
{
    int i = 0;
    while (++i)
    {
        printf("%d 
"
, i); Sleep(1000); } return 0; } int main02(void) { HANDLE th = CreateThread(NULL, 0, myFun01, NULL, 0, NULL); system("pause"); system("pause"); system("pause"); } DWORD WINAPI myFun02(void * p) { char str[10] = { 0 }; sprintf(str, " %d", *(int *)p); MessageBoxA(0, str, str, 0); return 0; } //01.CreateThread(); : // 1. : // HANDLE // 2. : // NULL: // 0: , , // myfun: // a+i: // 0: // NULL: int main02(void) { int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; HANDLE thArr[10] = { 0 }; for (int i = 0; i < 10; ++i) { thArr[i] = CreateThread(NULL, 0, myFun02, thArr + i, 0, NULL); //WaitForSingleObject(thArr[i], INFINITE); } WaitForMultipleObjects(10, thArr, FALSE, INFINITE); system("pause"); }

プログラムセグメント(05):マルチスレッド.c内容概要:スレッドを操作する
#include 
#include 
#include 
#include 

//01.           :
//      :CreateThread();
//      :_beginthread();
//02.           :
//      :_endthread();+ExitThread();
//      :TerminateThread();
//03.           :
//    :SuspendThread();
//    :ResumeThread();
DWORD WINAPI myFun(void * p)
{
    int i = 0;
    printf("%d 
"
, i); while (++i) { printf("%d
"
, i); if (i > 8000) { //_endthread(); //ExitThread(0); } } return 0; } int main01(void) { HANDLE th = CreateThread(NULL, 0, myFun, NULL, 0, NULL); system("pause"); SuspendThread(th); system("pause"); ResumeThread(th); system("pause"); TerminateThread(th, 0); system("pause"); }

プログラムセグメント(06):臨界領域.c内容概要:スレッド臨界領域によるスレッド衝突の解決
#include 
#include 
#include 

//01.                 :
//                       
//  #include --->      
#define N 20

int num = 0;

//02.        :
//  1.      ,      
//  2.           :
//                    
CRITICAL_SECTION cs1;
CRITICAL_SECTION cs2;

//03.         :
//               ;
//                 
DWORD WINAPI add(void * p)
{
    EnterCriticalSection(&cs1);//    
    for (int i = 0; i < 10000; ++i)
    {
        //EnterCriticalSection(&cs1);//    
        ++num;
        //LeaveCriticalSection(&cs1);//    
    }
    LeaveCriticalSection(&cs1);//    
    return 0;
}

//04.          :
//  1.               
//  2.                
DWORD WINAPI sub(void * p)
{
    EnterCriticalSection(&cs2);
    for (int i = 0; i < 10000; ++i)
    {
        --num;
    }
    LeaveCriticalSection(&cs2);
    return 0;
}

int main(void)
{
    //01.           
    InitializeCriticalSection(&cs1);
    InitializeCriticalSection(&cs2);
    //02.           :
    //          (    )
    //03.           :
    //             
    {
        HANDLE thArr[N] = { 0 };
        for (int i = 0; i < N; ++i)
        {
            thArr[i] = CreateThread(NULL, 0, add, NULL, 0, NULL);
            //WaitForSingleObject(thArr[i], INFINITE);
        }
        WaitForMultipleObjects(N, thArr, TRUE, INFINITE);
        printf("num = %d 
"
, num); } { HANDLE thArr[N] = { 0 }; for (int i = 0; i < N; ++i) { thArr[i] = CreateThread(NULL, 0, sub, NULL, 0, NULL); } WaitForMultipleObjects(N, thArr, TRUE, INFINITE); printf("num = %d
"
, num); } //04. DeleteCriticalSection(&cs1); DeleteCriticalSection(&cs2); system("pause"); } //01. ? // 1. // 2. Cocos2dx C++ , // ( ) // 3. 64,Windows // , // 4. : // --> --> --> --> //02. --> : // : , //03. : // 1.WaitForSingleObject(hd[i]); --> : // 2. , : // --> --> [ ] // & //04. : // : // CRITICAL_SECTION cs; // :Main(); // InitializeCriticalSection(&cs); // : // :EnterCriticalSection(); // :LeaveCriticalSection(); // :DeleteCriticalSection(); // 1. , , // 2. ( ) //05. : // C ,C++ //06. : // : // 1. // 2. // : // 1. // 2. // : (N-1) , // : // : // : // , !

プログラムセグメント(07):01.イベントc+02.イベントc+03.イベントc内容概要:スレッド通信イベントメカニズム
///01.  .c
#include 
#include 
#include 

//01.  :   3 
HANDLE eventArrA[3] = { 0 };
HANDLE threadArrA[3] = { 0 };
//02.  :    -->    -->    
DWORD WINAPI firstThread(void * p)
{
    MessageBoxA(0, "1", "1", 0);
    printf("         ! 
"
); SetEvent(eventArrA[0]);// ( Event ) return 0; } //03. : // , // : , DWORD WINAPI secondThread(void * p) { // [ Event , ] WaitForSingleObject(eventArrA[0], INFINITE);// ( Event ) MessageBoxA(0, "2", "2", 0); printf(" !
"
); return 0; } //04. : // + int main01(void) { //1. : // NULL: +TRUE: +FALSE: +NULL: eventArrA[0] = CreateEvent(NULL, TRUE, FALSE, NULL); eventArrA[1] = CreateEvent(NULL, TRUE, FALSE, NULL); //2. , : threadArrA[0] = CreateThread(NULL, 0, firstThread, NULL, 0, NULL); threadArrA[1] = CreateThread(NULL, 0, secondThread, NULL, 0, NULL); //3. : WaitForMultipleObjects(2, threadArrA, TRUE, INFINITE); printf(" !
"
); system("pause"); } //01. : // 1. : // (1). // (2). // 2. : // (1). // (2). // 3. : // (1). --> --> [ ] // (2). , // (3). , , //02. : [ ] // 1. , , // 2. ( ) //03. :CreateEvent(); // A: 0 // B:TRUE &FALSE // C: // D: //04. : [ ] // 1. // 2. , // (1). ( !) // (2). ( ) // (3)SetEvent(e);WaitForSingleObject(e); // ( )+ ( ) + //05. : // 1. // 2.
///02.  .c
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

HANDLE threadArrB[3] = { 0 };//     
HANDLE eventArrB[4] = { 0 };//    

//01.    :             
//                    
CRITICAL_SECTION csB;//    

//02.    :          
//  1.           
//  2.memset(str, '\0', 1024);
//           ,          
//  3.      :       
//                    ;
//                     
char strB[1024] = { 0 };//    

//03.        :
//    -->  (    0)
//        (    0)
//    -->  (    1)
//        (    0)
//    -->  (    2)
//        (    2)
//    -->  (    3)
//        (    3)
DWORD WINAPI haiHuaB(void * p)
{
    int i = 1;
    EnterCriticalSection(&csB);
    memset(strB, '\0', 1024);//        
    sprintf(strB, "haiHua %d  :i love you fangfang! 
"
, i);// LeaveCriticalSection(&csB); Sleep(1000);// ( ) SetEvent(eventArrB[0]);// //00: ( : ) while (++i) { WaitForSingleObject(eventArrB[3], INFINITE);// ( ) EnterCriticalSection(&csB); memset(strB, '\0', 1024);// sprintf(strB, "haiHua %d :i love you fangfang!
"
, i); LeaveCriticalSection(&csB); Sleep(1000);// SetEvent(eventArrB[0]); } return 0; } DWORD WINAPI ruiFuB(void * p) { int i = 0; while (++i) { WaitForSingleObject(eventArrB[0], INFINITE); EnterCriticalSection(&csB); printf(" HaiHua :%s
"
, strB); LeaveCriticalSection(&csB); Sleep(1000); SetEvent(eventArrB[1]); WaitForSingleObject(eventArrB[2], INFINITE); EnterCriticalSection(&csB); printf(" FangFang:%s
"
, strB); LeaveCriticalSection(&csB); Sleep(1000); SetEvent(eventArrB[3]); } //WaitForSingleObject(eventArrB[0], INFINITE); //printf("%s
", strB);
//SetEvent(eventArrB[1]); //WaitForSingleObject(eventArrB[2], INFINITE); //printf("%s
", strB);
return 0; } DWORD WINAPI fangFangB(void * p) { int i = 0; while (++i) { WaitForSingleObject(eventArrB[1], INFINITE); EnterCriticalSection(&csB); memset(strB, '\0', 1024); sprintf(strB, "fangFang %d :i love you haihua!
"
, i); LeaveCriticalSection(&csB); Sleep(1000); SetEvent(eventArrB[2]); } return 0; } int main02(void) { InitializeCriticalSection(&csB); eventArrB[0] = CreateEvent(NULL, TRUE, FALSE, NULL); eventArrB[1] = CreateEvent(NULL, TRUE, FALSE, NULL); threadArrB[0] = CreateThread(NULL, 0, haiHuaB, NULL, 0, NULL); threadArrB[1] = CreateThread(NULL, 0, ruiFuB, NULL, 0, NULL); threadArrB[2] = CreateThread(NULL, 0, fangFangB, NULL, 0, NULL); WaitForMultipleObjects(3, threadArrB, TRUE, INFINITE); printf("over!!!
"
); DeleteCriticalSection(&csB); system("pause"); } //01. : , , , //02. , , // 1. , --> // 2. i , // 3. : + [Show ]--> // 4. Show--> & //03. : // haihua --> // --> //04.. : // 1.haihua--> 0 // 2.zhongjiezhe--> 0-- 1 // 3.wangfang--> 1-- 2 // 4.zhongjiezhe--> 2--> 3 // 5.haihua--> 3-- 0 //05. //
///03.  .c
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

//01.HANDLE:           ,     (void *)
HANDLE threadArr[3] = { 0 };//      
HANDLE eventArr[4] = { 0 };//      

//02.CRITICAL_SECTION:                 
CRITICAL_SECTION cs = { 0 };//       :                 

char str[1024] = { 0 };//       {          }

//03.         :
//            0
//                0
//            1
//                1
//            2
//                2
//            3
//                3
//            0
DWORD WINAPI haiHua(void *p)//WINAPI:            
{
    int i = 1;//        
    EnterCriticalSection(&cs);//     
    memset(str, '\0', 1024);//    
    sprintf(str, "haiHua %d  :i love you Fang! 
"
, i);// LeaveCriticalSection(&cs);// Sleep(1000);// SetEvent(eventArr[0]);// 0 while (++i) { WaitForSingleObject(eventArr[3], INFINITE);// 3 EnterCriticalSection(&cs); memset(str, '\0', 1024); sprintf(str, "
haiHua %d :i love you Fang!
"
, i); LeaveCriticalSection(&cs); Sleep(1000); SetEvent(eventArr[0]); } return 0; } DWORD WINAPI ruiFu(void *p) {// int i = 0; int flag = 0; while (++i) { if (!flag) {// WaitForSingleObject(eventArr[0], INFINITE); EnterCriticalSection(&cs); printf(" %d %s
"
, i, str); LeaveCriticalSection(&cs); Sleep(1000); SetEvent(eventArr[1]); flag = 1;// } else { WaitForSingleObject(eventArr[2], INFINITE); EnterCriticalSection(&cs); printf(" %d %s
"
, i, str); LeaveCriticalSection(&cs); Sleep(1000); SetEvent(eventArr[3]); flag = 0; } } return 0; } DWORD WINAPI fangFang(void *p) { int i = 0; while (++i) { WaitForSingleObject(eventArr[1], INFINITE); EnterCriticalSection(&cs); memset(str, '\0', 1024); sprintf(str, "fangFang %d :i love you too!
"
, i); LeaveCriticalSection(&cs); Sleep(1000); SetEvent(eventArr[2]); } return 0; } void main03() { //01. InitializeCriticalSection(&cs); //02. eventArr[0] = CreateEvent(NULL, TRUE, FALSE, NULL); eventArr[1] = CreateEvent(NULL, TRUE, FALSE, NULL); eventArr[2] = CreateEvent(NULL, TRUE, FALSE, NULL); eventArr[3] = CreateEvent(NULL, TRUE, FALSE, NULL); //03. threadArr[0] = CreateThread(NULL, 0, haiHua, NULL, 0, NULL); threadArr[2] = CreateThread(NULL, 0, ruiFu, NULL, 0, NULL); threadArr[1] = CreateThread(NULL, 0, fangFang, NULL, 0, NULL); //04. , WaitForMultipleObjects(2, threadArr, TRUE, INFINITE); printf("over"); //05. : & DeleteCriticalSection(&cs); system("pause"); }

プログラムセグメント(08):スレッド競合.cコンテンツ概要:スレッドが互いに反発する
#include 
#include 
#include 

//01.    :                
//                       
int num = 0;

//02.   :   (void *)          
//  1.            64               
//              (           Windows    )
//      (                         )
//  2.            :
//                             !
HANDLE mutex = NULL;

DWORD WINAPI add(void * p)
{
    WaitForSingleObject(mutex, INFINITE);//      
    if (NULL == mutex)
    {//      
        printf("         ! 
"
); abort(); } for (int i = 0; i < 10000; ++i) { ++num; } ReleaseMutex(mutex);// return 0; } int main01(void) { mutex = CreateMutex(NULL, FALSE, NULL); HANDLE threadArr[64] = { 0 }; for (int i = 0; i < 64; ++i) { threadArr[i] = CreateThread(NULL, 0, add, NULL, 0, NULL); if (NULL == threadArr[i]) { printf(" !
"
); } } WaitForMultipleObjects(64, threadArr, TRUE, INFINITE); printf("num = %d
"
, num); for (int i = 0; i < 64; ++i) { CloseHandle(threadArr[i]);// } CloseHandle(mutex);// system("pause"); } //01. : // 1. --> --> : // 2. : , // : + //02. : // 1. 64 // 2. , , // [ ] //03. : // : // HANDLE mutex = NULL; // : // mutex = CreateMutex(NULL,FALSE,NULL); // : // :WaitForSingleObject(mutex,INFINITE);// // :ReleaseMutex(mutex);// // : // CloseHandle(mutex);

プログラムセグメント(09):01.Run.c+02.原子変数と原子操作c内容概要:原子変数と原子操作
///01.Run.c
#include 
#include 
#include 

//01.    (Debug)   (Release)    :
//  Debug:            
//  Release:           
//02.  register volatile     :
//  register:             (       )
//  volatile:              (      )
//03.for(int i = 0; i < INT_MAX; ++i){}:
//             Release      ,   
//                 ,          
//  :          +              
int main01(void)
{
    for (volatile int i = 0; i < INT_MAX; ++i) {}
    printf("over");

    system("pause");
}

//04.              :
//  1.           :
//               ,          
//  2.Debug   Release  :
//      Debug      (     ),Release      (    )
// :                   (volatile  )
volatile int num = 20;
DWORD WINAPI rmsg(void * p)
{//     
    //int * px = p;
    int * px = (int *)p;
    while (1)
    {
        //printf("%d 
", *px);//
//int data = *px;// , printf("%d
"
, num); Sleep(1000); } } DWORD WINAPI wmsg(void * p) {// int * px = (int *)p; while (1) { *px += 1; Sleep(1000); } } int main01(void) { CreateThread(NULL, 0, wmsg, &num, 0, NULL); CreateThread(NULL, 0, wmsg, &num, 0, NULL); CreateThread(NULL, 0, wmsg, &num, 0, NULL); CreateThread(NULL, 0, rmsg, &num, 0, NULL); system("pause"); } //01. ? // , ( ) //02. --> --> // : , , ... //03. :Atomitic // --> : // , ( ) //04.volatile: // Debug : // Release : , // : , // , //05.Realese: //06. : // --> // --> // [ ] //07. , // //08. // //09. : // register + volatile // : !
///02.         .c
#include 
#include 
#include 

//01.    :                    
//  1.             (     ,     ):
//             ,                
//             ,                  
//  2.                    :
//         :      
//         :      
//         :      
// :              ,           
//                  
// :                    
//  1.  ,      ,              ,   
//            
//  2.     :
//      int       -->       -->      
int num = 0;

//02.                 :
//  1.   ++num       ,       
//  2.         ,           :
//      (1).               ,       
//      (2).        ++num;  
//      (3).                  
//  3.  ++num;                  
//             ,                 
//  4.  ,          ,            
//            ,            ,      
//             !
//  5.C CPP                     :
//               
//03.         :
//  1. "  "    
//  2. "  " "  "    
//  3. "  " "  "    
//  4. "  " "  "    
// :       volatile     ,         
DWORD WINAPI runX(void * p)
{
    for (int i = 0; i < 10000; ++i)
    {
        //++num;
        //InterlockedIncrement(&num);//(++num) or (num++)
        //InterlockedIncrement(&num, 1);//      
        //InterlockedExchange(&num);//    
        //InterlocakedAdd(&num);//      ,        
        InterlocakedExchangeAdd(&num, 1);//       
    }
}

int main01(void)
{
    HANDLE threadArr[50] = { 0 };
    for (int i = 0; i < 50; ++i)
    {
        threadArr[i] = CreateThread(NULL, 0, runX, NULL, 0, NULL);
    }
    WaitForMultipleObjects(50, threadArr, TRUE, INFINITE);
    printf("num = %d 
"
, num); system("pause"); } //01. (C++ ) // //02. ? // // ( : !) //03.C 0 NULL // , //04. : // //5. ? // C -->C++ -->

プログラムセグメント(10):main.c内容概要:volatile
#include 
#include 
#include 
#include 

int num = 1120;//     :              

DWORD WINAPI rmsg(void *p)
{//  
    int * px = (int *)p;
    while (1)
    {
        //int data = *px;//        
        printf("
%d"
, num); Sleep(1000); } } DWORD WINAPI wmsg(void *p) {// int * px = (int*)p; while (1) { *px += 1; Sleep(10000); } } void main01() { CreateThread(NULL, 0, msg, &num, 0, NULL); CreateThread(NULL, 0, msg, &num, 0, NULL); CreateThread(NULL, 0, msg, &num, 0, NULL); CreateThread(NULL, 0, cmsg, &num, 0, NULL); system("pause"); }

プログラムセグメント(11):Time.c内容概要:時間同期
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

//01.    :     
//  1.             ,                :
//              ,         
//  2.                :
//                      ,        
// :                 !
//02.         :
//  1.       ,           7 
//  2.       ,              
// :timer          
int main01(void)
{
    HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);
    if (NULL == timer)
    {
        printf("       ! 
"
); abort(); } LARGE_INTEGER time; //-5000 000 0 : --> -->0.1 time.QuadPart = -20000000;// 2 //10^-7 --> // SetWaitableTimer(timer, &time, 0, NULL, 0, NULL);// // if (WAIT_OBJECT_0 == WaitForSingleObject(timer, INFINITE)) { printf(" !
"
); } else { printf(" !
"
); } while (1) { //01. , // //02. , // 1. , // 2. // : // (1) // (2) , // while(1) // { : , } // --> // --> [ ]--> //03. // printf("fangfang
"
); Sleep(2000); } system("pause"); } HANDLE timer;//(1. ) DWORD WINAPI go1(void *p) { MessageBoxA(0, "1", "1", 0); // go1 5 go2 //(2. ) timer = CreateWaitableTimer(NULL, TRUE, NULL); LARGE_INTEGER time; time.QuadPart = -50000000;//5 //10 -7 0.1 SetWaitableTimer(timer, &time, 0, NULL, 0, NULL);// 2 } DWORD WINAPI go2(void *p) { //if (WaitForSingleObject(timer,INFINITE)==WAIT_OBJECT_0) //{ WaitForSingleObject(timer, INFINITE); MessageBoxA(0, "2", "2", 0); printf(" !"); //} //else //{ //printf(" !"); //} } void main02() { // HANDLE hd=CreateThread(NULL, 0, go1, NULL, 0, NULL); // : // : , WaitForSingleObject(hd, INFINITE); if (WaitForSingleObject(timer,INFINITE)==WAIT_OBJECT_0) { CreateThread(NULL, 0, go2, NULL, 0, NULL); printf(" !"); } else { printf(" !"); } getchar(); } //01. : // : //02. : [ ] // //03. : // 1. // 2. : 3 // 3. //04. //05.

プログラムセグメント(12):time.c内容概要:マルチスレッド実戦
#include 
#include 
#include 
#include 

int i = 1;//    :       

void setTime(void * p)
{//     :
    while (1)
    {
        Sleep(1000);
        char str[40] = { 0 };
        sprintf(str, "title       %3d  ! 
"
, i++); system(str); } } void run(void * p) {// while (1) { if (3 == i) { system("calc"); } else if (10 == i) { system("notepad"); _endthread(); } else if (19 == i) { system("tasklist & pause"); } Sleep(1000); } } int main01(void) { system("title China World!
"
); _beginthread(setTime, 0, NULL);// _beginthread(run, 0, NULL);// system("pause"); }

プログラムセグメント(13):main.cコンテンツ概要:マルチスレッド取得配列
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

//01.       :
//                    
int isFind = 0;//    -->       
int * pFind = NULL;//        

typedef struct
{
    int arrLen;
    int findNum;
    int thID;
    int * intArr;
} thTaskInfo;

void searchNum(void * p)
{
    thTaskInfo * pThTask = (thTaskInfo *)p;
    printf("   %d       : 
"
, pThTask->thID); for (int * pTmp = pThTask->intArr; pTmp < pThTask->intArr + pThTask->arrLen; ++pTmp) { if (1 == isFind) { printf(" %d , !
"
, pThTask->thID); _endthread(); } if (pThTask->findNum == *pTmp) { isFind = 1; pFind = pTmp; printf(" %d %d, :%p !
"
, pThTask->thID, *pTmp, pTmp); _endthread(); } } printf(" %d , !
"
, pThTask->thID); } int main01(void) {// int data[1000] = { 0 }; for (int i = 999; i > -1; --i) { data[i] = i; } thTaskInfo thTaskArr[10] = { 0 }; for (int i = 0; i < 10; ++i) { thTaskArr[i].intArr = data + i * 100; thTaskArr[i].arrLen = 100; thTaskArr[i].findNum = 767; thTaskArr[i].thID = i; _beginthread(searchNum, 0, &thTaskArr[i]); } system("pause"); } //02. : // 1. N // 2. (N-1) // 1 // 3. (N-1) // int main02(void) { int data[1000] = { 0 }; for (int i = 999; i > -1; --i) { data[i] = i; } int findNum = 0; int thNum = 0; scanf("%d %d", &findNum, &thNum); int quotient = 1000 / thNum; int remainder = 1000 % thNum; thTaskInfo * pThTaskArr = (thTaskInfo *)malloc(thNum *sizeof(thTaskInfo)); if (!remainder) { for (int i = 0; i < thNum; ++i) { (pThTaskArr + i)->intArr = data + i * (1000 / thNum); (pThTaskArr + i)->arrLen = 1000 / thNum; (pThTaskArr + i)->findNum = findNum; (pThTaskArr + i)->thID = i; _beginthread(searchNum, 0, pThTaskArr + i); } } else { for (int i = 0; i < thNum - 1; ++i) { (pThTaskArr + i)->intArr = data + i * (1000 / quotient); (pThTaskArr + i)->arrLen = 1000 / quotient; (pThTaskArr + i)->findNum = findNum; (pThTaskArr + i)->thID = i; _beginthread(searchNum, 0, pThTaskArr + i); } int endI = thNum - 1; (pThTaskArr + endI)->intArr = data + quotient * (thNum - 1); (pThTaskArr + endI)->arrLen = remainder; (pThTaskArr + endI)->findNum = findNum; (pThTaskArr + endI)->thID = endI; _beginthread(searchNum, 0, pThTaskArr + endI); } system("pause"); }

プログラムセグメント(14):DualCircle.h+DualCircle.c内容概要:二重ループ
///DualCircle.h
#pragma once

typedef struct node
{
    int data;
    struct node * pPre;
    struct node * pNext;
}Node;

typedef struct dualcircle
{
    Node * pHead;
    Node * pTail;
}DualCircle;

void initNodeWithData(Node * pNode, int data);

void initDualCircle(DualCircle * pDualCircle);

void dualCircleHeadInsert(DualCircle * pDualCircle, int data);

void dualCircleTailInsert(DualCircle * pDualCircle, int data);

Node * dualCircleSelectFirst(DualCircle * pDualCircle, int data);

void dualCircleRandInsert(DualCircle * pDualCircle, int findData, int insertData);

void showDualCircle(DualCircle * pDualCircle);

void dualCircleDeleteFirst(DualCircle * pDualCircle, int data);

void dualCircleUpdateFirst(DualCircle * pDualCircle, int oldData, int newData);
///DualCircle.c
#include "DualCircle.h"
#include <Windows.h>

void initNodeWithData(Node * pNode, int data)
{
    if (NULL == pNode)
        abort();
    pNode->data = data;
    pNode->pPre = NULL;
    pNode->pNext = NULL;
}

void initDualCircle(DualCircle * pDualCircle)
{
    if (NULL == pDualCircle)
        abort();
    pDualCircle->pHead = pDualCircle->pTail = NULL;
}

void dualCircleHeadInsert(DualCircle * pDualCircle, int data)
{
    if (NULL == pDualCircle)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    initNodeWithData(pNew, data);
    if (NULL == pDualCircle->pHead)
    {//   
        pNew->pPre = pNew->pNext = pNew;
        pDualCircle->pHead = pDualCircle->pTail = pNew;
        return;
    }
    if (pDualCircle->pHead == pDualCircle->pTail)
    {//   
        pNew->pPre = pNew->pNext = pDualCircle->pHead;
        pDualCircle->pTail->pPre = pDualCircle->pTail->pNext = pNew;
        pDualCircle->pHead = pNew;
        return;
    }//   
    pNew->pPre = pDualCircle->pTail;
    pNew->pNext = pDualCircle->pHead;
    pDualCircle->pHead = pNew;
    pDualCircle->pTail->pNext = pNew;
}

void dualCircleTailInsert(DualCircle * pDualCircle, int data){}

Node * dualCircleSelectFirst(DualCircle * pDualCircle, int data)
{
    if (NULL == pDualCircle)
        abort();
    if (NULL == pDualCircle->pHead)
        abort();
    Node * pTmp = pDualCircle->pHead;
    do
    {
        if (data == pTmp->data)
            return pTmp;
        pTmp = pTmp->pNext;
    } while (pDualCircle->pHead != pTmp);
    return NULL;
}

void dualCircleRandInsert(DualCircle * pDualCircle, int findData, int insertData)
{
    if (NULL == pDualCircle)
        abort();
    if (NULL == pDualCircle->pHead)
        abort();
    int find = 0;
    Node * pTmp = pDualCircle->pHead;
    do
    {
        if (findData == pTmp->data)
        {
            find = 1;
            break;
        }
        pTmp = pTmp->pNext;
    } while (pDualCircle->pHead != pTmp);
    if (!find)
        return;
    Node * pInsert = (Node *)malloc(sizeof(Node));
    initNodeWithData(pInsert, insertData);
    if (pDualCircle->pTail == pTmp)
    {
        pDualCircle->pTail->pNext = pInsert;
        pInsert->pPre = pDualCircle->pTail;
        pInsert->pNext = pDualCircle->pHead;
        pDualCircle->pHead->pPre = pInsert;
        pDualCircle->pTail = pInsert;
        return;
    }
    pInsert->pPre = pTmp;
    pInsert->pNext = pTmp->pNext;
    pTmp->pNext = pInsert;
    pTmp->pNext->pPre = pInsert;
}

//       ,     !