Linux C语言 pthread_cond_wait()、pthread_cond_timedwait()函数(不允许cond被唤醒时产生竞争,所以需要和互斥锁搭配)

470 篇文章 71 订阅

man -f pthread_cond_wait(未看完,待更,,,)

PTHREAD_COND_TIMEDWAIT(3POSIX)                                       POSIX Programmer's Manual                                      PTHREAD_COND_TIMEDWAIT(3POSIX)

PROLOG	//序言
       This  manual  page is part of the POSIX Programmer's Manual.  The Linux implementation of this interface may differ (consult the corresponding Linux manual
       page for details of Linux behavior), or the interface may not be implemented on Linux.
       //本手册页是 POSIX 程序员手册的一部分。 
       //此接口的 Linux 实现可能不同(有关 Linux 行为的详细信息,请参阅相应的 Linux 手册页),或者该接口可能未在 Linux 上实现。

NAME
       pthread_cond_timedwait, pthread_cond_wait — wait on a condition	//条件等待

SYNOPSIS
       #include <pthread.h>

       int pthread_cond_timedwait(pthread_cond_t *restrict cond,
           pthread_mutex_t *restrict mutex,
           const struct timespec *restrict abstime);
       int pthread_cond_wait(pthread_cond_t *restrict cond,
           pthread_mutex_t *restrict mutex);

