【问题标题】:pthread locking not workingpthread锁定不起作用
【发布时间】:2013-08-24 17:13:00
【问题描述】:

我有一个小程序,它在一台服务器上运行正常,线程显示准确数据,但在其他服务器上显示重复。

我有以下代码:

// Structure
struct words_list {
    char myword[20];
    struct words_list * next;
};
struct myrepl_list {
    char myrepl[20];
    struct myrepl_list * next;
};

struct arg_struct {
    char *word;
    char *repl;
    int t;
};


int max_thread = 1;
// Mutex variables
pthread_mutex_t repl_list;
pthread_mutex_t thrd_list;

struct words_list * first_word = NULL;
struct myrepl_list * first_repl = NULL;

/*
do_process()
*/
void* do_process(void *arguments)
{
    int *res = malloc(sizeof(int));
    struct arg_struct *args = arguments;
    char *word, *repl;
    int t;
    pthread_mutex_lock(&thrd_list);
    word = args->word;
    repl = args->repl;
    t = args->t;
    pthread_mutex_unlock(&thrd_list);
    fprintf(stderr,"(%d) WORD: %s REPL: %s\n",t,word,repl);

//test example of return
    if (strstr(word, repl))
        *res = 1;
    else
        *res = 0;
//
    return res;
}


int main ()
{
    int ex = 0, i = 0;
    char myword[20];
    char myrepl[20];
    struct words_list * curr_word = first_word;
    struct myrepl_list * curr_repl = first_repl;
    struct arg_struct args;
    pthread_t thread_id[MAX_THREADS];

    while(ex == 0)
    {
        int ret = -1;
        for(i = 0 ; i < max_thread; i++)
        {
            // Get current word and myrepl
            pthread_mutex_lock(&repl_list);
            strncpy(myword,curr_word->myword,sizeof(myword) - 1);
            strncpy(myrepl,curr_repl->myrepl,sizeof(myrepl) - 1);
            pthread_mutex_unlock(&repl_list);
            args.myword = myword;
            args.myrepl = myrepl;
            args.t = i;

            //start threads
            if(pthread_create(&thread_id[i],NULL,&do_process,&args) != 0)
            {
                i--;
                fprintf(stderr,RED "\nError in creating thread\n" NONE);
            }
            else
            {
                pthread_mutex_lock(&repl_list);
                if(curr_repl->next == NULL)
                {
                    if(curr_word->next != NULL)
                    {
                        curr_word = curr_word->next;
                        curr_repl = first_repl;
                    }
                    else
                    {
                        ex = 1;
                        break;
                    }
                }
                else
                    curr_repl = curr_repl->next;
                pthread_mutex_unlock(&repl_list);
            }
        }

        for(i = 0 ; i < max_thread; i++)
        {
            void *join_result;
            if(pthread_join(thread_id[i],&join_result) != 0)
                fprintf(stderr,RED "\nError in joining thread\n" NONE);
            else
            {
                ret = *(int *)join_result;
                free(join_result);
                if(ret == 1)
                {
                    ex = 1;
                    break;
                }
                else
                {
                    //code missing
                }
            }
        }
    }//end while
}

在一台服务器上显示此输出:

(0) WORD: test1 REPL: bla0
(1) WORD: test1 REPL: bla1
(2) WORD: test1 REPL: bla2
(0) WORD: test1 REPL: bla3
(1) WORD: test1 REPL: bla4
(2) WORD: test1 REPL: bla5
(0) WORD: test1 REPL: bla6
(1) WORD: test1 REPL: bla7
(2) WORD: test1 REPL: bla8
(0) WORD: test1 REPL: bla9
(1) WORD: test1 REPL: bla10
(2) WORD: test2 REPL: bla0
(0) WORD: test2 REPL: bla1
(1) WORD: test2 REPL: bla2
(2) WORD: test2 REPL: bla3
(0) WORD: test2 REPL: bla4
(1) WORD: test2 REPL: bla5
(2) WORD: test2 REPL: bla6

在另一台服务器上显示:

(2) WORD: test1 REPL: bla2
(2) WORD: test1 REPL: bla2
(2) WORD: test1 REPL: bla2
(1) WORD: test1 REPL: bla1
(2) WORD: test1 REPL: bla4
(2) WORD: test1 REPL: bla4
(1) WORD: test1 REPL: bla6
(2) WORD: test1 REPL: bla7
(2) WORD: test1 REPL: bla7
(1) WORD: test1 REPL: bla9
(2) WORD: test1 REPL: bla10
(2) WORD: test2 REPL: bla10
(1) WORD: test2 REPL: bla1
(2) WORD: test2 REPL: bla2
(2) WORD: test2 REPL: bla2
(1) WORD: test2 REPL: bla4
(2) WORD: test2 REPL: bla3
(2) WORD: test2 REPL: bla3

我最后也得到了这个,可能是因为线程循环内部的中断:

Error in joining thread

Error in joining thread

Error in joining thread

Error in joining thread

我在这里做错了什么?

为什么在一台服务器上正确显示线程号和其余信息,但在另一台服务器上显示混乱的数据?

我尝试了一整天来解决它,但没有成功。

【问题讨论】:

  • 代码太多,请简化。
  • 已经简化了,不相关的部分已经去掉
  • 源代码无法编译。
  • 我删除了一些部分,可能在删除过程中我省略了一些东西,但它编译成功
  • 您不必发布所有代码,而是发布一个真实子集,该子集本身会复制您的特定问题而不会引入潜在问题减少的副作用是SSCCE 的目标。

标签: c struct pthreads


【解决方案1】:

在这一行

strncpy(myword,curr_word->myword,sizeof(myword) - 1);

程序取消引用NULL,因为curr_wordNULL。这会引发不法行为,因此在此之后任何事情都可能发生。


还要注意strncpy() 不一定将0-终止附加到目标字符数组。 detailsman strncpy

【讨论】:

  • 我什至用 while(curr_word != NULL) 替换了 while(ex == 0)
  • 我会尝试对 strncpy 部分进行一些修改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2015-02-06
相关资源
最近更新 更多