Click to See Complete Forum and Search --> : Random thread numbers


redspider
October 7th, 2005, 12:39 AM
Need help with random thread numbers.
I have a function that makes threads , the number of threads depends on user input.

So i need to have the right amount of HANDLE's for the threads, but the amount needs to be a const int eg.

const int threads = 5;
DWORD dwThreadId[threads];
HANDLE hThread[threads];

So is it ok to use

int thread_s = (user input from sonsole);
const int threads = sizeof(thread_s) / sizeof(int);

DWORD dwThreadId[threads];
HANDLE hThread[threads];

masg
October 7th, 2005, 01:48 AM
Let me parapharse it,
if a user wants 5 threads to be created, he would enter 5 at the console.
So, you need to update that vaule in a constant variable "threads".

If that's the requirement, then sizeof() will return the number of bytes that a paritcular variable takes.
In that case, "threads" will always be updated with a value 1.
Since thread_s is a int, sizeof(thread_s) and sizeof(int) would be same.
so, just assign the thread_s to threads.

Kheun
October 7th, 2005, 02:30 AM
Since you are allowing the user to specify the number of thread, you can't use const int as the number is no longer constant. In addition, sizeof operator can only report the size of static object. You may like to use std::vector as it allows its size to vary during runtime.


#include <vector>
#include <iostream>
using namespace std;
int main()
{
int id, handle;
int threadCount;
cin >> threadCount;

vector<HANDLE> hThreads(threadCount);
vector<DWORD> threadIDs(threadCount);

for(int i = 0; i < threadCount; ++i)
{
// ... get handle and id




hThreads[i] = handle;
threadIDs[i] = id;
}

}