简体   繁体   English

在While循环中初始化Pthread

[英]Initializing Pthreads inside While loop

im starting to use pthreads cpp library to resolve some homeworks. 即时通讯开始使用pthreads cpp库来解决一些作业。

In one of those, i do have to create a thread for each file named from 1 to T (T is fixed with Define as a positive integer), and this thread must process some informations of this file. 在其中之一中,我确实必须为每个从1到T命名的文件创建一个线程(T固定为Define为正整数),并且该线程必须处理该文件的某些信息。

Actually, my thinking is to put a while loop in Main, like this: 实际上,我的想法是在Main中放置一个while循环,如下所示:

pthread_t threads[T];

void *imprime(void *arg) {

    int a=-1, b=-1;
    string* t = reinterpret_cast<std::string*>(arg); //Recovering str
    string name = *t;
    cout<<"\nName: "<<name<<endl;
    ifstream inFile(nome.c_str());
    while(inFile>>a) {
            inFile>>b;
            cout<<"a: "<<a<<"\nb: "<<b<<endl;
    }
}

int main() {

    int lim = 1;
    string nome;
    int a = 0, b = 0;

    while(lim <= T) {
        nome = to_string(lim);
        cout<<"Opening: "<<lim<<endl;
        pthread_create(&threads[lim], NULL, &imprime, (void *)&nome);

        lim++;
    }
    cin.get();

    return 0;
}

At first, the thread didnt run, nothing happened. 最初,线程没有运行,什么也没发生。 So, i put the "cin.get()" under the while and it suddently works (What i didnt understood yet). 因此,我把“ cin.get()”放在了下面,然后突然起作用了(我还不了解)。

But now, if T is 1 it works properly, but if T > 1, it doesnt work like it supposed to work. 但是现在,如果T为1,它将正常工作,但是如果T> 1,它将无法正常工作。

I put two files(names 1, 2 and 3; with ints 'a' and 'b' separated by an space): 我放置了两个文件(名称1、2和3;整数“ a”和“ b”用空格分隔):

/* 
   File '1' = "1 2"
   File '2' = "3 4"
   File '3' = "5 6"
*/

and thats the output: 那就是输出:

Opening: 1
Opening: 2
Opening: 3

Nome: 3
a: 5
b: 6

Nome: 3
a: 5
b: 6

Nome: 3
a: 5
b: 6

By some reason, the program runs the entire while T times before starting the threads, and it overwrite every thread with the last one. 由于某种原因,程序在启动线程之前先运行整个T遍,然后用最后一个线程覆盖每个线程。

What can i do? 我能做什么?

Passing a pointer to nome is a bad idea as you can't be sure the individual thread copies the value before you change it again in main . 传递一个指向nome的指针是一个坏主意,因为在main再次更改它之前,您不能确保单个线程复制了该值。

Make an array for the argument to the threads: 为线程的参数创建一个数组:

string threads_args[T];

and then do 然后做

    threads_args[lim] = to_string(lim);
    cout<<"Opening: "<<lim<<endl;
    pthread_create(&threads[lim], NULL, &imprime, (void *)&threads_args[lim]);

Further notice: 另行通知:

1) You should join the threads in main instead of using cin.get 1)您应该在main中join线程,而不是使用cin.get

2) while(lim <= T) shall be while(lim < T) . 2) while(lim <= T)应该是while(lim < T) Currently you access outside array boundary. 当前,您访问数组边界之外。 Also you probably want lim to start from 0 instead of 1 and then have threads_args[lim] = to_string(lim+1); 同样,您可能希望lim0而不是1 ,然后使用threads_args[lim] = to_string(lim+1);

3) C++11 has std::thread which seems a better choice than pthread 3)C ++ 11拥有std :: thread似乎比pthread更好的选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM