pthread mutex not working correctly

前端 未结 2 861
夕颜
夕颜 2020-12-06 21:33

I am currently learning C from MIT\'s Open Courseware course called Practical Programming in C. In discussing race conditions in multithreading, the lecture notes contained

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 22:13

    You didn't initialize the mutex. You can either do this:

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    

    or in the main function, do this:

    if ( pthread_mutex_init( &mutex, NULL) != 0 )
        printf( "mutex init failed\n" );
    

提交回复
热议问题