【发布时间】:2020-09-13 03:57:33
【问题描述】:
我有一个关于 C 中的线程、锁和条件变量的练习。我需要编写一个程序来获取数据,将其转换为链表,启动 3 个线程,每个线程计算列表中每个节点和主线程的结果在 evreyone 完成后打印结果。
这是主要功能:
int thread_finished_count;
// Lock and Conditional variable
pthread_mutex_t list_lock;
pthread_mutex_t thread_lock;
pthread_cond_t thread_cv;
int main(int argc, char const *argv[])
{
node *list;
int pairs_count, status;
thread_finished_count = 0;
/* get the data and start the threads */
node *head = create_numbers(argc, argv, &pairs_count);
list = head; // backup head for results
pthread_t *threads = start_threads(&list);
/* wait for threads and destroy lock */
status = pthread_cond_wait(&thread_cv, &list_lock);
chcek_status(status);
status = pthread_mutex_destroy(&list_lock);
chcek_status(status);
status = pthread_mutex_destroy(&thread_lock);
chcek_status(status);
/* print result in original list */
print_results(head);
/* cleanup */
wait_for_threads(threads, NUM_THREADS);
free_list(head);
free(threads);
return EXIT_SUCCESS;
}
请注意,create_numbers 函数工作正常,列表按预期工作。
这里是start_thread和thread_function的代码:
pthread_t *start_threads(node **list)
{
int status;
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * NUM_THREADS);
check_malloc(threads);
for (int i = 0; i < NUM_THREADS; i++)
{
status = pthread_create(&threads[i], NULL, thread_function, list);
chcek_status(status);
}
return threads;
}
void *thread_function(node **list)
{
int status, self_id = pthread_self();
printf("im in %u\n", self_id);
node *currentNode;
while (1)
{
if (!(*list))
break;
status = pthread_mutex_lock(&list_lock);
chcek_status(status);
printf("list location %p thread %u\n", *list, self_id);
if (!(*list))
{
status = pthread_mutex_unlock(&list_lock);
chcek_status(status);
break;
}
currentNode = (*list);
(*list) = (*list)->next;
status = pthread_mutex_unlock(&list_lock);
chcek_status(status);
currentNode->gcd = gcd(currentNode->num1, currentNode->num2);
status = usleep(10);
chcek_status(status);
}
status = pthread_mutex_lock(&thread_lock);
chcek_status(status);
thread_finished_count++;
status = pthread_mutex_unlock(&thread_lock);
chcek_status(status);
if (thread_finished_count != 3)
return NULL;
status = pthread_cond_signal(&thread_cv);
chcek_status(status);
return NULL;
}
void chcek_status(int status)
{
if (status != 0)
{
fputs("pthread_function() error\n", stderr);
exit(EXIT_FAILURE);
}
}
请注意,self_id 用于调试目的。
我的问题
- 我的主要问题是关于拆分工作。每个线程从全局链表中获取一个元素,计算gcd,然后继续获取下一个元素。仅当我在 while 循环中解锁互斥锁后添加 usleep(10) 时,我才会获得此效果。如果我不添加usleep,FIRST线程将进入并完成所有工作,而其他线程等待并在所有工作完成后进入。
请注意!:我考虑过可能创建第一个线程的选项,直到创建第二个线程,第一个线程已经完成所有工作。这就是为什么我在创建 evrey 线程时使用 usleep(10) 添加“我在 #threadID”检查的原因。他们都进来了,但只有第一个在做所有的工作。 如果我在互斥锁解锁后睡着了,这是输出示例(注意不同的线程 ID)
睡眠时
./v2 nums.txt
im in 1333593856
list location 0x7fffc4fb56a0 thread 1333593856
im in 1316685568
im in 1325139712
list location 0x7fffc4fb56c0 thread 1333593856
list location 0x7fffc4fb56e0 thread 1316685568
list location 0x7fffc4fb5700 thread 1325139712
list location 0x7fffc4fb5720 thread 1333593856
list location 0x7fffc4fb5740 thread 1316685568
list location 0x7fffc4fb5760 thread 1325139712
list location 0x7fffc4fb5780 thread 1333593856
list location 0x7fffc4fb57a0 thread 1316685568
list location 0x7fffc4fb57c0 thread 1325139712
list location 0x7fffc4fb57e0 thread 1333593856
list location 0x7fffc4fb5800 thread 1316685568
list location (nil) thread 1325139712
list location (nil) thread 1333593856
...
normal result output
...
如果我在互斥锁后注释掉usleep,这就是输出(注意相同的线程ID) 不睡觉
./v2 nums.txt
im in 2631730944
list location 0x7fffe5b946a0 thread 2631730944
list location 0x7fffe5b946c0 thread 2631730944
list location 0x7fffe5b946e0 thread 2631730944
list location 0x7fffe5b94700 thread 2631730944
list location 0x7fffe5b94720 thread 2631730944
list location 0x7fffe5b94740 thread 2631730944
list location 0x7fffe5b94760 thread 2631730944
list location 0x7fffe5b94780 thread 2631730944
list location 0x7fffe5b947a0 thread 2631730944
list location 0x7fffe5b947c0 thread 2631730944
list location 0x7fffe5b947e0 thread 2631730944
list location 0x7fffe5b94800 thread 2631730944
im in 2623276800
im in 2614822656
...
normal result output
...
- 我的第二个问题是关于线程工作的顺序。我的练习要求我不要使用 join 来同步线程(仅在最后使用“释放资源”),而是使用该条件变量。
我的目标是每个线程将获取元素,进行计算,同时另一个线程将进入并获取另一个元素,新线程将获取每个元素(或至少接近那个)
感谢您的阅读,感谢您的帮助。
【问题讨论】:
-
请创建一个minimal reproducible example。
As a result only the first thread do ALL the work,同步不是命令。可能的解释是,您的系统一直选择第一个要执行的线程,或者第一个线程在其他两个开始之前完成所有工作,等等。But why is that?要回答有关您的特定代码行为的特定问题,您必须展示代码。
标签: c linux pthreads mutex condition-variable