将 char* 传递给 pthread 崩溃
Passing char* to pthread crashing
我正在尝试获得一个基于时间的线程池系统。我设法使用 fork() 实现了这一点,现在我正在尝试使用线程来实现它。计时器线程似乎工作正常,但由于某种原因我无法将 char* 数组传递给线程(转储核心)。
注意:如果我尝试退出状态为 0 的线程,我不会收到有关将整数赋给函数 returning void* 的警告。但是,当我尝试 return 其他内容时,将其设置为 1,我会收到警告。我试图将它们转换为 void*,但没有任何影响。
现在,对于一些代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define COUNTING_SUCCESS 0
#define POOLING_SUCCESS 1
int size = 3;
void *count(void *bound_arg){
int index = 0;
int *bound = (int*)bound_arg;
printf("Counting started...\n");
while(index < *bound){
printf("You have %d seconds left\n",*bound - index);
sleep(1);
index++;
}
printf("Time's up!\n");
pthread_exit(COUNTING_SUCCESS);
}
void *pool(void *questions_ptr){
char *questions = (char*)questions_ptr;
char *answer = calloc(sizeof(char*)*size,size);
for(int i =0 ; i < size ; i++){
printf("%s : ",questions[i]);
scanf("%s",&answer);
}
pthread_exit(0);
}
int main(){
char* questions[] = {"Q1","Q2","Q3"};
int limit = 3 ;
int *countingLimit = &limit;
void *countingStatus;
void *poolingStatus;
pthread_t timerThread;
int threadID = pthread_create(&timerThread,NULL,count,(void*)countingLimit);
pthread_join(timerThread,&countingStatus);
printf("%d\n",(int*)countingStatus);
pthread_t poolingThread;
int poolingThreadID = pthread_create(&poolingThread,NULL,pool,(void*)questions);
pthread_join(poolingThread,&poolingStatus);
printf("%d\n",(int*)poolingStatus);
}
示例输出:
Counting started...
You have 3 seconds left
You have 2 seconds left
You have 1 seconds left
Time's up!
0
Segmentation fault (core dumped) //this is where i try to pass the char*
无法进入功能
P.S。我正在使用以下方法构建可执行文件:
gcc -o pool pool.c -pthread
这与线程无关。您正在将单个 char
传递给 printf ,它在哪里需要 char *
(对于字符串),因此它崩溃了:
printf("%s : ",questions[i]);
您已将 quesstions
声明为 char *
(单个字符串),因此您正在获取单个 char
,然后 printf 会将其视为虚假指针并崩溃。
您可能打算将 questions
声明为 char **
,因为这就是您作为 pthread 参数传递的内容:
char **questions = (char**)questions_ptr;
我正在尝试获得一个基于时间的线程池系统。我设法使用 fork() 实现了这一点,现在我正在尝试使用线程来实现它。计时器线程似乎工作正常,但由于某种原因我无法将 char* 数组传递给线程(转储核心)。
注意:如果我尝试退出状态为 0 的线程,我不会收到有关将整数赋给函数 returning void* 的警告。但是,当我尝试 return 其他内容时,将其设置为 1,我会收到警告。我试图将它们转换为 void*,但没有任何影响。
现在,对于一些代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define COUNTING_SUCCESS 0
#define POOLING_SUCCESS 1
int size = 3;
void *count(void *bound_arg){
int index = 0;
int *bound = (int*)bound_arg;
printf("Counting started...\n");
while(index < *bound){
printf("You have %d seconds left\n",*bound - index);
sleep(1);
index++;
}
printf("Time's up!\n");
pthread_exit(COUNTING_SUCCESS);
}
void *pool(void *questions_ptr){
char *questions = (char*)questions_ptr;
char *answer = calloc(sizeof(char*)*size,size);
for(int i =0 ; i < size ; i++){
printf("%s : ",questions[i]);
scanf("%s",&answer);
}
pthread_exit(0);
}
int main(){
char* questions[] = {"Q1","Q2","Q3"};
int limit = 3 ;
int *countingLimit = &limit;
void *countingStatus;
void *poolingStatus;
pthread_t timerThread;
int threadID = pthread_create(&timerThread,NULL,count,(void*)countingLimit);
pthread_join(timerThread,&countingStatus);
printf("%d\n",(int*)countingStatus);
pthread_t poolingThread;
int poolingThreadID = pthread_create(&poolingThread,NULL,pool,(void*)questions);
pthread_join(poolingThread,&poolingStatus);
printf("%d\n",(int*)poolingStatus);
}
示例输出:
Counting started...
You have 3 seconds left
You have 2 seconds left
You have 1 seconds left
Time's up!
0
Segmentation fault (core dumped) //this is where i try to pass the char*
无法进入功能
P.S。我正在使用以下方法构建可执行文件:
gcc -o pool pool.c -pthread
这与线程无关。您正在将单个 char
传递给 printf ,它在哪里需要 char *
(对于字符串),因此它崩溃了:
printf("%s : ",questions[i]);
您已将 quesstions
声明为 char *
(单个字符串),因此您正在获取单个 char
,然后 printf 会将其视为虚假指针并崩溃。
您可能打算将 questions
声明为 char **
,因为这就是您作为 pthread 参数传递的内容:
char **questions = (char**)questions_ptr;