您在此代码中有多个问题,从您的 arg 结构开始。您的所有线程都共享相同的逻辑 arg 结构,其中引入了严重的竞争条件:
struct arg_struct args; // << == Note. Local variable.
int ret = -1;
for(i = 0 ; i < max_thread; i++)
{
pthread_mutex_lock(&word_list);
strncpy(myword,curr_word->myword,sizeof(myword) - 1);
pthread_mutex_unlock(&word_list);
args.myword = myword;
args.t = i;
//start threads NOTE: args passed by address.
if(pthread_create(&thread_id[i],NULL,&do_repl,&args) != 0)
{
i--;
fprintf(stderr,RED "\nError in creating thread\n" NONE);
}
// rest of code...
}
现在想一想当该线程启动时会发生什么。如果您的 for 循环在其中一些有机会访问该 arg-stuct 并提取其特定线程信息之前启动多个线程,那么这些线程将访问您保存在其中的最后一个数据,这将是最后一次迭代循环的(如果你真的不走运,你可能会在更新过程中发现它,但我不会深入研究那个级别的细节;这一点应该很明显)。
我建议您动态分配线程参数结构,并在完成后让线程销毁它。作为一种强烈建议的替代方案(并且通常这样做),将其用作您的返回值pthread_join
,然后main()
在提取线程完成数据后将其销毁。
其次,您的 args 结构正在使用一个指针,该指针为每个线程(局部变量)myword
设置为相同的缓冲区。char myword[20]
因此,即使您将参数结构“修复”为动态的,您的所有线程仍然使用相同的缓冲区。
解决方案
动态分配每个线程参数结构。在其中,拥有正在处理的单词的本地副本。同样,存储 args 将传递给那里的线程的返回代码(省去在线程中分配一个并将其释放的麻烦main()
)。
// thread arguments.
struct arg_struct
{
char myword[20];
int ret;
int t;
};
在您的线程启动循环中:
while(curr_word != NULL)
{
int ret = -1;
for(i = 0 ; i < max_thread; i++)
{
// allocate a new argument struct for the new thread
struct arg_struct *args = calloc(1, sizeof(*args));
args->t = i;
// this lock is pointless, btw.
pthread_mutex_lock(&word_list);
strcpy(args->myword, cur_word->myword); //note: assumes no overrun.
pthread_mutex_unlock(&word_list);
//start threads
if(pthread_create(&thread_id[i],NULL, &do_repl, args) != 0)
{
i--;
fprintf(stderr,RED "\nError in creating thread\n" NONE);
}
else
{
pthread_mutex_lock(&word_list);
if(curr_word->next == NULL)
ex = 1;
else
curr_word = curr_word->next;
pthread_mutex_unlock(&word_list);
}
}//end threads creating
for(i = 0 ; i < max_thread; i++)
{
void *join_result = NULL;
if(pthread_join(thread_id[i], &join_result) != 0)
fprintf(stderr,RED "\nError in joining thread\n" NONE);
else
{
ret = ((struct arg_struct*)join_result)->ret;
free(join_result);
if((ret == 1)
{
ex = 1;
break;
}
}
}//end threads joining
if (ex == 1)
break;
}//end while
在线程过程中,只需执行以下操作:
void* do_repl(void *arguments)
{
struct arg_struct *args = arguments;
fprintf(stderr,"(%d) word: %s\n", args->t, args->word);
args->ret = 0;
return args;
}
对不起,我可能留下的任何错别字,但我希望你明白这一点。
编辑OP 请求了一个简单的线程示例,该示例使用自定义参数块启动线程。以下就是这样做的,并且将实际的链表直接暴露给线程工作人员。线程都共享一个公共指针(按地址,因此指针指向),该指针最初指向列表头,并受互斥锁(线程也共享)保护。所有线程都运行直到它们检测到列表为空,此时它们退出。这意味着您可以加载一个比您的池大得多的列表(我选择了一个 5 个池,一个 20 个列表,但您的列表中可以有比这更多的条目)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
typedef struct node
{
char myword[20];
struct node *next;
} node;
// thread arguments.
typedef struct arg_struct
{
pthread_mutex_t *mtx;
node **pp;
int ret;
int t;
} arg_struct;
// thread procedure. doesn't do much
void* do_repl(void *arguments)
{
arg_struct *args = arguments;
while (1)
{
// lock, get, and unlock
pthread_mutex_lock(args->mtx);
node *p = *args->pp;
if (p)
{
*args->pp = p->next;
pthread_mutex_unlock(args->mtx);
// print the node we just got from the list.
fprintf(stderr,"(%d) word: %s\n", args->t, p->myword);
}
else
{
// no more entries in list. break
break;
}
};
// make sure this is released
pthread_mutex_unlock(args->mtx);
args->ret = 0;
return args;
}
// main entrypoint.
int main()
{
// very simple. we use a fixed number of threads and list nodes.
static const int n_threads = 5;
// build a simple forward-only linked list. will have 4x the
// number of threads in our crew.
node *list = NULL;
node **next = &list;
int i = 0;
for (i=0;i<n_threads*4;++i)
{
node *p = malloc(sizeof(*p));
sprintf(p->myword, "String-%d", i+1);
*next = p;
next = &(p->next);
}
*next = NULL;
// declare a mutex and thread pool for hitting all the elements
// in the linked list.
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_t threads[n_threads];
// lock the mutex before creating the thread pool.
pthread_mutex_lock(&mtx);
i = 0;
node *shared = list;
for (int i=0; i<n_threads; ++i)
{
// setup some thread arguments.
arg_struct *args = calloc(1, sizeof(*args));
args->mtx = &mtx;
args->pp = &shared;
args->t = i+1;
// launch the thread.
pthread_create(threads + i, NULL, do_repl, args);
}
// now unlatch the mutex and wait for the threads to finish.
pthread_mutex_unlock(&mtx);
for (i=0;i<n_threads;++i)
{
void *pv = NULL;
pthread_join(threads[i], &pv);
arg_struct *args = pv;
fprintf(stderr,"Thread %d finished\n", args->t);
free(args);
}
// cleanup the linked list.
while (list != NULL)
{
node *p = list;
list = list->next;
free(p);
}
return 0;
}
输出(因系统和运行实例而异)
(2) word: String-2
(1) word: String-1
(3) word: String-3
(4) word: String-4
(5) word: String-5
(2) word: String-6
(1) word: String-7
(3) word: String-8
(4) word: String-9
(5) word: String-10
(2) word: String-11
(1) word: String-12
(3) word: String-13
(4) word: String-14
(2) word: String-16
(1) word: String-17
(5) word: String-15
(3) word: String-18
(4) word: String-19
(2) word: String-20
Thread 1 finished
Thread 2 finished
Thread 3 finished
Thread 4 finished
Thread 5 finished
请注意报告每个字符串的线程 ID。这证明每个线程都在消耗列表中的多个条目,但每个条目只会消耗一个线程。参数块中的共享指针确保了这一点(以及明显的互斥保护)。