Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 关闭动态创建的线程_C_Linux_Multithreading - Fatal编程技术网

C 关闭动态创建的线程

C 关闭动态创建的线程,c,linux,multithreading,C,Linux,Multithreading,这是一个操作系统编程任务。我试图读取n个文件,使用线程搜索每个文件中出现的特定字符的数量 ./mycount j new.txt some.txt here.txt hello.txt 我的测试代码的输出应为: argumentCount: 6 threadCount: 4 pthread_create() for thread 0 returns: 0 Thread 1 pthread_create() for thread 1 returns: 0 Thread 2 pthread_cre

这是一个操作系统编程任务。我试图读取n个文件,使用线程搜索每个文件中出现的特定字符的数量

./mycount j new.txt some.txt here.txt hello.txt
我的测试代码的输出应为:

argumentCount: 6
threadCount: 4
pthread_create() for thread 0 returns: 0
Thread 1
pthread_create() for thread 1 returns: 0
Thread 2
pthread_create() for thread 2 returns: 0
Thread 3
pthread_create() for thread 3 returns: 0
Thread 4
但是,mycount的每次执行都是不同的,最后一个线程通常不执行/打印。要么这样,要么他们会断断续续地打印,等等

我试图利用互斥来确保数据的完整性,但我不确定幕后到底发生了什么

我如何确保每次都以相同的方式完成?最后一个线程总是返回0,但有时它不会完全执行我给它的函数

代码:

//全局
int=0;
//原型
void*scanFile(void*filePtr);
//初始化互斥的空间。
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
//从.exe调用接收参数
void main(int argumentCount,char*argumentVariables[])
{
//如果argumentCount>50,则退出。
如果(参数计数>50)
{
perror(“参数太多。输入少于50。\n”);
退出(退出失败);
}
printf(“argumentCount:%d\n”,argumentCount);
//实例化变量。
//i-迭代器
//*newCommand-用于保存输入的第一个命令的字符串值。
//*newVector-用于保存其余命令的字符串。是一个向量。
int i;
字符*搜索字符;
char*newVector[argumentCount];
//遍历命令行参数并拆分它们。
对于(i=0;i
多亏了用户2864740和肯·托马斯找到了解决方案

增加:

for (int j = 0; j < threads; j++)
{
    //Join the threads so all the data is good to go.
    pthread_join(thread[j], NULL);
}
for(int j=0;j
更正:

for (int i = 0; i < threads; i++)
{
    request[i].file = argumentVariables[i + 2];
    request[i].character = searchCharacter;

    //Create the thread. Any output from the integer newThread is an error code.
    newThread = pthread_create(&thread[i], NULL, *scanFile, &request[i]);
    if (newThread != 0)
    {
        printf("Error - pthread_create() return code: %d\n", newThread);
        exit(EXIT_FAILURE);
    }
}

for (int j = 0; j < threads; j++)
{
    //Join the threads so all the data is good to go.
    pthread_join(thread[j], NULL);
}
for(int i=0;i
连接线程的代码在哪里?为什么这对于确保“所有线程开始并完成工作”很重要?我不知道如何加入n个线程。另外,确保所有线程都执行对我来说也很重要,因为将来我需要每个线程扫描一个文件,以查找特定字符出现的次数。请参阅和。@AustinHeath,加入n个线程只是一次加入一个线程。发生的情况是,您的进程在所有线程完成之前退出,因为您没有采取任何措施来防止这种情况。一旦启动它们,就终止进程(及其所有线程)。完成了!谢谢发布解决方案@当然,这解决了问题,但消除了任何并发性。也就是说,您不再从使用多线程中获得任何好处。您正在一次创建一个,并等待它完成,然后再启动下一个。在加入任何一个之前,你都应该先创建它们。@Kenthomass发现了这一点!现在一切正常。
for (int i = 0; i < threads; i++)
{
    request[i].file = argumentVariables[i + 2];
    request[i].character = searchCharacter;

    //Create the thread. Any output from the integer newThread is an error code.
    newThread = pthread_create(&thread[i], NULL, *scanFile, &request[i]);
    if (newThread != 0)
    {
        printf("Error - pthread_create() return code: %d\n", newThread);
        exit(EXIT_FAILURE);
    }
}

for (int j = 0; j < threads; j++)
{
    //Join the threads so all the data is good to go.
    pthread_join(thread[j], NULL);
}