#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Help on threaing ...


    I am new in programing and trying to learn threading, when I am geting the thread Id for the thread It gives me new thread Id every time..Why so? and when I get the thread Id below the _beginthread it gives same thread Id for both the thread..
    can anyone explain it please....
    Thanks in advance ....
    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    
    
    void Thread(void *param);
    void Thread1(void *param);
    
    HANDLE handle;
    
    int cnt = 0;
    int main()
    {
    	int n;
    	int i;
    	int val = 0;
    
    	printf("Enter the no of threads \n");
    	scanf("%d",&n);
    	for(i = 1;i<=n;i++)
    	{
    		val = i;
    
    		handle = (HANDLE) _beginthread(Thread,0,&val);
    		WaitForSingleObject(handle, INFINITE);
    		Sleep(1000);
    
    		handle = (HANDLE) _beginthread(Thread1, 0 ,&val);
    		WaitForSingleObject(handle,INFINITE);
    		
    		Sleep(1000);
    
    		
    	}
    
    	return 0;
    }
    
    void Thread(void *param)
    {
    	int h = *((int *)param);
    
    	printf("First Thread:  %d \t", h);
    	cnt++;	
    	
    	int th = GetCurrentThreadId();
    
    	printf("thread1Id %d\t",th);
    	printf("thread1count %d\n",cnt);
    	
    	_endthread();
    }
    
    void Thread1(void *param)
    {
    	int h = *((int *)param);
    
    	printf("Second Thread : %d \t",h);
    	cnt++;
    
    	int th1 = GetCurrentThreadId();
    	printf("thread2Id %d\t",th1);
    	printf("thread2count %d\n",cnt);	
    	
    	_endthread();
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    It seems to me that you get the same threadID each time, because your main() waits for each thread to end, before creating another thread.

    If more than one thread existed at once, you would see more unique thread IDs.


    > handle = (HANDLE) _beginthread(Thread,0,&val);
    This is dangerous code.
    You're passing a pointer to a local variable into a thread. If this local variable changes, or goes out of scope, then the thread will end up with garbage data (or a crash) when it tries to dereference the pointer.

    You need to make sure the input pointer is unique (and persistent) for each thread you create.
    Code:
    		int *param = malloc(sizeof(*param));  // one int
    		*param = i;
    		handle = (HANDLE) _beginthread(Thread,0,param);
    ...
    
    void Thread(void *p)
    {
    	int *param = p;
    
    	// use param
    	free(param);  // all done, call it before thread exits
    	
    	_endthread();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Smile


    Thanks for your reply
    I got it.

IMN logo majestic logo threadwatch logo seochat tools logo