DESCRIPTION
       The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. The application shall ensure that these  functions  are
       called  with  mutex  locked  by  the calling thread; otherwise, an error (for PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior (for other
       mutexes) results.
       //pthread_cond_timedwait() 和 pthread_cond_wait() 函数将阻塞条件变量。 
       //应用程序应确保调用这些函数时调用线程锁定互斥锁; (为什么?)
       //否则,将导致错误(对于 PTHREAD_MUTEX_ERRORCHECK 和健壮的互斥锁)或未定义的行为(对于其他互斥锁)。

       These functions atomically release mutex and cause the calling thread to block on the condition variable cond;  atomically  here  means  ``atomically  with
       respect  to  access  by  another  thread  to the mutex and then the condition variable''. That is, if another thread is able to acquire the mutex after the
       about-to-block thread has released it, then a subsequent call to pthread_cond_broadcast() or pthread_cond_signal() in that thread shall  behave  as  if  it
       were issued after the about-to-block thread has blocked.
       //这些函数以原子方式释放互斥体并导致调用线程阻塞条件变量 cond; 
       //atomically 这里的意思是“相对于另一个线程访问互斥锁然后是条件变量的访问是原子的”。 
       //也就是说,如果另一个线程能够在即将阻塞的线程释放后获得互斥锁,
       //然后对 pthread_cond_broadcast() 或 pthread_cond_signal() 的后续调用在该线程中将表现得好像它是在即将阻塞的线程已阻塞之后发出的。

       Upon  successful  return,  the  mutex shall have been locked and shall be owned by the calling thread. If mutex is a robust mutex where an owner terminated
       while holding the lock and the state is recoverable, the mutex shall be acquired even though the function returns an error code.

       When using condition variables there is always a Boolean predicate involving shared variables associated with each condition  wait  that  is  true  if  the
       thread  should  proceed.  Spurious  wakeups  from  the  pthread_cond_timedwait()  or  pthread_cond_wait()  functions  may  occur.  Since  the  return  from
       pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated  upon  such
       return.

       When  a  thread waits on a condition variable, having specified a particular mutex to either the pthread_cond_timedwait() or the pthread_cond_wait() opera‐
       tion, a dynamic binding is formed between that mutex and condition variable that remains in effect as long as at least one thread is blocked on the  condi‐
       tion  variable.  During this time, the effect of an attempt by any thread to wait on that condition variable using a different mutex is undefined. Once all
       waiting threads have been unblocked (as by the pthread_cond_broadcast() operation), the next wait operation on that condition variable  shall  form  a  new
       dynamic  binding  with  the  mutex specified by that wait operation. Even though the dynamic binding between condition variable and mutex may be removed or
       replaced between the time a thread is unblocked from a wait on the condition variable and the time that it returns to the  caller  or  begins  cancellation
       cleanup, the unblocked thread shall always re-acquire the mutex specified in the condition wait operation call from which it is returning.

       A  condition  wait (whether timed or not) is a cancellation point. When the cancelability type of a thread is set to PTHREAD_CANCEL_DEFERRED, a side-effect
       of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation  cleanup
       handler.  The  effect  is  as  if  the  thread were unblocked, allowed to execute up to the point of returning from the call to pthread_cond_timedwait() or
       pthread_cond_wait(), but at that point  notices  the  cancellation  request  and  instead  of  returning  to  the  caller  of  pthread_cond_timedwait()  or
       pthread_cond_wait(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.

       A  thread that has been unblocked because it has been canceled while blocked in a call to pthread_cond_timedwait() or pthread_cond_wait() shall not consume
       any condition signal that may be directed concurrently at the condition variable if there are other threads blocked on the condition variable.

       The pthread_cond_timedwait() function shall be equivalent to pthread_cond_wait(), except that an error is  returned  if  the  absolute  time  specified  by
       abstime  passes (that is, system time equals or exceeds abstime) before the condition cond is signaled or broadcasted, or if the absolute time specified by
       abstime has already been passed at the time of the call. When such timeouts occur, pthread_cond_timedwait() shall nonetheless release  and  re-acquire  the
       mutex referenced by mutex, and may consume a condition signal directed concurrently at the condition variable.

       The condition variable shall have a clock attribute which specifies the clock that shall be used to measure the time specified by the abstime argument. The
       pthread_cond_timedwait() function is also a cancellation point.

       If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes  waiting  for  the  condition
       variable as if it was not interrupted, or it shall return zero due to spurious wakeup.

       The behavior is undefined if the value specified by the cond or mutex argument to these functions does not refer to an initialized condition variable or an
       initialized mutex object, respectively.

RETURN VALUE
       Except in the case of [ETIMEDOUT], all these error checks shall act as if they were performed immediately at the beginning of processing for  the  function
       and shall cause an error return, in effect, prior to modifying the state of the mutex specified by mutex or the condition variable specified by cond.

       Upon successful completion, a value of zero shall be returned; otherwise, an error number shall be returned to indicate the error.

ERRORS
       These functions shall fail if:

       ENOTRECOVERABLE
              The state protected by the mutex is not recoverable.

       EOWNERDEAD
              The  mutex  is a robust mutex and the process containing the previous owning thread terminated while holding the mutex lock. The mutex lock shall be
              acquired by the calling thread and it is up to the new owner to make the state consistent.

       EPERM  The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and the current thread does not own the mutex.

       The pthread_cond_timedwait() function shall fail if:

       ETIMEDOUT
              The time specified by abstime to pthread_cond_timedwait() has passed.

       EINVAL The abstime argument specified a nanosecond value less than zero or greater than or equal to 1000 million.

       These functions may fail if:

       EOWNERDEAD
              The mutex is a robust mutex and the previous owning thread terminated while holding the mutex lock. The mutex lock shall be acquired by the  calling
              thread and it is up to the new owner to make the state consistent.

       These functions shall not return an error code of [EINTR].

       The following sections are informative.

EXAMPLES
       None.

APPLICATION USAGE
       Applications  that  have  assumed  that non-zero return values are errors will need updating for use with robust mutexes, since a valid return for a thread
       acquiring a mutex which is protecting a currently inconsistent state is [EOWNERDEAD].  Applications that do not check the error returns, due to ruling  out
       the  possibility  of  such  errors  arising, should not use robust mutexes. If an application is supposed to work with normal and robust mutexes, it should
       check all return values for error conditions and if necessary take appropriate action.

RATIONALE
       If an implementation detects that the value specified by the cond argument to pthread_cond_timedwait() or pthread_cond_wait() does not refer to an initial‐
       ized  condition variable, or detects that the value specified by the mutex argument to pthread_cond_timedwait() or pthread_cond_wait() does not refer to an
       initialized mutex object, it is recommended that the function should fail and report an [EINVAL] error.

   Condition Wait Semantics
       It is important to note that when pthread_cond_wait() and pthread_cond_timedwait() return without error, the associated predicate may still be false.  Sim‐
       ilarly,  when  pthread_cond_timedwait() returns with the timeout error, the associated predicate may be true due to an unavoidable race between the expira‐
       tion of the timeout and the predicate state change.

       The application needs to recheck the predicate on any return because it cannot be sure there is another thread waiting on the thread to handle the  signal,
       and if there is not then the signal is lost. The burden is on the application to check the predicate.

       Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the condition variable is signaled simultane‐
       ously on different processors.

       In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition wait to determine whether  it  can
       safely  proceed,  should  wait  again,  or  should declare a timeout. A return from the wait does not imply that the associated predicate is either true or
       false.

       It is thus recommended that a condition wait be enclosed in the equivalent of a ``while loop'' that checks the predicate.

   Timed Wait Semantics
       An absolute time measure was chosen for specifying the timeout parameter for two reasons. First, a relative time measure can be easily implemented  on  top
       of  a  function that specifies absolute time, but there is a race condition associated with specifying an absolute timeout on top of a function that speci‐
       fies relative timeouts. For example, assume that clock_gettime() returns the current time and cond_relative_timed_wait() uses relative timeouts:

           clock_gettime(CLOCK_REALTIME, &now)
           reltime = sleep_til_this_absolute_time -now;
           cond_relative_timed_wait(c, m, &reltime);

       If the thread is preempted between the first statement and the last statement, the thread blocks for too long. Blocking, however, is irrelevant if an abso‐
       lute timeout is used. An absolute timeout also need not be recomputed if it is used multiple times in a loop, such as that enclosing a condition wait.

       For  cases  when  the  system  clock  is advanced discontinuously by an operator, it is expected that implementations process any timed wait expiring at an
       intervening time as if that time had actually occurred.

   Cancellation and Condition Wait
       A condition wait, whether timed or not, is a cancellation point. That is, the functions pthread_cond_wait() or pthread_cond_timedwait() are points where  a
       pending (or concurrent) cancellation request is noticed. The reason for this is that an indefinite wait is possible at these points—whatever event is being
       waited for, even if the program is totally correct, might never occur; for example, some input data being awaited might never be sent. By making  condition
       wait a cancellation point, the thread can be canceled and perform its cancellation cleanup handler even though it may be stuck in some indefinite wait.

       A  side-effect  of  acting on a cancellation request while a thread is blocked on a condition variable is to re-acquire the mutex before calling any of the
       cancellation cleanup handlers. This is done in order to ensure that the cancellation cleanup handler is executed in the same state  as  the  critical  code
       that  lies both before and after the call to the condition wait function. This rule is also required when interfacing to POSIX threads from languages, such
       as Ada or C++, which may choose to map cancellation onto a language exception; this rule ensures that each exception handler guarding  a  critical  section
       can always safely depend upon the fact that the associated mutex has already been locked regardless of exactly where within the critical section the excep‐
       tion was raised. Without this rule, there would not be a uniform rule that exception handlers could follow regarding the lock, and so coding  would  become
       very cumbersome.

       Therefore, since some statement has to be made regarding the state of the lock when a cancellation is delivered during a wait, a definition has been chosen
       that makes application coding most convenient and error free.

       When acting on a cancellation request while a thread is blocked on a condition variable, the implementation is required to ensure that the thread does  not
       consume any condition signals directed at that condition variable if there are any other threads waiting on that condition variable. This rule is specified
       in order to avoid deadlock conditions that could occur if these two independent requests (one acting on a thread and the  other  acting  on  the  condition
       variable) were not processed independently.

   Performance of Mutexes and Condition Variables
       Mutexes  are  expected  to be locked only for a few instructions. This practice is almost automatically enforced by the desire of programmers to avoid long
       serial regions of execution (which would reduce total effective parallelism).

       When using mutexes and condition variables, one tries to ensure that the usual case is to lock the mutex, access shared data, and unlock the mutex. Waiting
       on  a  condition variable should be a relatively rare situation. For example, when implementing a read-write lock, code that acquires a read-lock typically
       needs only to increment the count of readers (under mutual-exclusion) and return. The calling thread would actually wait on  the  condition  variable  only
       when  there  is already an active writer. So the efficiency of a synchronization operation is bounded by the cost of mutex lock/unlock and not by condition
       wait. Note that in the usual case there is no context switch.

       This is not to say that the efficiency of condition waiting is unimportant. Since there needs to be at least one context switch  per  Ada  rendezvous,  the
       efficiency  of  waiting on a condition variable is important. The cost of waiting on a condition variable should be little more than the minimal cost for a
       context switch plus the time to unlock and lock the mutex.

   Features of Mutexes and Condition Variables
       It had been suggested that the mutex acquisition and release be decoupled from condition wait. This was rejected because it is the combined nature  of  the
       operation that, in fact, facilitates realtime implementations. Those implementations can atomically move a high-priority thread between the condition vari‐
       able and the mutex in a manner that is transparent to the caller. This can prevent extra context switches and provide more deterministic acquisition  of  a
       mutex  when  the  waiting thread is signaled. Thus, fairness and priority issues can be dealt with directly by the scheduling discipline.  Furthermore, the
       current condition wait operation matches existing practice.

   Scheduling Behavior of Mutexes and Condition Variables
       Synchronization primitives that attempt to interfere with scheduling policy by specifying an ordering rule are considered undesirable.  Threads waiting  on
       mutexes and condition variables are selected to proceed in an order dependent upon the scheduling policy rather than in some fixed order (for example, FIFO
       or priority). Thus, the scheduling policy determines which thread(s) are awakened and allowed to proceed.

   Timed Condition Wait
       The pthread_cond_timedwait() function allows an application to give up waiting for a particular condition after a given amount of time. An example  of  its
       use follows:

           (void) pthread_mutex_lock(&t.mn);
               t.waiters++;
               clock_gettime(CLOCK_REALTIME, &ts);
               ts.tv_sec += 5;
               rc = 0;
               while (! mypredicate(&t) && rc == 0)
                   rc = pthread_cond_timedwait(&t.cond, &t.mn, &ts);
               t.waiters--;
               if (rc == 0 || mypredicate(&t))
                   setmystate(&t);
           (void) pthread_mutex_unlock(&t.mn);

       By  making  the timeout parameter absolute, it does not need to be recomputed each time the program checks its blocking predicate. If the timeout was rela‐
       tive, it would have to be recomputed before each call.  This would be especially difficult since such code would need to take into account the  possibility
       of  extra  wakeups  that result from extra broadcasts or signals on the condition variable that occur before either the predicate is true or the timeout is
       due.

FUTURE DIRECTIONS
       None.

SEE ALSO
       pthread_cond_broadcast()

       The Base Definitions volume of POSIX.12008, Section 4.11, Memory Synchronization, <pthread.h>

COPYRIGHT
       Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology  --  Portable
       Operating  System  Interface  (POSIX),  The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engi‐
       neers, Inc and The Open Group.  (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this  version
       and  the  original  IEEE  and  The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be
       obtained online at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source  files  to  man
       page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .

IEEE/The Open Group                                                            2013                                                 PTHREAD_COND_TIMEDWAIT(3POSIX)
 Manual page pthread_cond_wait(3posix) line 195/236 (END) (press h for help or q to quit)

pthread_cond_wait:它首先将当前线程加入到唤醒队列,然后旋即解锁mutex,最后等待被唤醒。被唤醒后,又对mutex加锁

参考文章:pthread_cond_wait 为什么需要传递 mutex 参数? - 丁凯的回答 - 知乎

pthread_cond_wait 其实源码中包含了三个步骤,先解锁,然后wait,然后再加锁

参考文章:pthread_cond_wait 为什么需要传递 mutex 参数? - etnlGD的回答 - 知乎

pthread_mutex_unlock(mtx);
pthread_cond_just_wait(cv);
pthread_mutex_lock(mtx);

我猜上面这三部大概就是所谓的“原子操作”,我不太懂,只能大概这么理解。。。

20220728 我的小测试案例(其中一个线程每隔一段时间用条件变量通知另一个线程,让它给全局变量iCount加一)需睡眠等待

test_cond.c

//编译运行指令:
//gcc test_cond.c -lpthread && ./a.out

//brief:
//其中一个线程每隔一段时间用条件变量通知另一个线程,让它给全局变量iCount加一

#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"
#include <string.h>
#include <sys/time.h>

#define printf(format, ...)                                                             \
    do {                                                                                \
          /*2.generate a time string and connect it to the log string tail.*/           \
            char str[1024] = {0};                                                       \
            struct timeval tv;                                                          \
            struct tm* t;                                                               \
            gettimeofday(&tv, NULL);                                                    \
            t = localtime(&tv.tv_sec);                                                  \
            sprintf(str,"[%04d-%02d-%02d %02d:%02d:%02d.%03ld] ",                       \
                            1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday,               \
                            t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);       \
            printf("%s#%d "format ,str ,__LINE__ , ##__VA_ARGS__);                      \
       } while (0)

static int iCount = 0;

void *thread1(void *arg);
void *thread2(void *arg);

pthread_mutex_t mutex;
pthread_cond_t cond;


int main()
{
    pthread_t tid1,tid2;
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_create(&tid1,NULL,thread1,NULL);
    pthread_create(&tid2,NULL,thread2,NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
}


//thread 1:
void *thread1(void *arg)
{
    while(1)
    {
        printf("thread 1 尝试获得锁\n");        
        pthread_mutex_lock(&mutex);
        printf("\n");
        printf("thread 1 获得锁, icount = %d\n", iCount);
        pthread_cond_wait(&cond,&mutex);
        printf("thread 1 cond 信号 收到\n");
        sleep(2);   //任务处理时间
        iCount++;
        pthread_mutex_unlock(&mutex);
        printf("thread 1 释放锁, icount = %d\n", iCount);
        usleep(1000);   //这里必须要给与一定的“处理”时间,否则其他线程就没太大机会获得锁,下同
    }
}

//thread 2:
void *thread2(void *arg)
{
    while(1)
    {
        printf("thread 2 触发 cond\n");
        pthread_cond_signal(&cond);
        sleep(3);   //每隔x秒触发一次cond
    }
}

ubuntu编译运行结果:

[root@ubuntu /arnold_test/20220721_test_pthread]175# gcc test_mutex.c -lpthread && ./a.out 
[2022-07-28 13:52:37.325] #54 thread 1 尝试获得锁
[2022-07-28 13:52:37.325] #56 
[2022-07-28 13:52:37.325] #57 thread 1 获得锁, icount = 0
[2022-07-28 13:52:37.325] #73 thread 2 触发 cond
[2022-07-28 13:52:37.326] #59 thread 1 cond 信号 收到
[2022-07-28 13:52:39.326] #63 thread 1 释放锁, icount = 1
[2022-07-28 13:52:39.328] #54 thread 1 尝试获得锁
[2022-07-28 13:52:39.328] #56 
[2022-07-28 13:52:39.328] #57 thread 1 获得锁, icount = 1
[2022-07-28 13:52:40.326] #73 thread 2 触发 cond
[2022-07-28 13:52:40.326] #59 thread 1 cond 信号 收到
[2022-07-28 13:52:42.327] #63 thread 1 释放锁, icount = 2
[2022-07-28 13:52:42.329] #54 thread 1 尝试获得锁
[2022-07-28 13:52:42.329] #56 
[2022-07-28 13:52:42.329] #57 thread 1 获得锁, icount = 2
[2022-07-28 13:52:43.327] #73 thread 2 触发 cond
[2022-07-28 13:52:43.327] #59 thread 1 cond 信号 收到
[2022-07-28 13:52:45.328] #63 thread 1 释放锁, icount = 3
[2022-07-28 13:52:45.330] #54 thread 1 尝试获得锁
[2022-07-28 13:52:45.330] #56 
[2022-07-28 13:52:45.330] #57 thread 1 获得锁, icount = 3
[2022-07-28 13:52:46.328] #73 thread 2 触发 cond
[2022-07-28 13:52:46.328] #59 thread 1 cond 信号 收到
^C
[root@ubuntu /arnold_test/20220721_test_pthread]176# 

20220730 我的另一个demo,无需睡眠,一个线程可以不断发送信号,另一个线程接收信号(不存在信号丢失),缺陷是thread2的while循环占用了不少资源(我在想有没有能不用while循环的方法,但貌似没找到,主要是那个pthread_cond_wait必须要执行并阻塞线程后,才能接收来自另一个线程的cond触发信号,否则会造成信号丢失,但是另一个线程不知道你啥时候准备好了啊,还是得用一个while循环不断去判断🤣)

test_cond.c

//编译运行指令:
//gcc test_cond.c -lpthread && ./a.out

//brief:
//其中一个线程每隔一段时间用条件变量通知另一个线程,让它给全局变量iCount加一

#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"
#include <string.h>
#include <sys/time.h>

#define printf(format, ...)                                                             \
    do {                                                                                \
          /*2.generate a time string and connect it to the log string tail.*/           \
            char str[1024] = {0};                                                       \
            struct timeval tv;                                                          \
            struct tm* t;                                                               \
            gettimeofday(&tv, NULL);                                                    \
            t = localtime(&tv.tv_sec);                                                  \
            sprintf(str,"[%04d-%02d-%02d %02d:%02d:%02d.%03ld] ",                       \
                            1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday,               \
                            t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);       \
            printf("%s#%d "format ,str ,__LINE__ , ##__VA_ARGS__);                      \
       } while (0)

static int iCount = 0;
static int wait_flag = 0;
static int send_flag = 0;
static int thread2_judge_time = 0;

void *thread1(void *arg);
void *thread2(void *arg);

pthread_mutex_t mutex;
pthread_cond_t cond;


int main()
{
    pthread_t tid1,tid2;
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_create(&tid1,NULL,thread1,NULL);
    pthread_create(&tid2,NULL,thread2,NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
}


//thread 1:
void *thread1(void *arg)
{
    while(1)
    {
        printf("\n");
        printf("thread 1 尝试获得锁\n");        
        pthread_mutex_lock(&mutex);
        printf("thread 1 获得锁, icount = %d\n", iCount);
        wait_flag = 1;
        printf("thread 1 wait_flag has been set to 1, icount = %d\n", iCount);
        printf("thread 1 cond_wait 解锁并开始 wait, icount = %d\n", iCount);
        pthread_cond_wait(&cond, &mutex);    //解锁、wait、加锁
        printf("thread 1 cond 信号 收到, 加锁\n");
        //sleep(2);   //任务处理时间
        iCount++;
        wait_flag = 0;
        send_flag = 0;  //置零
        printf("thread 1 尝试释放锁, icount = %d\n", iCount);
        pthread_mutex_unlock(&mutex);
        printf("thread 1 释放锁, icount = %d, thread2_judge_time = %d\n", iCount, thread2_judge_time);
        //usleep(1000);   //这里必须要给与一定的“处理”时间,否则其他线程就没太大机会获得锁,下同
    }
}

//thread 2:
void *thread2(void *arg)
{   
    //sleep(1);
    int i = 0;
    while(1)
    {
        if(send_flag == 1)
        {
            thread2_judge_time++;   //就是感觉判断次数太多了,占用了大量的资源
            continue;
        }

        printf("thread 2 尝试获得锁\n");        
        pthread_mutex_lock(&mutex);
        printf("thread 2 获得锁, icount = %d\n", iCount);

        if(wait_flag == 1)  //必须要用flag,不然线程1没在wait也发个信号过去,多尴尬!
        { 
            printf("thread 2 尝试释放锁, icount = %d\n", iCount);
            pthread_mutex_unlock(&mutex);
            printf("thread 2 释放锁, icount = %d\n", iCount);
            send_flag = 1;
            pthread_cond_signal(&cond); //要在线程2没有锁的情况下触发,这样触发后线程1能立即相应并拿到锁,否则线程1不能立即拿到锁

            printf("thread 2 触发 cond\n");
            printf("thread 2 i = %d\n", ++i);
        }
        else
        {
            printf("thread 2 尝试释放锁, icount = %d\n", iCount);
            pthread_mutex_unlock(&mutex);
            printf("thread 2 释放锁, icount = %d\n", iCount);
        }
        if(i == 9999)break;
    }
}

ubuntu编译运行命令:

gcc test_cond.c -lpthread && ./a.out

在这里插入图片描述

另外还有不懂,参见

pthread_cond_wait 详解

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值