我有两个应用程序,一个WinForms应用程序和一个Windows服务,它们都运行在同一台机器上.我希望WinForms应用程序能够可靠地检测服务何时运行.我完全控制了两个应用程序的设计和实现.
我的第一个想法是使用Mutex,由服务实例化并由WinForms App检测.
有更好的设计吗?
我有一个共享内存池,许多不同的线程可以从中请求分配.从这个请求分配将在每个线程中发生很多,但是线程的数量可能很小,通常只有1个线程在运行.我不确定以下哪种方法可以解决这个问题.
最终我可能需要实现两者并看看哪个产生更有利的结果......我也担心即使考虑#2也可能是过早的优化,因为我实际上并没有使用这个共享资源编写的代码.但问题是如此有趣,以至于它继续分散我对其他工作的注意力.
1)创建互斥锁并让线程在获取分配之前尝试锁定它,然后解锁它.
2)让每个线程注册一个请求槽,当需要分配时,它将请求放入槽中,然后阻塞(while(result == NULL){usleep()})等待请求槽有结果.单个线程连续迭代请求时隙,进行分配并将它们分配给请求时隙中的结果.
数字1是简单的解决方案,但如果时机正确,单个线程可能会占用锁定.第二个更复杂,但是当从资源中提取时,确保线程之间的公平性.但是它仍然会阻塞请求线程,如果有很多线程,迭代可以在没有进行任何实际分配的情况下刻录周期,直到找到要满足的请求.
注意:使用pthreads在Linux上使用C语言
我有互斥的问题我有这个代码,我不知道为什么它不能正常工作...
#include <windows.h>
#include <process.h>
#include <stdio.h>
HANDLE mutex;
unsigned _stdcall t(void*){
printf(":D:D:D\n");
return NULL;
}
int main(){
mutex=CreateMutex(NULL,FALSE,NULL);
WaitForSingleObject(mutex,INFINITE);
_beginthreadex(NULL,NULL,&t,NULL,0,NULL);
WaitForSingleObject(mutex,INFINITE);
printf("HD\n");
}
Run Code Online (Sandbox Code Playgroud)
结果是:
HD
:D:D:D
Run Code Online (Sandbox Code Playgroud)
我希望不要在控制台看到高清......
但是这段代码工作正常
HANDLE mutex;
unsigned _stdcall t(void*){
WaitForSingleObject(mutex,INFINITE);
printf(":D:D:D\n");
ReleaseMutex(mutex);
return NULL;
}
int main(){
mutex=CreateMutex(NULL,FALSE,NULL);
WaitForSingleObject(mutex,INFINITE);
_beginthreadex(NULL,NULL,&t,NULL,0,NULL);
printf("HD\n");
while(1){
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
HD
Run Code Online (Sandbox Code Playgroud)
谢谢大家....
我目前正在使用 win32 API 用 C 编程。
我想知道如何永久删除或取消互斥锁和信号量。
我该怎么做,直到现在我在 MSDN 中还没有找到任何关于它的信息。
谢谢!
免责声明:Boost和C++ 11都不允许.
我有一个程序,我在其中创建了一个实例,Foo并在多个线程中使用它.然后我想要安全地删除它,以便这些线程不会陷入分段错误.
Foo每次线程函数运行时,我都会添加一个互斥锁成员并将其锁定.为了使不同的线程不相互冲突.
class Foo
{
public:
pthread_mutex_t mutex;
};
void* thread ( void* fooPtr )
{
Foo* fooPointer = (Foo*) fooPtr;
while ( 1 )
{
if ( fooPointer )
{
pthread_mutex_lock ( &fooPointer->mutex );
/* some code, involving fooPointer */
pthread_mutex_unlock ( &fooPointer->mutex );
}
else
{
pthread_exit ( NULL );
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要foo安全删除,因此线程中不会发生错误.我添加了一个析构函数Foo:
Foo::~Foo()
{
pthread_mutex_lock ( &mutex );
}
Run Code Online (Sandbox Code Playgroud)
现在,在所有线程完成当前循环之前,不会删除该对象.
问题是:删除实例后是否会解锁互斥锁?删除实例后,所有线程都会完成吗?我打赌答案是no.所以我改变了析构函数,但现在看起来线程不安全:
Foo::~Foo()
{
pthread_mutex_lock ( …Run Code Online (Sandbox Code Playgroud) 我会做一个假设的场景,只是为了清楚我需要知道什么。
假设我有一个经常更新的文件。
我需要通过几个不同的线程读取和解析这个文件。
每次重写此文件时,我都会唤醒一个条件互斥锁,以便其他线程可以为所欲为。
我的问题是:
如果我有 10000 个线程,第一个线程执行会阻塞其他 9999 个线程的执行吗?
它是并行工作还是同步工作?
下面是示例 Poco 线程程序,以了解互斥锁和线程同步。仍然看到同一程序的不同输出。
#include "Poco/ThreadPool.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Mutex.h"
#include <iostream>
#include <unistd.h>
using namespace std;
class HelloRunnable: public Poco::Runnable
{
public:
static int a;
HelloRunnable(){
}
HelloRunnable(unsigned long n):_tID(n){
}
void run()
{
Poco::Mutex::ScopedLock lock(_mutex);
std::cout << "==>> In Mutex thread " << _tID << endl;
int i;
for (i=0;i<50000;i++)
{
a = a+1;
}
Poco::Mutex::ScopedLock unlock(_mutex);
}
private:
unsigned long _tID;
Poco::Mutex _mutex;
};
int HelloRunnable::a = 0;
int main(int argc, char** argv)
{
Poco::Thread thread1("one"), …Run Code Online (Sandbox Code Playgroud) https://en.cppreference.com/w/cpp/thread/lock_tag
void transfer(bank_account &from, bank_account &to, int amount)
{
// lock both mutexes without deadlock
std::lock(from.m, to.m);
// make sure both already-locked mutexes are unlocked at the end of scope
std::lock_guard<std::mutex> lock1(from.m, std::adopt_lock);
std::lock_guard<std::mutex> lock2(to.m, std::adopt_lock);
// equivalent approach:
// std::unique_lock<std::mutex> lock1(from.m, std::defer_lock);
// std::unique_lock<std::mutex> lock2(to.m, std::defer_lock);
// std::lock(lock1, lock2);
from.balance -= amount;
to.balance += amount;
}
Run Code Online (Sandbox Code Playgroud)
通过一次锁定两个互斥锁可以获得什么?
他们在这里得到了什么?
请解释他们的决定背后的原因.
我无法理解如何在互斥锁中修改 Option。
当没有选项时,它工作正常
let mut my_int = Arc::new(Mutex::new(5));
let my_int_clone = Arc::clone(&my_int);
thread::spawn(move || {
let mut my_int = my_int_clone.lock().unwrap();
*my_int += 1;
}).join();
let my_int_clone_print = Arc::clone(&my_int);
println!("Value: {}", my_int_clone_print.lock().unwrap());
Run Code Online (Sandbox Code Playgroud)
但是,当我将值包装在 中时Some,我必须手动使用ref mut等(我从这里找到它),因为lock().unwrap()返回的是MutexGuard,而不是Option本身。
let mut my_int = Arc::new(Mutex::new(Some(5)));
let my_int_clone = Arc::clone(&my_int);
thread::spawn(move || {
let mut my_int = my_int_clone.lock().unwrap();
match *my_int {
Some(ref mut val) => {
*val += 1;
},
None => {
println!("Value is …Run Code Online (Sandbox Code Playgroud) 在我的代码库中,我有两个看起来像这样的函数:
void inside() {
lock_guard<mutex> lock(LOCK);
cout << "Finished inside" << endl;
}
void outside() {
lock_guard<mutex> lock(LOCK);
inside();
cout << "Finished outside" << endl;
}
Run Code Online (Sandbox Code Playgroud)
这会导致我的代码库中出现死锁,我觉得这很奇怪,因为我的印象是 lock_guard 在超出范围时会被破坏。我也尝试过使用 unique_lock,但得到了相同的结果。我能够解决它的唯一方法是在调用内部之前调用 unlock:
void outside() {
LOCK.lock();
// Do stuff
LOCK.unlock();
inside();
cout << "Finished outside" << endl;
}
Run Code Online (Sandbox Code Playgroud)