c - 如何修复我的 pthread 代码遇到的此错误?

标签 c macos terminal pthreads mutex

我目前是一名初学者,正在学习如何在 C 中使用 pthread。我的代码当前正在运行,因此没有实际的错误,但它并没有达到预期的效果。该代码应提示用户输入每个问题,然后在用户输入信息后打印所有结果,但一旦输入名称,该代码似乎会跳过并完成。请有人帮我找出我的代码哪里出错了?下面是我的代码和运行时的输出:

#include <stdio.h
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sched.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <string.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

typedef struct compound {
     char name [1024];
     int age;
     int birthMonth;
     int birthYear;
     int threadNumber;
}compound;

   void * threadName (void * param){

    compound *lparam = (compound *) param;

    int lock;

    lock = pthread_mutex_lock(&mutex);

    printf ("> Please type your name for thread %d\n", lparam->threadNumber);
    scanf ("%c", lparam->name);

    lock = pthread_mutex_unlock(&mutex);

    lock = pthread_mutex_lock(&mutex);

    printf ("> Please type your age for thread %d\n", lparam->threadNumber);
    scanf ("%d", &(lparam->age));

    lock = pthread_mutex_unlock(&mutex);

    lock = pthread_mutex_lock(&mutex);

    printf ("> Please type your birth month for thread %d\n", lparam->threadNumber);
    scanf ("%d", &(lparam->birthMonth));

    lock = pthread_mutex_unlock(&mutex);

    lock = pthread_mutex_lock(&mutex);

    printf ("> Please type your birth year for thread %d\n", lparam->threadNumber);
    scanf ("%d", &(lparam->birthYear));

    lock = pthread_mutex_unlock(&mutex);
    return NULL;
}

// main function
int main ( void) {

    pthread_t thread_ID3, thread_ID4;
    void *exitstatus;

    compound first, second;
    first.threadNumber= 1;
    second.threadNumber = 2;

    pthread_create (&thread_ID3, NULL, threadName, &first);
    pthread_create (&thread_ID4, NULL, threadName, &second);

    pthread_join (thread_ID3, &exitstatus);
    pthread_join (thread_ID4, &exitstatus);

    printf("Finished\n");

    getchar();
    return 0;
}

This is what happens when the code is executed:

[370user14@nostromo ex2]$ make
gcc -Wall  -c base_code.c
gcc -lm -lpthread base_code.o  -o baseprog
rm -f *.o *~
[370user14@nostromo ex2]$ ./baseprog 
> Please type your name for thread 1
gurinder
> Please type your age for thread 1
> Please type your birth month for thread 1
> Please type your name for thread 2
> Please type your age for thread 2
> Please type your birth month for thread 2
> Please type your birth year for thread 2
> Please type your birth year for thread 1
Finished

最佳答案

这里是更正后的代码,修复了对 scanf() 的调用,消除了困惑,包括适当的错误检查,公理每行只有一个语句,每个语句(最多)一个变量声明 遵循并纳入其他评论。

#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <pthread.h> // pthread_*()
//#include <semaphore.h>
//#include <sched.h>
//#include <unistd.h>
//#include <signal.h>
//#include <sys/types.h>
//#include <string.h>


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

typedef struct compound
{
     char name [1024];
     int age;
     int birthMonth;
     int birthYear;
     int threadNumber;
} compound;


void * threadName (void * param)
{
    compound *lparam = (compound *) param;

    pthread_mutex_lock(&mutex);

    printf ("> Please type your name for thread %d\n", lparam->threadNumber);
    scanf ("%1023s", lparam->name);

    pthread_mutex_unlock(&mutex);

    pthread_mutex_lock(&mutex);

    printf ("> Please type your age for thread %d\n", lparam->threadNumber);
    scanf ("%d", &(lparam->age));

    pthread_mutex_unlock(&mutex);

    pthread_mutex_lock(&mutex);

    printf ("> Please type the number of your birth month for thread %d (January=1)\n", lparam->threadNumber);
    scanf ("%d", &(lparam->birthMonth));

    pthread_mutex_unlock(&mutex);

    pthread_mutex_lock(&mutex);

    printf ("> Please type your birth year for thread %d\n", lparam->threadNumber);
    scanf ("%d", &(lparam->birthYear));

    pthread_mutex_unlock(&mutex);
    pthread_exit( NULL );
} // end function: threadName


// main function
int main (void)
{

    pthread_t thread_ID3;
    pthread_t thread_ID4;


    compound first;
    compound second;

    first.threadNumber= 1;
    second.threadNumber = 2;

    if( 0 != pthread_create (&thread_ID3, NULL, threadName, (void*)&first) )
    { // then pthread_create failed
        perror( "pthread_create for first thread failed");
        exit( EXIT_FAILURE );
    }

    // implied else pthread_create successful

    if( 0 != pthread_create (&thread_ID4, NULL, threadName, (void*)&second) )
    { // then pthread_create failed
        perror( "pthread_create for second thread failed" );
        exit( EXIT_FAILURE );
    }

    // implied else pthread_create successful

    pthread_join (thread_ID3, NULL);
    pthread_join (thread_ID4, NULL);

    printf("Finished\n");

    int ch;
    while( (ch = getchar()) != EOF && '\n' != ch );
    getchar(); // wait for user to enter a final keystroke
    return 0;
} // end function: main

关于c - 如何修复我的 pthread 代码遇到的此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062538/

相关文章:

c - MQTT 问题 : Broker closing connection as i send connect packet

xcode - 如何为网守共同设计现有的 Mac OS X .app 文件?

git - 从命令行获取 Pull Request 的标题

C: "zsh: abort"错误

c - 如何在 C 中以编程方式实现 'tee'?

objective-c - 如何将我的程序添加到 OS X 系统菜单栏?

c++ - 如何使用 C++ 执行 shell 命令并与 linux 上的输出交互?

C - 省略了参数名称?

c - 如何将整个文件加载到 C 中的字符串中

ios - 如何在 iOS 14+ 应用程序中创建自定义文档图标