简体   繁体   English

互斥锁和许多线程

[英]Mutex lock and a number of threads

I have an API (written in C ) that permits any number of incoming (concurrent) connections. 我有一个API(用C编写),允许任意数量的传入(并发)连接。 Each connection is handled by an independet pthread created whenever a client connects to the API. 每当客户端连接到API时,每个连接都由一个独立的pthread处理。 Each of these connections can (but do not have to) change properties of the server such that requests should not be processed at the same time. 这些连接中的每一个都可以(但不必)更改服务器的属性,以便不应同时处理请求。

My code basically follows this structure: 我的代码基本上遵循这个结构:

pthread_mutex_t lock;
void request_handler(char * request)
{
    pthread_mutex_lock(&lock);
    process_request(request);
    pthread_mutex_unlock(&lock);
}

Assume now one request is being processed that takes a long time (eg ten seconds). 现在假设正在处理一个需要很长时间(例如十秒)的请求。 In this time, five other requests come in, so five additional pthreads will reach at the pthread_mutex_lock function. 在这个时候,有五个其他请求进入,因此pthread_mutex_lock函数将会有另外五个pthreads

  • Do they just wait there and continue as intended (one after another is served)? 他们只是在那里等待并按预期继续(一个接一个送达)?

The reason why I'm asking this is that this is what I'd expect but I haven't found an example with more than one concurrent thread in the official documentation. 我问这个的原因是这是我所期望的,但我没有找到官方文档中有多个并发线程的示例。

  • Is there a guarantee that the requests will be processed in the same order in which have been received or will any single one of the five waiting threads be allowed to start after the long request finished? 是否保证请求将按照已接收的相同顺序处理,或者在长请求完成后是否允许五个等待线程中的任何一个启动?

Strict in-order execution is not needed by my code, but I'd like to know beforehand what to expect in order to design my code properly. 不需要严格的顺序执行我的代码,但我想知道,以正确设计我的代码,以期望事先什么。

I also read about recursive mutex , but I'd like to avoid them due to a number of reasons. 我也读过有关递归mutex ,但由于种种原因,我想避免使用它们。 Furthermore, my code will not try to lock multiple times from one single pthread by construction. 此外,我的代码不会尝试通过构造从一个单独的pthread锁定多次。

The mutex ensures that exactly one thread enters the critical section of your code, in your case, the call to process_request() , at a time. 互斥锁确保只有一个线程进入代码的关键部分,在您的情况下,一次调用process_request() Once a thread t obtains the lock, any subsequent thread must wait until t releases it. 一旦线程t获得锁,任何后续的线程必须等待,直到t释放它。

If multiple such threads arrive, who gets a chance to go first depends on the scheduling of the threads by the operating system, which is not deterministic and can be different each time you run the program. 如果多个这样的线程到达,谁有机会先行取决于操作系统对线程的调度,这不是确定性的,并且每次运行程序时都可能不同。 However, the guarantee is that it will be only one thread that can pass, each time. 但是,保证每次只能传递一个线程。

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

相关问题 互斥锁线程 - Mutex lock threads 互斥锁是锁定但其他线程正在进入关键部分 - Mutex is lock but other threads are entering in critical section 如何为各种线程锁定pthread_mutex_lock - how to lock for a variety of threads pthread_mutex_lock 尝试获取pthread_mutex_lock(&mutex)的线程如果他们没有获得锁定会发生什么? - Threads trying to acquire pthread_mutex_lock(&mutex) What happens if they don't get the lock? Mutex锁定主函数和pthread中其他线程之间的优先级 - Mutex lock priority between main function and the other threads in pthread 什么是多线程等待的`pthread_mutex_lock()`唤醒命令? - What is the `pthread_mutex_lock()` wake order with multiple threads waiting? 严格使用 mutex_lock 和 mutex_unlock 在 2 个线程之间切换作为学术练习 - Strictly using mutex_lock & mutex_unlock to switch between 2 threads as an academic exercise 使用 Mutex 或 Semaphor 修复程序的最大线程数 - Fix maximum number of threads for a program using a Mutex or Semaphor 如何完成多个线程以多次加入一个进程? (pthread_mutex_lock) - How can I accomplish multiple threads to join multiple times into a process? (pthread_mutex_lock) 使用Windows API 使用互斥锁方法创建100个线程导致意外结果,如何解决? - Using Windows API to create 100 threads with a mutex lock method induces unexpected result, how to fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM