c語言用鎖鎖住一個函數(shù),怎樣鎖住函數(shù)

(C語言中)互斥鎖的死鎖問題

如果你將mutex_c換成mutex_p,則不會死鎖,因為,你第一個線程鎖上后,切換到第二個線程,因為mutex_p未釋放,第二個線程無法獲取mutex_p,進(jìn)入等待狀態(tài),此時OS將再次調(diào)度第一個線程,直到第一個線程釋放mutex_p之后,第二個線程才會被激活,然后調(diào)試第二線程,獲取mutex_p.

成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、蓬溪網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為蓬溪等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

使用OS提供的互斥量來保護(hù)公共資源還是比較安全的,但如果用二值信號量的話,就可能會有優(yōu)先級反轉(zhuǎn)的情況.

密碼鎖c語言編程代碼

#include?stdio.h

#include?string.h

#include?stdlib.h

int?main()

{

char?password[10],password2[10];

memset(password,0,sizeof(password));

memset(password2,0,sizeof(password2));

printf("請設(shè)置8位數(shù)以內(nèi)密碼:\n\n");

scanf("%s",password);

printf("請設(shè)置校驗密碼:\n\n");

scanf("%s",password2);

if(atoi(password2)==atoi(password))

{

printf("密碼輸入正確!:\n");

}

else

{

printf("密碼輸入錯誤!:\n");

}

return?0;

}

求51單片機(jī)C語言編的密碼鎖程序

#include reg52.h

#define uint unsigned int

#define uchar unsigned char

#define BIN(a,b,c,d,e,f,g,h) ((a7)+(b6)+(c5)+(d4)+(e3)+(f2)+(g1)+(h0))

//下面的code表示數(shù)組存放在ROM中,因為這個數(shù)組的值不需要改寫

uchar code KeyCode[16]={15,14,12,8,30,28,24,16,60,56,48,32,120,112,96,64};//值為m*(n+1)的乘積,用于Key()

uchar dis[6];

msdelay(uint x)//延時子函數(shù)

{uchar j;

while(x--)

{for(j=0;j125;j++){;}

}

}

//鍵盤子程序一,鍵盤值與數(shù)組值對比得到

uchar Key(void)

{uchar temp,m,n,i,j,matrix,k;

P1=0xF0; /*行線電平為高,列線為低*/

temp=P10xf0;

if (temp==0xf0) return(16); /*行仍為高,無按健,退出*/

else msdelay(10);

for(i=1;i16;i=i*2)

{m=i;

for(j=1;j16;j=j*2)

{n=(~j)0x0f;

P1=(m4)|n; /*m為P1的行值由i循環(huán)得到,n為列值,由j循環(huán)并取反得到*/

temp=P10xf0;

if (!temp)

{do{temp=P10xf0;}while(!temp);

matrix=m*(n+1);/*為避免乘積重復(fù),n+1*/

for(k=0;k16;k++){if (matrix==KeyCode[k]) return(k);} //KeyCode:見前

return(16);

} //if loop

}//j loop

}//i loop

}//Key end

//用Switch...case語句得到鍵盤值*/

uchar Key1(void)

{uchar temp,m,n,i,j,matrix;

P1=0xF0; /*行線電平為高,列線為低*/

temp=P10xf0;

if (temp==0xf0) return(16); /*行仍為高,無按健,退出*/

else msdelay(10);

for(i=1;i16;i=i*2)

{m=i;

for(j=1;j16;j=j*2)

{n=(~j)0x0f;

P1=(m4)|n;/*m為P1的行值由i循環(huán)得到,n為列值,由j循環(huán)并取反得到*/

temp=P10xf0;

if (!temp)

{do{temp=P10xf0;}while(!temp);

matrix=m*(n+1);

switch(matrix) //此方法的基本思路:

{case 15:return(1); break; //由循環(huán)得到的m,n值賦于P1端口實現(xiàn)逐個鍵掃描

case 14:return(2); break; //同時由m,n+1的值相乘得到對應(yīng)鍵點(diǎn)de的積

case 12:return(3); break; //m*(n+1)值掃描鍵點(diǎn)對應(yīng)而得出鍵值

case 8:return(4); break; //

case 30:return(5); break; //

case 28:return(6); break; //

case 24:return(7); break; //

case 16:return(8); break;

case 60:return(9); break;

case 56:return(0); break;

case 48:return(10); break;

case 32:return(11); break;

case 120:return(12); break;

case 112:return(13); break;

case 96:return(14); break;

case 64:return(15); break;

default:return(16);

} //switch end

} //if loop

}//j loop

}//i loop

}//Key end

//依次掃描16個按鍵

uchar Key2(void)

{uchar temp;

P1=0xF0; /*使P1=1111 0000,行線電平為高,列線為低*/

temp=P10xf0;

if (temp==0xf0) return(16); /*讀P1=1111 xxxx,表示行仍為高,無按健,退出(x表示不關(guān)心)?/

else msdelay(10);

P1=0x1e; /*P1=0001 1110,行一為高,列一為低,掃描第一個按鍵*/

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(1);}

P1=0x1d; /*P1=0001 1101,行一為高,列二為低,掃描第二個按鍵,下面掃描其余按鍵*/

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(2);}

P1=0x1b;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(3);}

P1=0x17;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(4);}

P1=0x2e;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(5);}

P1=0x2d;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(6);}

P1=0x2b;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(7);}

P1=0x27;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(8);}

P1=0x4e;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(9);}

P1=0x4d;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(0);}

P1=0x4b;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(10);}

P1=0x47;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(11);}

P1=0x8e;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(12);}

P1=0x8d;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(13);}

P1=0x8b;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(14);}

P1=0x87;

temp=P10xf0;

if (!temp) {do{temp=P10xf0;}while(!temp);

return(15);}

return(16); //掃描all按鍵都未按下,則輸出16

}//Key2 end.

////////時鐘中斷顯示子程序

void T0_int() interrupt 1

{static uchar i;

if (i==6){i=0;}

P0=5-i;

P0=P0|(dis[i]4);

i++;

TL0=0;

TH0=252;}

void distri(uint disnum)

{uint temp;

dis[0]=0;

dis[1]=disnum/10000;

temp=disnum%10000;

dis[2]=temp/1000;

temp=temp%1000;

dis[3]=temp/100;

temp=temp%100;

dis[4]=temp/10;

dis[5]=temp%10;

}

Main()

{uchar KeyVal,i=0;

TMOD=0x01;

IE=0x82;

TH0=252;

TL0=0;

TR0=1;

distri(0);

do{

KeyVal=Key();

if (KeyVal!=16) dis[1]=KeyVal; //注意:當(dāng)有按鍵時才賦于顯示位dis[1],否則出錯,請分析!

}while(1);

}

C語言queue如何加鎖和解鎖

有兩種辦法:

重載一個queue模板,把鎖封裝到重載的函數(shù)里

每次使用queue的函數(shù)的時候加鎖,使用完畢后,解鎖

本文題目:c語言用鎖鎖住一個函數(shù),怎樣鎖住函數(shù)
轉(zhuǎn)載源于:http://muchs.cn/article38/phijsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計小程序開發(fā)、自適應(yīng)網(wǎng)站品牌網(wǎng)站制作、網(wǎng)站維護(hù)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司