pthread锁定不起作用

时间:2013-08-24 17:13:01

标签: c struct pthreads

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

我有以下代码:

// 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

我在这里做错了什么?

为什么在一台服务器上正确显示了主题号和其他信息,但另一方显示了混乱的数据?

我整天都试着解决它,但没有成功。

1 个答案:

答案 0 :(得分:1)

在这一行

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

该计划取消引用NULLcurr_wordNULL。这会引发不端行为,所以在此之后就会发生任何事情。


另请注意,strncpy()不一定会将0 - 终止附加到目标字符数组。有关details的信息,请参见man strncpy

相关问题