Example of multithreaded programming with Pthreads Library in C language programming _c language

Source: Internet
Author: User
Tags gtk win32

Some configuration needs to be done before running:
1. Download pthread Windows Development Pack Pthreads-w32-2-4-0-release.exe (available in any one version)
http://sourceware.org/pthreads-win32/, extract to a directory.
2. Locate the Include and Lib folders, and add them below the vc++6.0 header file path and the static link library path, respectively:
a). Tools->options, select the Directory page, and then select Include files (default) in directories to add the path to include in the show directories for:. In Show directories for: Select library files,
Add the path to the Lib in directories.
b). project->settings, select the link page, and then add the *.lib file under Lib to the Object/library Modules,
Each lib file is separated by a space.
c). Copy the *.dll file under Lib to the engineering directory, which is the root directory.

We do multithreaded programming, can have a variety of options, you can use WINDOWSAPI, if you are using GTK, you can also use GTK to achieve the line threading, if you want your program to have more portability you'd better choose POSIX in the Pthread function library, My program is written in Linux, so I used the Pthread library (is not very sad, I know a lot of people are looking forward to the WINDOWSAPI, well, have the opportunity to talk about that later, now write this series of topics ^_^)

If you're using LINUX/UNIX/MACOSX, then we're ready to start, and if you're using Windows, you'll need to download Pthread's Windows Development Kit from the Web, but luckily he's very small. The website address is http://sourceware.org/pthreads-win32/

Example
let's look at a basic example:

#include <pthread.h>
#include <iostream>

using namespace std;

void* Tprocess1 (void* args) {while
   (1) {
     cout << "Tprocess1" << Endl;
   }
   return NULL;
}

void* Tprocess2 (void* args) {while
   (1) {
     cout << "Tprocess2" << Endl;
   }
   return NULL;
}

int main () {
   pthread_t t1;
   pthread_t T2;
   Pthread_create (&t1,null,tprocess1,null);
   Pthread_create (&t2,null,tprocess2,null);
   Pthread_join (t1,null);
   return 0;
}

In the above example, we first added the Pthread.h file contains, which is so necessary for the pthread multithreaded process, followed by the iostream of our input and output, followed by the definition of two functions, which is no different from ordinary functions,

void* Tprocess1 (void* args)

This form is entirely to cater to the parameter types of the Pthread_create function, and you can not define it as long as you force the pointer type to be cast when the pthread_create creation thread is invoked.

These two functions will be used as the execution body of the thread, that is to say, the two functions are run at the same time in two Chengri.

Now let's look at the main function, and the pthread-related calls are here.
pthread_t is a thread structure that is used to hold thread-related data, and you can also interpret it as a thread type, declaring a thread object (variable).

   pthread_t T1;
   pthread_t T2;

Here we declare two thread variables T1,t2

   Pthread_create (&t1,null,tprocess1,null);
   Pthread_create (&t2,null,tprocess2,null);

These two sentences are very important, pthread_create is used to create threads and start, and his prototype is

int pthread_create (pthread_t * thread, pthread_attr_t * attr, void * (*start_routine) (void *), void * arg);

We can know that the first parameter is the thread pointer, the second parameter is the thread property pointer, the thread attribute pthread_attr_t is used to specify attributes such as thread priority, in general, we do not need to modify, use the default property to construct the thread, so this is generally null, we do so, The third argument is a function pointer (function pointer?). What have you never heard of? ...... Big Halo, well, you review the C or C + + pointer that part of the code is the thread to execute, here we have to execute Tprocess1 tprocess2 is written above, where the type definition of this function pointer is to return an empty type pointer, To receive a function pointer to an empty type pointer parameter, if your function is not this definition, then you can directly convert it.

After writing these two lines of code, two threads have been executed, but if you omit the

   Pthread_join (T1,null);

Then, when you try to compile the program, you'll notice that the program is out of the way, and yes, that's because the operating system shuts down all the resources that the application uses, including threads, when the main thread of the program exits. So before the end of main function we have to find a way to stop the program, the function of the Pthread_join method is to wait for the thread to end, the thread to wait is the first parameter, the program will stop in this place until the thread is finished, the second parameter is used to accept the return value of the thread function, The pointer to the type, if there is no return value, set it directly to NULL.

Program written well, how do we compile to run it?
If you are using Linux:
Enter in the terminal

g++ thread.cpp-othread-lpthread
./thread

You can finish compiling and running the program.

If you are using the VC:
Add several library files in the development package to the engineering attributes
Put those DLL files into your engineering path, that is, the working path of the program running, this in VC6 and 2005 seems different, if you are not sure, then put it directly into the SYSTEM32 ...
The work below is very simple.
Point run, prompted to compile, OK, OK, the results came out ...

is not the feeling of multithreading so simple, a few lines of code is done, I think you can write a simple multithreaded program it, oh, in fact, the problem is not so simple, multithreading we have to face the problem of thread synchronization, I will be in the next topic for everyone to talk about.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.