具有 return 值的 pthread 函数的基本用法
basic usage of pthread function with return value
我正在尝试创建一个线程,当从 main.c
调用时,该线程可以读取文件中的行数
main.c
#include <stdio.h>
#define MAX_FILE_NAME 100
#include "t_func.h"
#include <pthread.h>
int main(){
int linecount;
pthread_t thread_id;
FILE *fh = fopen("/home/usr154/out.log", "r");
pthread_create(&thread_id, NULL, count_lines,&fh);
pthread_join(thread_id, (void **) linecount);
printf("lines: %d \n", linecount);
}
t_func.h
#include <stdlib.h>
#include <stdio.h>
int count_lines(int *fh){
char c;
int count =0;
if (fh == NULL)
{
printf("Could not open file %s", fh);
return 0;
}
for (c = getc(fh); c != EOF; c = getc(fh))
if (c == '\n')
count++;
fclose(fh);
return count;
}
我面临 2 个(或更多)问题,文件指针未被接受且 return 值未被处理,非常感谢任何帮助(我是 C 编程的新手)。
- MAX_FILE_NAME 未使用。
- 在同一范围内管理资源。我选择在
main()
中执行,在本例中包括 fopen()
、错误检查和 fclose()
- 更改了
*count_lines()
上的签名以符合 pthread_create()
的期望。
- 将 c 的类型从 char 更改为 int。
- 更改了在命令行上获取文件的行为,以避免必须创建代码期望的文件。
- 我收到关于
return (void *) count;
和 gcc -Wall -Wextra
的警告 warning: cast to pointer from integer of different size
。有没有更好的方法来 return 一个值?除了全局变量,传入 arg 和 out 参数作为值,或者在线程中分配一些东西。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *count_lines(void *arg) {
FILE *fh = (FILE *) arg;
int c;
int count = 0;
while((c = getc(fh)) != EOF) {
if(c == '\n') count++;
}
return (void *) count;
}
int main(int argc, char *argv[]) {
pthread_t thread_id;
if(argc != 2) {
printf("usage: %s path_of_flie\n", argv[0]);
return 1;
}
FILE *fh = fopen(argv[1], "r");
if(!fh) {
printf("Could not open file %s", argv[1]);
return 1;
}
pthread_create(&thread_id, NULL, count_lines, fh);
int linecount;
pthread_join(thread_id, (void **) &linecount);
fclose(fh);
printf("lines: %d \n", linecount);
}
和运行程序本身returns:
lines: 32
我正在尝试创建一个线程,当从 main.c
调用时,该线程可以读取文件中的行数main.c
#include <stdio.h>
#define MAX_FILE_NAME 100
#include "t_func.h"
#include <pthread.h>
int main(){
int linecount;
pthread_t thread_id;
FILE *fh = fopen("/home/usr154/out.log", "r");
pthread_create(&thread_id, NULL, count_lines,&fh);
pthread_join(thread_id, (void **) linecount);
printf("lines: %d \n", linecount);
}
t_func.h
#include <stdlib.h>
#include <stdio.h>
int count_lines(int *fh){
char c;
int count =0;
if (fh == NULL)
{
printf("Could not open file %s", fh);
return 0;
}
for (c = getc(fh); c != EOF; c = getc(fh))
if (c == '\n')
count++;
fclose(fh);
return count;
}
我面临 2 个(或更多)问题,文件指针未被接受且 return 值未被处理,非常感谢任何帮助(我是 C 编程的新手)。
- MAX_FILE_NAME 未使用。
- 在同一范围内管理资源。我选择在
main()
中执行,在本例中包括fopen()
、错误检查和fclose()
- 更改了
*count_lines()
上的签名以符合pthread_create()
的期望。 - 将 c 的类型从 char 更改为 int。
- 更改了在命令行上获取文件的行为,以避免必须创建代码期望的文件。
- 我收到关于
return (void *) count;
和gcc -Wall -Wextra
的警告warning: cast to pointer from integer of different size
。有没有更好的方法来 return 一个值?除了全局变量,传入 arg 和 out 参数作为值,或者在线程中分配一些东西。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *count_lines(void *arg) {
FILE *fh = (FILE *) arg;
int c;
int count = 0;
while((c = getc(fh)) != EOF) {
if(c == '\n') count++;
}
return (void *) count;
}
int main(int argc, char *argv[]) {
pthread_t thread_id;
if(argc != 2) {
printf("usage: %s path_of_flie\n", argv[0]);
return 1;
}
FILE *fh = fopen(argv[1], "r");
if(!fh) {
printf("Could not open file %s", argv[1]);
return 1;
}
pthread_create(&thread_id, NULL, count_lines, fh);
int linecount;
pthread_join(thread_id, (void **) &linecount);
fclose(fh);
printf("lines: %d \n", linecount);
}
和运行程序本身returns:
lines: 32