Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tlug: Multithreading, how to learn?



On Wed, 20 Jan 1999, Hernando TANAKA wrote:

> At Wed, 20 Jan 1999 21:41:39 +0100,
> mike.fabian@example.com wrote:
> > What is  a  multithreaded  C or  C++  program,
> 
> You need to know what multithread is?
> Thread is kinda "light-weight" process.
> Instead of creating a new process by fork(),
> you create a new "thread" by something like pthread_create().
> 
> The cost of creating a new thread is much lower
> than creating a new process, and it's way much easier to share
> data between thread than between processes.
> 
> It's often used to have multiple execution points
> in the program, having several tasks running concurrently.
> 
> 
> > for what purposes is multithreading needed
> 
> There are mainly two purposes for using multithread programing;
> 
> 1. To make the program run faster.
> 2. To prevent the program to hang while doing some time-consuming
>    calculation or operation.
> 
> The first one is usually meaningful only if you have multi-CPUs
> and your OS supports multithreads running on different CPUs,
> AND your task is very heavy or time-consuming.
> 
> The second one is perhaps the top most reason to use multithread.
> You can have the program calculate, say, the julian set,
> while enabling the user to interact with the GUI.
> 
> 
> > how does one write a multithreaded C or C++ program?
> 
> I use pthread (POSIX-thead) library.
> Actually it comes with RedHat Linux.
> I'm sure they are also available for many other linux distributions.
> I think pthread was embeded in the new glibc2, but not sure.

It is - lilbpthread is part of glibc2.  I just taught myself (basically)
how to do this... something like this:

1.  Define the starting function of the child thread, like:

void *childThreadProcess(void *data) {
...
}

(it has to have that return type and parameter type).

2.  do a pthread_create(&thread,NULL,childThreadProcess,argument).  thread
should be a 'pthread_t'.  The second argument can optionally be a thread
attribute pointer, created with pthread_init_attr or something like that.
The third arg is of course the function to run, and argument is a void *
which you can typecast from pretty much anything.

3.  Away it goes.  Have your main program call pthread_cancel(thread) if
you want to stop the child thread, or have the child call pthread_exit if
it'll terminate itself.  Otherwise the thread terminates when
childThreadProcess returns.

I learned all this from reading 'man pthread_create'.  Follow the links to
the other man pages that it references and it should get you going.  This
wans't nearly as difficult/complicated as I thought it would be.

--------------------------------------------------
Scott M. Stone <sstone@example.com>
Head of TurboLinux English / Systems Administrator
Pacific HiTech, Inc. (http://www.turbolinux.com)


-------------------------------------------------------------------
Next Technical Meeting: February 13 (Sat), 12:30 place: Temple Univ.
** presentation: XEmacs, by Steven Baur and Martin Buchholz
Next Nomikai: March 19 (Fri), 19:30   Tengu TokyoEkiMae 03-3275-3691
-------------------------------------------------------------------
more info: http://tlug.linux.or.jp                     Sponsor: PHT


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links