Solved

pthread_create() of unix to vc++ call

Posted on 2000-03-08
10
789 Views
Last Modified: 2013-12-03
hi,  

I have done  a project in c on unix machine.In that I have used pthread_create and pthread_cancel system calls.

Now i want to port that code to vc++ IDE which is installed on windowsNT and I want to convert those unix thread calls to vc++ related calls.I can i chane them.Here I  am giving the calls i have used.
pthread_t tid;

pthread_create(&tid,pthread_attr_default,Execute_thread,&expr);
and
pthread_cancel(tid);

When the thread is created that tid ,I am adding to a linked list.Inwhich tid is of type pthread_t. The same operation is to be done here also.

In vc++ compatible thread call How can i access the tid values.

please do it .It is urgent.

regards
AdinarayanaK


0
Comment
Question by:adinarayanak
  • 4
  • 4
  • 2
10 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 2596267
Hi adinarayanak,

from Windows API you can use CreateThread() to create a thread and TerminateThread to terminate it. CreateThread() returns the thread's handle which must be passed to TerminateThread().

hope that helps,

ZOPPO
0
 
LVL 22

Accepted Solution

by:
nietod earned 25 total points
ID: 2596523
>> you can use CreateThread()
True, but for a C/C++ program written in VC you need to use _beginthread() or _beginthreadex() to start the new thread, otherwise the standard library may not work correctly.  Also _endthread() _endthreadex() to end the threads.

>> In vc++ compatible thread call How can
>> i access the tid values
windows has two things that identify threads.  A thread ID and a thread handle.  The thread ID really only identifies the thread while the handle allows you to "control"  (affect?) the thread.

You may want to use just the handle, not the ID, but I'm not sure what your needs are.

_beginthread() will return the thread handle for you.
GetCurrentThreadID() returns the current thread's ID
GetCurrentThread() returns a handle that identifies the current thread, but this handle isn;t a "real" thread handle.  it can be used only by the current thread, it cannot be used by other threads.  (Actually it alwasy identifies the thread that is using it, not the thread that was current when the handle was created!)  However you can use DuplicateHandle() to create a real thread handle from this psuedo-handle.

Let me know if you have any questions.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2596556
Hi nietod, I have a question: How to terminate a thread which is not the current thread without TerminateThread()?
0
 

Author Comment

by:adinarayanak
ID: 2596735
well it is right.If I use CreateThread() call and I will pass handle to terminatethread(). What is the type of the handle ?.

if i want to put that handle into the linked list,what type should i declare in the typedef structure of the linked list ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2596830
>> How to terminate a thread which is not the
>> current thread without TerminateThread()?

you can just call TerminateThread().  That will leave memory leaks in the standard library because the thread-specific info doesn't get cleaned up.  But in general TermianteThread is going to cause memory leaks and other problems answays, so what is one or two more problems.

Basically, that is a last ditch resort.  You should not be using it on a regular basis anyways.

>> use CreateThread() call and I will pass
>> handle to terminatethread().
using CreateThread is discouraged in windows C/C++.  use _beginthread() isntead.  see my comment above.

Using TerminateThread() is always discouraged.  You should use it only if a thread seems to be malfunctioning and cannot be terminated in any other way.    

To end a thread return from the thread procedure or use _endthread(). (In C++ returning is slightly preferred as you wil be sure to destroy all your local variables that way.)

