简体   繁体   English

C线程同步

[英]C thread synchronization

I have a problem of using multiple threads and sync them in a proper order. 我有一个使用多个线程并以适当顺序同步它们的问题。 The problem states the following: I have 44 threads from 1-44. 问题指出以下几点:我有1-44个线程中的44个。 I can have a maximum of 4 threads that can enter the critical area at a moment. 我最多可以有4个线程可以同时进入关键区域。 Thread number 12 must close only when there are 4 threads in the critical area, including them. 仅当关键区域中有4个线程(包括它们)时,才必须关闭线程号12。 The problem is the following: sometimes the thread closes well, sometimes it doesn't. 问题如下:有时线程关闭良好,有时没有。 I don't know when it is going to come, it might be the last one, so i forced it to be the first one that enters the critical area. 我不知道什么时候会到来,它可能是最后一个,所以我强迫它成为进入关键领域的第一个。 This is the code sample that sync the 44 threads: 这是同步44个线程的代码示例:

void *thread_sync_P7(void *arg){
    int *val = (int*)arg;

    if(*val != 12) {
        sem_wait(&semP7_2);
    }

    sem_wait(&semP7_1);
    info(BEGIN, 7, *val);

    if(*val == 12) {
        sem = 0;
        for(int i = 0;i < threadP7 - 1;i++){
            sem_post(&semP7_2);
        }
    }


    info(END, 7, *val);
    sem_post(&semP7_1);

    return NULL;   
}

sempP7_1 sync the critical area to stop more than 4 threads being there at one, and semP7_2 makes sure that the first thread to enter the area is thread with id 12. Sometimes this happens: sempP7_1同步关键区域以停止其中有4个以上的线程,并且semP7_2确保进入该区域的第一个线程是ID为12的线程。有时会发生这种情况:

[T] BEGIN P7 T12 pid=24850 ppid=24848 tid=825038592

[T]  END  P4 T3 pid=24848 ppid=24846 tid=900572928

[T] BEGIN P7 T2 pid=24850 ppid=24848 tid=908965632

[T]  END  P4 T1 pid=24848 ppid=24846 tid=917358336

[T] BEGIN P7 T3 pid=24850 ppid=24848 tid=900572928

[T]  END  P5 T0 pid=24851 ppid=24847 tid=925882176

[T]  END  P7 T12 pid=24850 ppid=24848 tid=825038592

[T] BEGIN P7 T4 pid=24850 ppid=24848 tid=892180224

As you can see, P7 T12 is the thread with id 12. It is followed by T2 an T3, but it closes and there are only 3 threads displayed. 如您所见,P7 T12是ID为12的线程。其后是T2和T3,但是它关闭并且仅显示3个线程。 How can i sync them better? 我如何更好地同步它们?

You have several ways to implement this: 您有几种方法可以实现此目的:

  1. (waiting) Use a condition variable associated with the final condition. (等待)使用与最终条件关联的条件变量。 Your thread number 12 will be waiting for that condition to be true, and the other threads will evaluate the condition and awaken it when the condition becomes true. 您的线程号12将等待该条件成立,其他线程将评估该条件并在该条件成立时将其唤醒。 As each thread enters into the critical region, it evaluates the condition and if true, awakens the thread. 当每个线程进入关键区域时,它会评估条件,如果为true,则会唤醒线程。 As each thread gets out of the critical region, reevaluates the condition (this mean to increment/decrement a variable with the number of threads in the critical region) Onde the thread 12 is awaken, it can close (with closing meaning whatever you want) In this case your thread is waiting for the condition to happen to close . 当每个线程离开关键区域时,请重新评估条件(这意味着使用关键区域中的线程数来增加/减少变量)唤醒线程12后,它可以关闭( 关闭表示您想要的任何含义)在这种情况下,您的线程正在等待条件关闭

  2. (polling) In case your thread has something to do until it is about to decide if to close or not to close , just evaluate the condition on the other threads on entry to the critical region and store it (you need a critical region nested on the first to achieve this). (轮询)如果您的线程在决定要关闭还是不关闭之前有事要做,只需在进入临界区时评估其他线程的条件并存储它(您需要嵌套在临界区上)首先实现这一目标)。 You can have also a boolean variable indicating if the condition has been reached in the past (we have already reached the four threads inside, even if now there are not those four threads) Then, your thread has freedom to make another task and only evaluate if it has to close at certain points in time. 您还可以具有一个布尔变量,该变量指示过去是否已达到条件(即使现在没有这四个线程,我们也已经到达了其中的四个线程)然后,您的线程可以自由执行另一项任务,并且仅求值是否必须在某些时间关闭

  3. (signalled) In case you have to asynchronously signal the process to leave what it can be doing and close , just send it a signal. (已标记)如果必须异步发送信号以离开进程可以关闭关闭它,则只需发送一个信号即可。 This will interrupt the thread execution and you can decide to abort your task or continue after the signal on your signal handler. 这将中断线程的执行,您可以决定中止您的任务,或者在信号处理程序上发出信号后继续执行。 Beware that handling signals (you send signals to a process, which normally is a group of threads) and attaching them to specific threads of a process is something difficult to properly achieve, you have to decide which threads are going to handle what kind of signals, and this is an elaborate thing. 注意处理信号(将信号发送到一个进程,通常是一组线程)并将它们附加到进程的特定线程上是很难正确实现的,因此,您必须确定哪些线程将处理哪种信号,这是一件很复杂的事情。

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

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