Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I'm xth thread, how to understand that?

Posted on 2008-10-19
17
Medium Priority
?
258 Views
Last Modified: 2013-12-14
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!
0
Comment
Question by:CSecurity
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755198
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
 
LVL 17

Author Comment

by:CSecurity
ID: 22755240
How can I access X from CallFunction1 or 2 or 3?
0
 
LVL 17

Author Comment

by:CSecurity
ID: 22755244
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755288
>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
 
LVL 17

Author Comment

by:CSecurity
ID: 22755307
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755437
>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
 
LVL 17

Author Comment

by:CSecurity
ID: 22755450
1) Every thread creates new hook

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

3) Yes, but how?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755640
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22755828
>> 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
 
LVL 17

Author Comment

by:CSecurity
ID: 22755886
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755935
>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
 
LVL 86

Expert Comment

by:jkr
ID: 22757675
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
 
LVL 17

Author Comment

by:CSecurity
ID: 22768261
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
 
LVL 86

Expert Comment

by:jkr
ID: 22768369
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
 
LVL 17

Author Comment

by:CSecurity
ID: 22770158
But when I created thread how I can get threadID of created thread?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 22770323
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
 
LVL 17

Author Comment

by:CSecurity
ID: 22770793
Thanks, it works now!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question