C 线程函数中的字符串参数
我在使用pthread_create将字符串传递给线程函数时遇到问题 当我运行程序时,我会有奇怪的性格 这是密码C 线程函数中的字符串参数,c,multithreading,C,Multithreading,我在使用pthread_create将字符串传递给线程函数时遇到问题 当我运行程序时,我会有奇怪的性格 这是密码 #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <time.h> #include &
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/stat.h>
#define NTHREADS 3
void *myFun(void *ptr){
char * string;
string = (char *) ptr;
printf("string: %s\n", string);
return NULL;
}
int main(int argc, char *argv[]){
pthread_t threads[NTHREADS];
char* thread_args[NTHREADS];
int i;
char* string;
/* spawn threads */
for (i=0; i<NTHREADS; ++i){
string = "file1.txt";
thread_args[i] = string;
if(pthread_create(&threads[i], NULL, myFun, (void *) &thread_args[i]) != 0){
printf("Error creating thread\n");
exit(1);
}
}
/* Wait for threads to finish */
for (i=0; i<NTHREADS; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义第3行
void*myFun(void*ptr){
字符*字符串;
字符串=(char*)ptr;
printf(“字符串:%s\n”,字符串);
返回NULL;
}
int main(int argc,char*argv[]){
pthread_t threads[n线程];
字符*线程参数[n读取];
int i;
字符*字符串;
/*繁殖线程*/
对于(i=0;i传递指针,而不是指针的地址)
if(pthread_create(&threads[i], NULL, myFun, (void *) thread_args[i])
^
“不工作”是什么意思?调试器在说什么?在thread\u args
变量中没有意义-尤其是不是数组。