线程是否可能在C中创建但不调用目标函数? [英] Is it possible that a thread gets created in C but does not invoke the target function?
问题描述
大家好,
我正在使用C中的线程,我编写了一个代码来创建执行函数的线程。问题是我正在创建线程,但是,目标函数没有被调用。我怎么知道的?我到处都有打印声明。我获得了成功的次数,因为我想要线程,但没有任何目标函数打印语句被打印,我觉得这是奇怪的。
以下是我的代码
<<线程创建>>
Hi all,
I am working with threads in C, I have written a code to create threads which execute a function. Problem is I am getting the threads created, however, the target function is not getting invoked. How do I know it? I have print statements everywhere. I get success as many times as I want the threads but none of the target function print statements are getting printed, which I feel is bizarre.
Below is my code
<<creation of threads>>
void replace(char **args)
{
char *word1=args[1];
char *word2=args[2];
int worker=atoi(args[3]);
int err;
// printf("no of workers %d\n",worker);
int size=0;
size_t segment;
int i=0;
int j;
//int count=0;
char *w_ptr;
for(i=0;i<worker;i++)
{
int begin = i / (float)worker * filestat.st_size;
int end = ( i + 1 )/ (float)worker * filestat.st_size;
//printf("Searching from %d to %d\n", begin, end );
thread_data_replace_array[i].begin=begin;
thread_data_replace_array[i].end=end;
thread_data_replace_array[i].word1=word1;
thread_data_replace_array[i].word2=word2;
//struct thread_data_replace *my_data;
struct thread_data_replace *my_data=malloc(sizeof(struct thread_data_replace));
err=pthread_create(&(tid[i]),NULL,s_replace,(void *) &thread_data_replace_array[i]);
if (err!=0) printf("\n cant create a thread");
else printf("\n success");
}
}
<<>>
<<目标函数>>
<<>>
<<Target function>>
void *s_replace(void *threadarg)
{
// int count=0;
pthread_mutex_lock(&relock);
printf("\ninside s_replace for a new thread");
// struct thread_data_replace *my_data;
// my_data=malloc(sizeof(struct(thread_data_replace)));
struct thread_data_replace *my_data=(struct thread_data_replace *)threadarg;
int begin=my_data->begin;
int end= my_data->end;
char *word1= my_data->word1;
char *word2= my_data->word2;
int j;
int i;
printf("\ninside replace word to be replaced is %s",word1);
printf("\n inside replace replaced word is %s",word2);
printf("\n from %d to %d in replace",begin, end);
for(j=begin;j<end;j++)>
{
//printf("\n inside for not inside if replacing \n");
if(!memcmp(data+j,word1,strlen(word1)))
{
printf("inside if this time for replace function replacing words");
for(i=j;i<strlen(word1);i++)>
{
data[i]=word2[i];
}
}
}
pthread_mutex_unlock(&relock);
}
<<>>
我尝试过:
我在创建线程而不是在目标函数中尝试将内存分配给参数结构。我到处都有锁,所以我不会有不稳定的行为。如果它有帮助,在ssh的帮助下在unix机器上工作。
当我输入6个线程作为输入时我得到的输出是
成功
成功
成功
成功
成功
成功
然后程序结束
<<>>
What I have tried:
I have tried allocating the memory to the struct of arguments while creating the threads instead of in the target function. I have applied locks everywhere so I don't get erratic behavior. If it helps, m working on a unix machine with the help of an ssh.
the output I get when I give 6 threads as input is
success
success
success
success
success
success
and then the program ends
推荐答案
你没有显示你的调用replace()
的主函数。因为你的程序终止,你可能不会等待线程终止(完成他们的工作)。换句话说:主线程到达它的结束并在工作线程有机会被执行之前终止。
为了避免这种情况,请调用 pthread_join [ main()末尾的所有线程,target =_ blanktitle =New Window> ^ ] 函数。这将暂停执行主线程,直到已连接的工作线程终止。
You did not show your main function which callsreplace()
. Because your programs terminates, you are probably not waiting for the threads to have terminated (done their job). In other words: The main thread reaches it's end and terminates before the worker threads had a chance to be executed.
To avoid this, call pthread_join[^] for all threads at the end of yourmain()
function. This will suspend execution of the main thread until the joined worker threads had terminated.
这篇关于线程是否可能在C中创建但不调用目标函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!