我的应用程序中有 4 个线程。一个是主线程,另外三个是工作线程。我希望这 3 个工作线程中的前 2 个生成数据,第三个在生成数据时将其写入。数据生成器线程应该同步以使其并行运行(同时开始“for”循环的每次迭代)。如果 CPU 足够快,写入器线程应该一直在写入。我不知道如何在 C++ 中专业地同步所有这 3 个线程,所以我编写了代码,就像有 ' __syncthreads()
' 函数来代表我的意思最好的方式。__syncthreads()
传统 C++ 中是否有相当于 CUDA C ' ' 的东西?如果不是,那么如何以我想要的方式最佳地实现同步?(我不喜欢while
代码中的那些循环。
volatile bool write_flag ;
int main(int argc, char **argv)
{
...
write_flag = false ; // nothing to write at the beginning
...
HANDLE *trdHandles = new HANDLE[WORKING_THREADS] ;
int IDs[] = {0, 1} ; // IDs for generator threads
trdHandles[0] = CreateThread(NULL, 0, generator, &IDs[0], 0, NULL) ; // 1st data generator thread
if(trdHandles[0] == NULL)
ExitProcess(0) ;
trdHandles[1] = CreateThread(NULL, 0, generator, &IDs[1], 0, NULL) ; // 2nd data generator thread
if(trdHandles[1] == NULL)
ExitProcess(0) ;
trdHandles[2] = CreateThread(NULL, 0, writer, f_out, 0, NULL) ; // writer thread
if(trdHandles[2] == NULL)
ExitProcess(0) ;
...
}
WINAPI DWORD generator(LPVOID lpParam)
{
int *ID = static_cast<int*>(lpParam) ;
dataGen(*ID) ;
return 0 ;
}
void dataGen(int id)
{
...
for(int aa = 0; aa < cycles; aa++)
{
__syncthreads() ;
... // both threads generate data here in parallel
while(write_flag) // don't generate data too fast. Wait for writes to complete (this flag is initially set to 'false')
;
setBuffers(id, aa) ; // for swapping in/out buffers
if(id == 0) // only one thread needs to set the flag
write_flag = true ;
}
}
WINAPI DWORD writer(LPVOID lpParam)
{
ofstream *f_out = static_cast<ofstream*>(lpParam) ;
while(1)
{
if(write_flag)
{
f_out->write(out_buffer0, chunk_len) ;
f_out->write(out_buffer1, chunk_len) ;
write_flag = false ;
if(!finish)
continue ;
else
return 0 ;
}
}
}