while循环中线程内部的另一个线程

Thread inside another thread in a while loop

本文关键字:线程 另一个 内部 while 循环      更新时间:2023-10-16

我是c++的新手,我现在正在尝试线程。我试图在while循环下的线程内创建一个线程。但我认为这似乎不起作用。现在我的代码看起来像这样:

#include <>
std::vector<pthread_t> outer_thread, inner_thread;
    void *inner_thread(void *ptr)
    {
      string data1;
      data1 = *(reinterpret_cast<string*>(ptr));
      cout << "inner thread started " << data1;
/* do something */
      cout << "inner thread stopped " << data1;
pthread_exit(NULL);
  return 0;

    }
    void *outer_thread(void *ptr )
    {
cout << "out thread started" << endl;
//cout << ptr << endl;
//cout << *(reinterpret_cast<string*>(ptr)) << endl;
string data;
data = *(reinterpret_cast<string*>(ptr));

      string str3;
while (getline(data,str3))
{
      cout << "out thread started" << endl;

pthread_t in_thread;
in_vec.push_back(str3);
                int create_thread2 = pthread_create(&in_thread, NULL, &inner_thread, reinterpret_cast<void*>(&(in_vec.at(j))));
                inner_thread.push_back(in_thread);

      if (create_thread2 != 0) 
        cout << "Error : Thread";

      j++;
      cout << "out thread ends " << j << create_thread2 << endl ;
    }
         for (int k = 0; k < j ; k++)
{
pthread_join(inner_thread.at(k),NULL) ;
}
pthread_exit(NULL);
  return 0;
}   
    int main (int argc, char *argv[])
    {
      int i = 0;
      while (getline(gin,str))
      {
 string str1;
                pthread_t out_thread;
                cout << "str1" << str1 << endl;
now_vec.push_back(str1);
int create_thread = pthread_create(&out_thread, NULL, &outer_thread, reinterpret_cast<void*>(&(now_vec.at(i))));
                outer_thread.push_back(out_thread);
                if (create_thread != 0) cout << "Error : Thread" ;
        i++;
      }
for (int k = 0 ; k < i; k ++)
{
cout << i << endl;
//cout << "third thread " << outer_thread.at(1) << endl;
cout << outer_thread.at(k) << endl;
cout << "out out out" << endl;
pthread_join(outer_thread.at(k),NULL) ;
}

    }

我正在尝试读取一个包含应该读取的文件列表的文件。我想同时读取所有这些文件。所有这些文件都包含信息,并且需要另一组线程来启动另一个操作。所以这也需要同时进行。这就是我有两组线程运行的原因。如果有更快更简单的方法来做这件事,请告诉我。

它似乎等到内部线程完成,然后开始下一次迭代。我希望内部线程在外部线程内同时运行。我能知道怎么做吗?

你对线程操作的看法是错误的。一个线程不能在另一个线程内操作。它们是同一进程中独立的执行流,它们的共存是扁平的,而不是分层的。

处理多线程时要遵循的一些简单规则:

    创建线程是很昂贵的,所以要避免快速创建和销毁线程。最好在应用程序开始时创建线程,并在工作可用时为它们分配工作。
  • 在进行计算工作时,避免创建超过CPU可同时执行的线程数。任何额外的线程都会导致过多的上下文切换,从而降低应用程序的速度。
  • 尽量避免使用共享资源,如果一个数据结构必须在线程之间共享,尝试找到一个无锁的实现。如果您的共享资源在无锁实现中不可用,那么使用锁来保护它,但是要非常小心,不正确地使用锁可能导致应用程序死锁,或者应用程序的性能下降到串行执行的情况(好像只有一个线程)。

在您的特殊情况下,如果您希望通过并行处理多个文件来加快处理速度(并假设这些线程需要完成的唯一任务就是处理这些文件),那么可能的解决方案如下:

  1. 读取要在
  2. 上操作的文件列表
  3. 将列表划分为几个部分(每个部分对应CPU上的每个逻辑处理器)。
  4. 创建工作线程(每个逻辑处理器一个),传递文件列表的部分(不要尝试在创建它的同一个循环中加入线程,这将阻塞直到线程完成执行,导致应用程序串行执行而不是并行执行,这是您提供的示例代码中的情况)

工作线程可以遍历它们的文件列表,每次读取一个文件并处理它们。

与您建议的解决方案相反,这个解决方案不会为每个文件创建一个线程。相反,它会在CPU上创建尽可能多的并行线程,避免过多的上下文切换。

上面的一个基本例子:

#include <pthread.h>
#include <vector>
#include <string>
#define NUM_THREADS 4
std::vector<std::string> work_pool[NUM_THREADS];
void *worker_thread(void *args);
int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    // Read list of files here, distribute them evenly amongst the work_pools
    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_create(&threads[i], NULL, worker_thread, (void *)i);
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }
    return 0;
}
void *worker_thread(void *args)
{
    const int id = (int)args;
    std::vector<std::string>::iterator it;
    for (it = work_pool[id].begin(); it != work_pool[id].end(); it++) {
        // Read file and process it here
    }
    return NULL;
}

不确定您在尝试做什么,但在许多语法错误中,我希望来自简化您的代码,以下是发生的情况:

  1. 主线程生成一个线程(1)并等待它完成(join)
  2. (1)线程执行outer_thread并生成另一个线程(2)并等待它完成(join)
  3. (2)线程执行inner_thread和finish.
  4. (2)得到连接,(1)线程能够完成。
  5. (1)被连接,主线程可以进入下一个迭代。
  6. 进程重新启动

请注意,您没有任何并行执行,因为您的线程正在等待其他完成。

注意,在任务中抛出线程并不是提高速度的方法。

线程是:

  • 更好地利用你的CPU资源(当你有多个CPU资源…并且只使用尽可能多的CPU资源)
  • 通过将请求封装为线程来简化代码的组织(但这种技巧的规模非常糟糕)