多线程代码无法理解的结果
Incomprehensible result of a multithread code
我将开始一个将使用多线程的 C 编程项目。在开始项目之前,我已经写了一个练习代码。我的目的是了解互斥锁和线程是如何工作的。但是它不能正常工作。这是代码:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
pthread_mutex_t myMutex;
char myStrings[100][30];
int i=0;
void *PrintThread1()
{
printf("this is initial of FIRST Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is FIRST thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
void *PrintThread2()
{
printf("this is initial of SECOND Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is SECOND thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
void *PrintThread3()
{
printf("this is initial of THIRD Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is THIRD thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t firstThread, secondThread, thirdThread;
//pthread_attr_t attr;
pthread_mutex_init(&myMutex, NULL);
//pthread_attr_init(&attr);
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
int ft;
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
ft = pthread_create(&secondThread, NULL, PrintThread2(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
ft = pthread_create(&thirdThread, NULL, PrintThread3(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
pthread_join(firstThread, NULL);
pthread_join(secondThread, NULL);
pthread_join(thirdThread, NULL);
pthread_mutex_destroy(&myMutex);
for (int j=0;j<100; j++) {
printf("String[%d] = %s\n",j,myStrings[j]);
}
printf("\n");
pthread_exit(NULL);
return -1;
}
当我执行这段代码时,我的结果是:
this is initial of FIRST Thread
Program ended with exit code: 0
我想不出错误。
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
应该阅读
ft = pthread_create(&firstThread, NULL, PrintThread1, NULL);
你的所有 pthread_create
电话也是如此。
要使用 pthread_create
,您需要将线程启动例程的地址传递给它,您在 C 中通过编写函数名称 而没有 来完成函数调用括号。
如您所写,您调用预期的线程启动例程,在主线程上,并传递任何它returns 到 pthread_create
成为线程启动例程。但它从来没有 returns,因为它调用了 pthread_exit
,这(因为 pthread_create
还没有被调用,所以只有一个线程)终止了整个程序。
不幸的是,你必须在编译器发现这个错误之前 way 发出警告,即使那样也不是很清楚问题是什么:
$ gcc -std=c99 -Wall -pedantic -pthread test.c
test.c: In function ‘main’:
test.c:60:45: warning: ISO C forbids passing argument 3
of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic]
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
^
没有-pedantic
,无怨无悔
我将开始一个将使用多线程的 C 编程项目。在开始项目之前,我已经写了一个练习代码。我的目的是了解互斥锁和线程是如何工作的。但是它不能正常工作。这是代码:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
pthread_mutex_t myMutex;
char myStrings[100][30];
int i=0;
void *PrintThread1()
{
printf("this is initial of FIRST Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is FIRST thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
void *PrintThread2()
{
printf("this is initial of SECOND Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is SECOND thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
void *PrintThread3()
{
printf("this is initial of THIRD Thread\n");
for (int j=0; j<33; j++) {
pthread_mutex_lock(&myMutex);
strcpy(myStrings[i], "This is THIRD thread");
i++;
pthread_mutex_unlock(&myMutex);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t firstThread, secondThread, thirdThread;
//pthread_attr_t attr;
pthread_mutex_init(&myMutex, NULL);
//pthread_attr_init(&attr);
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
int ft;
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
ft = pthread_create(&secondThread, NULL, PrintThread2(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
ft = pthread_create(&thirdThread, NULL, PrintThread3(), NULL);
if (ft){
printf("ERROR; return code from pthread_create() is %d\n", ft);
exit(-1);
}
pthread_join(firstThread, NULL);
pthread_join(secondThread, NULL);
pthread_join(thirdThread, NULL);
pthread_mutex_destroy(&myMutex);
for (int j=0;j<100; j++) {
printf("String[%d] = %s\n",j,myStrings[j]);
}
printf("\n");
pthread_exit(NULL);
return -1;
}
当我执行这段代码时,我的结果是:
this is initial of FIRST Thread
Program ended with exit code: 0
我想不出错误。
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
应该阅读
ft = pthread_create(&firstThread, NULL, PrintThread1, NULL);
你的所有 pthread_create
电话也是如此。
要使用 pthread_create
,您需要将线程启动例程的地址传递给它,您在 C 中通过编写函数名称 而没有 来完成函数调用括号。
如您所写,您调用预期的线程启动例程,在主线程上,并传递任何它returns 到 pthread_create
成为线程启动例程。但它从来没有 returns,因为它调用了 pthread_exit
,这(因为 pthread_create
还没有被调用,所以只有一个线程)终止了整个程序。
不幸的是,你必须在编译器发现这个错误之前 way 发出警告,即使那样也不是很清楚问题是什么:
$ gcc -std=c99 -Wall -pedantic -pthread test.c
test.c: In function ‘main’:
test.c:60:45: warning: ISO C forbids passing argument 3
of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic]
ft = pthread_create(&firstThread, NULL, PrintThread1(), NULL);
^
没有-pedantic
,无怨无悔