Using stringstream to name a variable

I am going to be working on a CUDA gpu based chess engine, and I will be using an unknown number of cpu and gpu threads, possibly hundreds or thousands of threads. The thing i want to do is create objects' names using stringstream and the Boost library.

*Edit: All the gpu threads will be handled by CUDA. But I still need to be able to create cpu threads systematically without writing out each of their names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <boost\thread.hpp>
#include <sstream>
#include <iostream>
using namespace std;

class Worker
{
public:

	Worker(string n)
		:name(n)
	{
		cout << "Initializing: " << name << endl; // Would say "Initializing Worker_1"
	}

	void operator()()
	{
		cout << name << " is doing stuff" << endl; // "Worker_1 is doing stuff"
	}
private:  
	string name; // Used to identify itself.
};

int main()
{
	int X = 0;
	stringstream threadname (stringstream::in | stringstream::out | stringstream::app);
	stringstream workername (stringstream::in | stringstream::out | stringstream::app);
	for (int X = 0; X < 4; ++X)
	{
		threadname.str("");
		workername.str("");

		threadname<< "Thread_";
		workername<< "Worker_";

		threadname << X;
		workername << X;

		Worker Worker_X(workername.str().c_str()); // Creates an object called Worker_1
		boost::thread Thread_X(Worker_X); // Opening a new thread called Thread_1 running Worker_1

		// Shows "Worker_1 starting on Thread_1" for example.
		cout << workername.str().c_str() << " starting on " << threadname.str().c_str() << endl;
	}
	cin >> X; // This is just so the program doesn't exit out right away.
}


A shorter way to put it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class cl{};

// instead of
cl Ex_1;
cl Ex_2;
cl Ex_3;
cl Ex_4;
// or
int N_1;
int N_2;
int N_3;
int N_4;

// I would like to use
for (int i = 0; i < 4; ++i)
{
	cl Ex_i;
	// or
	int N_i;
}

// And then work with those.
for (int i = 0; i < 4; ++i)
{
	Ex_i.Something = Whatever;
	// or
	N_i = IDK;
}
Last edited on
Creating a variable whose name is not known at compile time is not possible. However, I don't see why you couldn't use a container such as vector (if indexes are enough) or a map / hash table to store your variables.
What you want is an array (or vector if dynamically)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class cl{};

// instead of
cl Ex_1;
cl Ex_2;
cl Ex_3;
cl Ex_4;
// or
int N_1;
int N_2;
int N_3;
int N_4;

// I would like to use
for (int i = 0; i < 4; ++i)
{
	cl Ex_i;
	// or
	int N_i;
}

// And then work with those.
for (int i = 0; i < 4; ++i)
{
	Ex_i.Something = Whatever;
	// or
	N_i = IDK;
}


->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class cl{};

// instead of
cl Ex[4];
// or
int N[4];

// I would like to use
for (int i = 0; i < 4; ++i)
{
	Ex[i] = ...;
	// or
	N[i] = ...;
}

// And then work with those.
for (int i = 0; i < 4; ++i)
{
	Ex[i].Something = Whatever;
	// or
	N[i] = IDK;
}

What i was doing was creating object based CPU threads and my focus was the objects, not the threads. I don't know if my program will have 1 main thread and 2 worker threads, or if it will have 6 worker threads, the number of threads will depend on the user preference. I know how to use arrays and vectors, it's the unknown number of threads I am trying to accomplish. When I write programs in CUDA, I can create a kernel with a variable number of threads like:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "device_launch_parameters.h"
#include <cuda_runtime.h>

__global__ void GPUfunction ()
{
    // Task for a GPU thread.
    // Uses its thread ID to know what to do.
}

int main ()
{
    GPUfunction <<<x, y>>> (); // x number of blocks, y number of threads per block.
}

But I can't, or at least don't know how to, systematically create threads without naming them individually in the manner i showed before. I am also using the Boost library to do this if I haven't already mentioned it.
Last edited on
Topic archived. No new replies allowed.