gpt4 book ai didi

c - 实现线程数组

转载 作者:行者123 更新时间:2023-11-30 15:16:12 27 4
gpt4 key购买 nike

下面是一个c程序,我想在其中实现线程数组。有两个线程函数。我想在每个函数内发送一个 int 值。但代码没有给出任何输出。示例程序:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>


void * threadFunc1(void * arg)
{

int id = *((int *) arg);
printf("Inside threadfunc2 for thread %d",id)
}

void * threadFunc2(void * arg)
{
int i= *((int *)arg);
printf("Inside threadfunc2 for thread %d",i)

}

int main(void)
{

pthread_t thread[10];

for(int i=0;i<10;i++)
{

pthread_create(&thread[i],NULL,threadFunc1,(void*)&i ); // want to send the value of i inside each thread

pthread_create(&thread[i],NULL,threadFunc,(void*)&i );
}


while(1);
return 0;
}

代码有问题吗?

最佳答案

只需在线程函数内的 printf 中的字符串中添加“\n”终止符即可。这将迫使 flushing the output buffer .

您粘贴的代码中还存在一些语法错误,但您可能会很容易地找出这些错误。你可以使用 pthread_join()而不是 while (1); ...

关于c - 实现线程数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33183877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com