简体   繁体   English

使用互斥量和信号量

[英]Using Mutex and semaphore

I am confused that what should I use either a mutex or a semaphore in my application,basically my application is a multithreaded server programmed using C and Pthreads.我很困惑我应该在我的应用程序中使用互斥锁还是信号量,基本上我的应用程序是一个使用 C 和 Pthreads 编程的多线程服务器。 In my application one thread has a dependency over the other ie one thread needs to wait until a condition is met in the other thread.在我的应用程序中,一个线程依赖于另一个线程,即一个线程需要等待,直到另一个线程满足条件。 Earlier I was using the While loop to check when the other thread set the condition as true,but While loop consumes CPU cycles needlessly ie CPU consumption goes upto 100%.早些时候我使用 While 循环来检查其他线程何时将条件设置为 true,但 While 循环不必要地消耗 CPU 周期,即 CPU 消耗高达 100%。

Currently I started using a mutex in my application as follows:目前我开始在我的应用程序中使用互斥锁,如下所示:

pthread_mutex_lock(&t_data[rc].mutex);
pthread_mutex_unlock(&t_data[rc].mutex);

In one thread I lock the mutex and when the condition is met in the second thread I unlock it in the second thread( I have handled this doing indexing in a structure,in Which along which other items I have kept a mutex field, each thread is assigned an index when a new client makes a connection ).Using this everything is working fine and CPU consumption of server has came down to 2%.But I have one issue in my mind.在一个线程中,我锁定了互斥锁,当第二个线程中满足条件时,我在第二个线程中将其解锁(我已经在结构中进行了索引处理,其中其他项目我保留了一个互斥体字段,每个线程当一个新客户端建立连接时被分配一个索引。使用它一切正常,服务器的 CPU 消耗下降到 2%。但我有一个问题。

As the definition of mutex says that consider if 10 threads are running and they are sharing a common resource suppose some global variables,so when one thread locks a mutex then other threads cannot access the shared resource until the thread which has locked the mutex releases it.Same will be the case with my application.正如互斥锁的定义所说,如果有 10 个线程正在运行并且它们共享一个公共资源假设一些全局变量,那么当一个线程锁定互斥锁时,其他线程无法访问共享资源,直到锁定互斥锁的线程释放它.我的申请也是如此。 consider I have 10 active threads 5 threads will lock the mutex turn by turn and other 5 will release the mutex.考虑我有 10 个活动线程,其中 5 个线程将依次锁定互斥锁,而其他 5 个线程将释放互斥锁。 If a thread has locked the mutex then other 4 threads need to wait until it has been released.如果一个线程锁定了互斥锁,则其他 4 个线程需要等待直到它被释放。 so at some point of time a deadlock condition might occur if a thread locks a mutex and it didn't get released then all other threads will keep waiting.所以在某个时间点,如果一个线程锁定了一个互斥锁并且它没有被释放,那么可能会发生死锁情况,然后所有其他线程将继续等待。

please help me get out of this issue.Being Theoretical it might look awkward but it is a real case scenario.Please go through the question again before giving a downvote.请帮我解决这个问题。从理论上讲,它可能看起来很尴尬,但这是一个真实的案例场景。请在给出否决票之前再次检查这个问题。

In your case consider use conditional variable.在您的情况下,请考虑使用条件变量。 Make the 5 threads waiting on the condvar, and if the thread is done, signal the condvar if you want one other thread to continue run, or call broadcast to let all other blocking threads continue to run.让 5 个线程在 condvar 上等待,如果线程完成,如果您希望其他线程继续运行,则向 condvar 发出信号,或者调用广播让所有其他阻塞线程继续运行。 Checkout these pthreads API:查看这些 pthreads API:

pthread_cond_wait to wait on the condition
pthread_cond_signal  to signal one thread of the waiters
pthread_cond_broadcast  to signal all threads that are waiting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM