Last active
February 28, 2019 13:25
-
-
Save segfo/1674c05cec6cbd212f40d9a600effe27 to your computer and use it in GitHub Desktop.
名前付きMutexをつくるだけ。動作確認用。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C版 | |
// Rust版で作成するMutex名と同じにすると、競合させられる。 | |
#include<windows.h> | |
#include<stdio.h> | |
int main() { | |
// 名前付きミューテックスを作り、確保する | |
CreateMutexA(NULL,0,"AAAAA_NAMED_MUTEX\0"); | |
if(GetLastError()==183){ | |
// すでに獲得済みのミューテックスの場合は183がGetLastErrorから返却される。 | |
printf("すでに名前付きミューテックスが作成されています。"); | |
return 0; | |
} | |
printf("Enterを押すと終了\n"); | |
char s[3]; | |
fgets(s,2,stdin); | |
printf("おわり\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rust版 | |
// C版で作成するMutex名と同じにすると、競合させられる。 | |
fn main() { | |
// 名前付きミューテックスを作り、確保する | |
unsafe{ | |
let ret = kernel32::CreateMutexA( | |
std::ptr::null_mut(), | |
0, | |
"AAAAA_NAMED_MUTEX\0".as_ptr() as *const i8, | |
); | |
match kernel32::GetLastError(){ | |
// すでに獲得済みのミューテックスの場合は183がGetLastErrorから返却される。 | |
183 => { | |
println!("すでに名前付きミューテックスが作成されています。"); | |
return; | |
}, | |
_=>{} | |
}; | |
} | |
print!("Enterを押すと終了"); | |
let mut s=String::new(); | |
std::io::stdin().read_line(&mut s).unwrap(); | |
println!("おわり"); | |
} | |
/* Cargo.tomlは以下のような感じ。 | |
[package] | |
name = "test_namedmutex" | |
version = "0.1.0" | |
edition = "2018" | |
[dependencies] | |
kernel32-sys = "0.2.2" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment