c 多執行緒,2個執行緒模擬賣火車票的小程式

2021-06-27 02:14:44 字數 944 閱讀 1789

如果遇到「createmutexw」: 不能將引數 3 從「const char [8]」轉換為「lpcwstr」的錯誤

可以在vs2010中,將專案的字符集,從「使用unicode字符集」改為「使用多位元組字符集」

#include 

<

windows.h

>

#include 

<

iostream.h

>

dword winapi fun1proc(lpvoid lpparameter);

//thread data

dword winapi fun2proc(lpvoid lpparameter);

//thread data

intindex=0

;int

tickets=10

;handle hmutex;

void

main()

}waitforsingleobject(hmutex,infinite);

releasemutex(hmutex);

releasemutex(hmutex);

sleep(

4000);}

//執行緒1的入口函式

dword winapi fun1proc(lpvoid lpparameter)

//thread data

else

break

;releasemutex(hmutex);

}return0;

}//執行緒2的入口函式

dword winapi fun2proc(lpvoid lpparameter)

//thread data

else

break

;releasemutex(hmutex);

}return0;

}

多執行緒賣火車票的問題

同步 塊 synchronized object 其中object為我們使用的同步鎖,對於繼承的方式,使用this或者建立乙個非靜態物件作為鎖是無用的,無法阻止錯誤資料的產生。所以我們使用static的物件 同步方法 public synchronized void method 由於同步方法的鎖使...

iOS執行緒鎖NSLock案例 賣火車票

import rootviewcontroller.h inte ce rootviewcontroller 宣告總票數 property nonatomic assign nsinteger totaltickets 宣告剩餘票數 property nonatomic assign nsinteg...

使用多執行緒實現賣火車票

1.使用多執行緒實現三個視窗同時售票,用繼承thread方式 package com public class deamo extends thread else catch interruptedexception e public static void main string args 執行後...