驗證winCE系統的同步機制

2021-06-17 20:11:43 字數 4225 閱讀 8253

讀了田海立老師的兩篇文章  wince系統的同步機制

關於 windows ce 系統中同步機制的思考,受益匪淺。於是寫**驗證了一下,

測試環境wince6.0

主函式所在的檔案testthread.cpp

// testthread.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include "threadtest.h"

//#include "pwinuser.h"

handle threadhdary[maxtdlen];

dword threadidary[maxtdlen];

int threadparam[maxtdlen];

bool finishflag[maxtdlen];

unsigned int threadcnt;

//critical_section g_cs;

int _tmain(int argc, _tchar* argv)

else

if (null!=threadhdary[threadcnt])

}// bool rtnflag=false;

// threadparam[threadcnt]=threadcnt+1;

// threadhdary[threadcnt]=createthread(

// null, //lpsecurity_attributes lpsa,the security attributes for the new thread.

// null, //dword dwstacksize,the stack size for the new thread.

// thread1proc, //the thread procedure of the new thread.

// threadparam+threadcnt, //void* pvparam,the parameter to be passed to the thread procedure.

// create_suspended, //0:立即執行,create_suspended;需要被喚醒

// &threadidary[threadcnt]); //[out] address of the dword variable that, on success, receives the thread id of the newly created thread.

// rtnflag=cesetthreadpriority(threadhdary[threadcnt],120);

// //rtnflag=cesetthreadpriority(threadhdary+threadcnt,255);//最初寫錯了

// // dword tempdw=getlasterror();

// if (!rtnflag)

//

// threadcnt++;

// threadparam[threadcnt]=threadcnt+1;

// threadhdary[threadcnt]=createthread(

// null, //lpsecurity_attributes lpsa,the security attributes for the new thread.

// null, //dword dwstacksize,the stack size for the new thread.

// thread2proc, //the thread procedure of the new thread.

// threadparam+threadcnt, //void* pvparam,the parameter to be passed to the thread procedure.

// 0, //0:立即執行,create_suspended;需要被喚醒

// &threadidary[threadcnt]); //[out] address of the dword variable that, on success, receives the thread id of the newly created thread.

// rtnflag=cesetthreadpriority(threadhdary[threadcnt],120);

// if (!rtnflag)

//

// // threadcnt++;

// threadparam[threadcnt]=threadcnt+1;

// threadhdary[threadcnt]=createthread(

// null, //lpsecurity_attributes lpsa,the security attributes for the new thread.

// null, //dword dwstacksize,the stack size for the new thread.

// thread3proc, //the thread procedure of the new thread.

// threadparam+threadcnt, //void* pvparam,the parameter to be passed to the thread procedure.

// 0, //0:立即執行,create_suspended;需要被喚醒

// &threadidary[threadcnt]); //[out] address of the dword variable that, on success, receives the thread id of the newly created thread.

// rtnflag=cesetthreadpriority(threadhdary[threadcnt],60);

// if (!rtnflag)

//

while (true)

sleep(100);

} for (int i=0;i執行緒函式體定義放在了另外乙個cpp裡

//critical_section g_cs;

#include "stdafx.h"

#include "threadtest.h"

critical_section g_cs;

fstream testfile;

dword threadproc(lpvoid lpparm)//測試移入.h

{ int exterparam=*(int *)lpparm;

int threadparam=exterparam-1;//相對量

while(exterparam)

{ entercriticalsection(&g_cs);

exterparam=*(int *)lpparm;

//cout<<"thread "<

#ifndef thread_test_h

#define thread_test_h

#include "iostream"

#include "winbase.h"

#include "fstream"

using namespace std;

using namespace std;

#define maxtdlen 20

dword threadproc(lpvoid);

dword thread1proc(lpvoid);

dword thread2proc(lpvoid);

dword thread3proc(lpvoid);

extern critical_section g_cs;

extern fstream testfile;

extern bool finishflag[maxtdlen];

// dword thread1proc(lpvoid lpparm)//測試移入.h

// {

// while(true)

// {

// entercriticalsection(&g_cs);

// int cnt=*(int *)lpparm;

// cout<<"thread "《還有一點,wince6.0支援的最大程序數增加了,不止32個。

linux同步機制

一.併發控制 1 自旋鎖 得不到資源,會原地打轉,直到獲得資源為止 定義自旋鎖 spinlock t spin 初始化自旋鎖 spin lock init lock 獲得自旋鎖 spin lock lock 獲得自旋鎖,如果能立即獲得,則馬上返回,否則自旋在那裡,直到該自旋鎖的保持者釋放 spin ...

MySQL 同步機制

innodb沒有使用作業系統同步機制,而是自己封裝,通過spin 自旋 和wait array 等待佇列 的設計提高效能 目前的cpu都支援tas指令。該指令通過讀取乙個位元組或者乙個word,然後和0比較,並且無條件的將其在記憶體中的值設為1,是原子操作。用到swap atomic操作,將記憶體中...

執行緒同步機制

本週主要學習 執行緒同步機制 互斥量 讀寫鎖和條件變數 和簡單程式的實現,對執行緒同步有了進一步認識 內容如下 執行緒的基本概念,相關函式 互斥量 說明 處於標圓形框之上的線段表示相關的執行緒沒有擁有互斥量 處於圓形框中心線之上的線段表示相關的執行緒等待互斥量 處於圓形框中心線之下的線段表示相關的執...