C++ 完成工作后如何停止所有pthread?

C++ 完成工作后如何停止所有pthread?,c++,parallel-processing,pthreads,brute-force,C++,Parallel Processing,Pthreads,Brute Force,我试图创建一个代码来强制执行一个随机字符串,但是在一个线程上运行它会花费很长时间(正如预期的那样)。我正在摆弄pthreads,这就是我想到的: void* bruteForce ( void* ARGS ) { args *arg = ( args * ) ARGS; string STRING= arg->STRING; string charSet = arg->charSet; string guess = arg->guess;

我试图创建一个代码来强制执行一个随机字符串,但是在一个线程上运行它会花费很长时间(正如预期的那样)。我正在摆弄pthreads,这就是我想到的:

void*
bruteForce ( void* ARGS )
{
    args *arg = ( args * ) ARGS;
    string STRING= arg->STRING;
    string charSet = arg->charSet;
    string guess = arg->guess;

    char c;
    int size;
    int pos;
    int lenght;
    int j = 0;
    char CHAR[STRING.length ( )];
    size = charSet.length ( );

    do
    {
        for ( j = 0; j < STRING.length ( ); j++ )
        {
            pos = rand ( ) % size;
            CHAR[j] = charSet[pos];
            guess = string ( CHAR );
            //cout << guess[j];
        }
        //cout << guess << endl;
    }
    while ( STRING!= guess );

}

int
main ( int argc, char** argv )
{

    srand ( ( unsigned ) ( time ( NULL ) ) );

    const int NUMBER_OF_THREADS = 10;

    args arg;
    ifstream myFile;
    string STRING;
    string charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    string guess;

    pthread_t threads[NUMBER_OF_THREADS];
    void* status;

    arg.charSet = charSet;
    arg.STRING= STRING;

    char c;
    int size;
    int pos;
    int lenght;
    int j = 0;

    myFile.open ( "string.txt" );

    getline ( myFile, STRING);
    size = charSet.length ( );


    int rc;

    //Creating threads for cracking the string
    for ( int i = 0; i < NUMBER_OF_THREADS; i++ )
    {
        rc = pthread_create ( &threads[i], NULL, bruteForce, ( void* ) &arg );
        if ( rc )
        {
            cout << "Couldnt create thread";
            exit ( 1 );
        }
    }


    //Joining threads
    for ( int i = 0; i < NUMBER_OF_THREADS; i++ )
    {
        rc = pthread_join ( threads[i], &status );
        if ( rc )
        {
            cout << "thread number " << i << " was unable to join: " << rc << endl;
            exit ( 1 );
        }
    }
}
void*
暴力力量(无效*ARGS)
{
args*arg=(args*)args;
字符串字符串=arg->string;
字符串字符集=arg->charSet;
字符串猜测=arg->guess;
字符c;
整数大小;
int pos;
内部长度;
int j=0;
char char[STRING.length()];
size=charSet.length();
做
{
对于(j=0;j//cout只要您不想在找到答案后再运行程序,就可以从找到答案的线程调用

do
{
    // ...
}
while ( STRING!= guess );
std::cout << guess << std::endl;
std::exit(0);
do
{
// ...
}
while(字符串!=猜测);

std::cout笨拙但在您的情况下可行:

在全局作用域中添加完成标志。当任何线程找到结果时设置它。 使每个线程的循环依赖于该标志

 bool DONE=false; // set to true to stop other threads

 void*bruteForce ( void* ARGS )
 {   ...
     do
     {  <try a string>
     }
     while ( !DONE && STRING!= guess );
     DONE=true; // set redundantly but that doesn't hurt
 }
bool DONE=false;//设置为true以停止其他线程
void*bruteForce(void*ARGS)
{   ...
做
{  
}
而(!DONE&&STRING!=guess);
DONE=true;//冗余设置,但这不会造成伤害
}

您的主程序仍然可以执行联接以收集完成的pthread,然后继续对猜测的答案执行它可能想要执行的任何工作。

一旦打印出结果,您可以调用
exit()
@AntonSavin,但我在哪里调用exit())函数?来自找到答案的线程。很抱歉问了这么多。exit()函数终止线程还是整个程序?