I'm xth thread, how to understand that?

Hi

I'm going to develop a network computer up-time checker, first it scans network and finds all computer and I store them in char array with an index

Like:

char Computers[1024][255];

It have IP address of Computers, I want to create thread per computer, but in thread, IN ENTIRE THREAD, I want to have a number which shows it's Xth thread, thread will be like this:

VOID WINAPI MyThread()
{
CallFunction1();
CallFunction2();
CallFunction3();
CallFunction4();
}

I want to have my thread number in ALL functions that thread calls.

Is it possible? Please advice.

Thanks from now!
LVL 17
CSecurityAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Pass it as an argument at the time of creation of thread

HANDLE WINAPI CreateThread(
  __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in       SIZE_T dwStackSize,
  __in       LPTHREAD_START_ROUTINE lpStartAddress,
  __in_opt   LPVOID lpParameter,    <-----  Pass X here
  __in       DWORD dwCreationFlags,
  __out_opt  LPDWORD lpThreadId
);
0
CSecurityAuthor Commented:
How can I access X from CallFunction1 or 2 or 3?
0
CSecurityAuthor Commented:
I know I can pass X as argument to that functions also, but that's not going to work

Because Function 1 2 3 is a callback function (hook)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

sunnycoderCommented:
>Because Function 1 2 3 is a callback function (hook)
Who calls them? The threads?

VOID WINAPI MyThread()
{
CallFunction1();
CallFunction2();
CallFunction3();
CallFunction4();
}

You can pass X as argument to these functions.
0
CSecurityAuthor Commented:
No I have a something like

Hook(myfunction);

Then any time new packet arrives myfunction gets fired, that's per thread, each thread uses same function but in that function I want to do different things per thread
0
sunnycoderCommented:
>Then any time new packet arrives myfunction gets fired, that's per thread,
Every thread calls this hook function?

>each thread uses same function but in that function I want to do different things per thread
So packet arrives and this hook function gets called from each thread and you wish to do something different in the function depending on which thread's context it is executing in?

Am I correct?
Would it be possible for you have a static data structure that holds the mapping of thread handles and IDs?
0
CSecurityAuthor Commented:
1) Every thread creates new hook

2) Yes, each thread receives it's own packets

3) Yes, but how?
0
sunnycoderCommented:
Initialize the static data structure in main ... whenever a new thread is created append to that data structure, whenever a thread exits, cleanup its entry from the data structure.
Within the callback function a thread can lookup its own handle in the data structure and find its ID.
0
Infinity08Commented:
>> I want to create thread per computer

Are you sure you need that many threads ? That sounds like it will slow down the whole lot quite a bit.
0
CSecurityAuthor Commented:
In callback function I have a comparison like this:

if (memcmp(packeth->shost, IpTarget, 6) == 0)

IpTarget is IP which I want each thread work for...

But writing it and making it to check for each packet with an entire array, will make it more slower, isn't it?
0
sunnycoderCommented:
>But writing it and making it to check for each packet with an entire array,
For IP lookup there are plenty of efficient data structures ... google for patricia trie
0
jkrCommented:
Thread Local Storage (TLS - see http://msdn.microsoft.com/en-us/library/ms686749.aspx) might be what you're looking for. You can find some more complex examples at http://www.codeproject.com/KB/threads/tlsByDougDoedens.aspx ("Use Thread Local Storage To Pass Thread Specific Data") and http://www.codeproject.com/KB/threads/tls.aspx ("Thread Local Storage - The C++ Way"). The scoop would be the following (taken from http://msdn.microsoft.com/en-us/library/ms686991(VS.85).aspx):
#include <windows.h> 
#include <stdio.h> 
 
#define THREADCOUNT 4 
DWORD dwTlsIndex; 
 
VOID ErrorExit(LPSTR); 
 
VOID CommonFunc(VOID) 
{ 
   LPVOID lpvData; 
 
// Retrieve a data pointer for the current thread. 
 
   lpvData = TlsGetValue(dwTlsIndex); 
   if ((lpvData == 0) && (GetLastError() != ERROR_SUCCESS)) 
      ErrorExit("TlsGetValue error"); 
 
// Use the data stored for the current thread. 
 
   printf("common: thread %d: lpvData=%lx\n", 
      GetCurrentThreadId(), lpvData); 
 
   Sleep(5000); 
} 
 
DWORD WINAPI ThreadFunc(VOID) 
{ 
   LPVOID lpvData; 
 
// Initialize the TLS index for this thread. 
 
   lpvData = (LPVOID) LocalAlloc(LPTR, 256); 
   if (! TlsSetValue(dwTlsIndex, lpvData)) 
      ErrorExit("TlsSetValue error"); 
 
   printf("thread %d: lpvData=%lx\n", GetCurrentThreadId(), lpvData); 
 
   CommonFunc(); 
 
// Release the dynamic memory before the thread returns. 
 
   lpvData = TlsGetValue(dwTlsIndex); 
   if (lpvData != 0) 
      LocalFree((HLOCAL) lpvData); 
 
   return 0; 
} 
 
int main(VOID) 
{ 
   DWORD IDThread; 
   HANDLE hThread[THREADCOUNT]; 
   int i; 
 
// Allocate a TLS index. 
 
   if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) 
      ErrorExit("TlsAlloc failed"); 
 
// Create multiple threads. 
 
   for (i = 0; i < THREADCOUNT; i++) 
   { 
      hThread[i] = CreateThread(NULL, // default security attributes 
         0,                           // use default stack size 
         (LPTHREAD_START_ROUTINE) ThreadFunc, // thread function 
         NULL,                    // no thread function argument 
         0,                       // use default creation flags 
         &IDThread);              // returns thread identifier 
 
   // Check the return value for success. 
      if (hThread[i] == NULL) 
         ErrorExit("CreateThread error\n"); 
   } 
 
   for (i = 0; i < THREADCOUNT; i++) 
      WaitForSingleObject(hThread[i], INFINITE); 
 
   TlsFree(dwTlsIndex);
 
   return 0; 
} 
 
VOID ErrorExit (LPSTR lpszMessage) 
{ 
   fprintf(stderr, "%s\n", lpszMessage); 
   ExitProcess(0); 
}

Open in new window

0
CSecurityAuthor Commented:
Thanks a lot jkr, I'm going to use TLS, just 2 questions:

1) I'm to set Thread Count to 256, does it makes problem?

2) TlsSetValue(dwTlsIndex, lpvData)
lpvData could be unsigned char, right?
0
jkrCommented:
Neither should be a problem. But, as a side note - you can also use 'GetCurrentThreadId()' (http://msdn.microsoft.com/en-us/library/ms683183.aspx) to uniquely identify a thread in any function running in that thread's context.
0
CSecurityAuthor Commented:
But when I created thread how I can get threadID of created thread?
0
jkrCommented:
When you call 'CresteThread()', the last parameter will give ysou the ID, e.g.
DWORD dwThreadId;
 
HANDLE hThread = CreateThread(NULL,0,MyThreadProc,pParam,0,%dwThreadId);
// now 'dwThreadId' has the thread's ID.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CSecurityAuthor Commented:
Thanks, it works now!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.