>>  want to put that handle into the linked
>> list,what type should i declare
All handles in windows are 32 bit types.  The windows API functions that deal with threads (like CreateThread()) use a type called "HANDLE" as a thread handle.  This is defined somewhere in the recesses of windows.h.  it is defined as an unsigned 32 bit value, like "unsigned int"  However the _beginthread() and associated functions use "unsigned long" (also unsigned  32 bits) for thread handels.  This is so they can be used without including windows.h  (since these functions are not part of the windows API, and may be used on programs that aren't really windows-specific.)

So if you will be including windows.h--to use other windows-specific features--call the type HANDLE.  otherwise, just call them type "unsigned long".
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:adinarayanak
ID: 2599919
Ok if i use _beginThread() and _endThread.

In my problem I am calling this thread call in a function which takes the head(which is * to a structure) and a char *expr.The expression will be different in each time.Whan ever an expression is passed a new thread will be created with different handle.then I want to put that handle in the linked list.

Suppose i have passed 5 expressions,there by i will create 5 threads.Then if i want to cancel the 2 nd thread.How to do it._end thread will not take any parameters.I have the hanle in the linked list.How can i use it to cancel a thread.

please reply me soon.

regards,
AdinarayanaK
0
 

Author Comment

by:adinarayanak
ID: 2599952
Ok if i use _beginThread() and _endThread.

In my problem I am calling this thread call in a function which takes the head(which is * to a structure) and a char *expr.The expression will be different in each time.Whan ever an expression is passed a new thread will be created with different handle.then I want to put that handle in the linked list.

Suppose i have passed 5 expressions,there by i will create 5 threads.Then if i want to cancel the 2 nd thread.How to do it._end thread will not take any parameters.I have the hanle in the linked list.How can i use it to cancel a thread.

please reply me soon.

regards,
AdinarayanaK
0
 
LVL 22

Expert Comment

by:nietod
ID: 2600179
What are these threads doing?

Are they able to determine when they are finished doing their work?

They should usually just return from their main procedure when they are finished, that will end them.

>> _end thread will not take any parameters
_endthread() ends the current thread, not a different thread.  It is a very bad idea to end a different thread.  TerminateThread() will end a different thread, but it is only to be used for extreme cases.  It will leave memory leaiks and perhaps other types of problems.  No design should depend on using it as a primary means to end threads, only as a last resort.  Instead you need to design your program so the threads can end themsleves when needed.  However, I'm not sure what the threads are doing so I can't make any recomendations yet.
0
 

Author Comment

by:adinarayanak
ID: 2600730

each expression will be taken by different thread(i e the expression is passed as the arg to thread function).In the thread function we have an infinite loop with some Sleep().as long as the thread is active this will continue to run in the loop.In the loop It will wait for an interval and does some important work.

mean while the user can fire another expression which can be taken care by another another thread.

suppose if the user want to cancel the  any one of thread then he searches for that expression already configured, in the linked list and will get the thread handle which is stored at the time of creation and will pass to the TerminateThread().

here I am sending a sample code please compile it on

windows NT version 4.0 and vc++ 6 IDE.

#include <stdio.h>
#include <windows.h>
#include <process.h>

void printmsg1(int *expr)
{
      int i;
      int j;

      printf("I am in printmsg \n");

      for (i = 0;i<10;i++)
      {
            for(j=0;j<500000;j++)
            {      
      
                  printf(" got :  %d\n ",*expr);
                  Sleep(2000L);
            }

      }
}


int main()
{
      int j = 0;

      int a = 12;
      int b = 111;
    unsigned long thread1, thread2;

    if((thread1 = _beginthread(printmsg1, 0, &a) ) < 0)
      {
            printf("thread creation failed\n");
            return 0;
      }

    thread2 = _beginthread(printmsg1, 0, &b);

       printf("handle1 = %ld\n",thread1);
         printf("handle2 = %ld\n",thread2);

/*       WaitForSingleObject((HANDLE) thread1, INFINITE);

     WaitForSingleObject((HANDLE) thread2, INFINITE);  */

       Sleep(1000L);


       for(j=0;j<500000;j++)
       {
            printf("I am in parent loop\n");
            printf("count j = %d\n",j);
      
            if(j==5)
            
            if(TerminateThread((HANDLE)thread1,1) < 0)
            {
                  printf("threadTermination failed\n");
            }
            else
            {
                  printf("thread1 has been terminated\n");

            }
            
            if (j==7)

                  TerminateThread((HANDLE)thread2,1);

            Sleep(2000L);
      }

   

return 0;      
}
let me if at all any way other than TerminateThread()

0
 
LVL 22

Expert Comment

by:nietod
ID: 2600790
>> In the thread function we have an infinite
>> loop with some Sleep().as long as the thread
>> is active this will continue to run in the loop. In
>> the loop It will wait for an interval and does
>> some important work.
This is a very bad idea in windows and really not a good idea in UNIX either.  It is also a bad idea in an C++ program regardless of OS.

have the loop check for a termination condition of some sort.  Like just a bool stored in that linked list.  When you want the thread to end, flip the bool and the loop will soon end and the thread procedure will return.  

>> will get the thread handle which is stored at the
>> time of creation and will pass to the
>> TerminateThread()
That works but it will certainly leak memory and may have other undesirable effects.   For example, If the thread has an open file the file won't get closed.  if the thread has created a DC, the DC doesn't get released.  This can cause very bad behaviour.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now