如何用线程c++写写文本行

How write write text line with thread c++

我有一个程序可以创建文本文件并写入一些随机符号。如何让线程写入这些符号?下面是我的代码:

static void MyThreadProc(){}
static const char alphanum[] = "0123456789";//this is random symbols I'm using
int stringLength = sizeof(alphanum) - 1;

char genRandom()   {
return alphanum[rand() % stringLength];
}

int main() {
String^ fileName = "file.txt";

StreamWriter^ sw = gcnew StreamWriter(fileName);

srand(time(0));

Thread^ myThread1 = gcnew Thread(gcnew ThreadStart(MyThreadProc));
Thread^ myThread2 = gcnew Thread(gcnew ThreadStart(MyThreadProc));
//Here I create my threds

for (int z = 0; z < 21; z++){//This cycle writes 21 random simbol

// myThread1->sw->WriteLine(genRandom());   
// myThread1->sw->WriteLine(genRandom()); this threads I want to start writing symbols
    sw->WriteLine(genRandom()); //write random symbols
}
sw->Close();
}

此代码使用线程将符号写入文本文件:

public ref class Simple{
public:
static void Main(){

    String^ fileName = "file.txt";
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    srand(time(0));
    Thread^ myTread1 = gcnew Thread(gcnew ThreadStart(MyThreadProc));
    myTread1->Start();
    for (int z = 0; z < 3; z++){
        sw->WriteLine(genRandom());
    }
    myTread1->Join();
    Thread^ myTread2 = gcnew Thread(gcnew ThreadStart(MyThreadProc));
    myTread2->Start();

    for (int z = 0; z < 3; z++){
        sw->WriteLine(genRandom());
    }
    myTread2->Join();
    sw->Close();
}};