An example of multithreaded programming using the pthreads library in C language programming

  • 2020-05-05 11:38:17
  • OfStack

Some configuration needs to be done before can be run:
1. Download pthreads-w32-2-4-0-release. exe (any version is available)
    http: / / sourceware org/pthreads - win32 /, unzip to a directory.
2. Find the include and lib folders and add them to the VC++6.0 header path and the static link library path respectively:
    a).Tools- > Options, select Directory page, then in Show directories for: select Include files(default)         add include path to Directories. In Show directories for: select Library files,
      adds the path of lib to Directories.
    b).Project- > Settings, select the Link page, and then add the *.lib file under lib to Object/library Modules,
        each lib file is separated by a space.
Copy the *.dll file under lib to the project directory, the root directory.  

We multithreaded programming, there can be multiple choice, you can use WindowsAPI, if you're using GTK, can also use GTK implements threading library, if you want to make your program more portability is your best choice of POSIX Pthread function library, my program is written under the Linux, so I use the Pthread library (is very sad, I know a lot of people are looking forward to is WindowsAPI, ok, have a chance to speak again after that, now I'll finish this a series of special ^_^)

If you use LINUX/UNIX/MacOSX, then we are ready to start. If you use WINDOWS, then you need to download PTHREAD's WINDOWS development kit from the website. Fortunately, it is very small. Web site address is http: / / sourceware org/pthreads - win32 /

example
let's start with 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 example above, we first added pthread.h, which is necessary for pthread multithreaded programs, and then iostream which we use for input and output, and then the definition of the two functions, which is no different from normal functions, why


void* tprocess1(void* args)

This form is all for the parameter type of the pthread_create function, and you can simply cast the pointer type when calling pthread_create to create the thread.

These two functions will be used as the execution body of the thread, that is, both functions will be run in two threads at the same time.

Now let's look at the main function, all the calls to pthread are here.
pthread_t is a thread structure used to hold thread-related data. You can also interpret it as a thread type and declare 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 them. Its 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 pointer to the thread, the second parameter is a pointer to thread attributes, thread attributes pthread_attr_t thread priority is used to specify properties, such as the general case, we don't need to modify, use the default properties to construct the thread, so here generally take NULL, we are also doing so, the third parameter is a function pointer (function pointer? What? Never heard of it? ... Huge halo, well, you review the C or C + + pointer that part) is a thread to execute code, here we respectively to perform tprocess1 tprocess2 was written above, here the function pointer type definition is to return a null pointer type, receives a null pointer type parameters of a function pointer, if your function is not this definition, it can directly into the can.

After writing these two lines of code, the two threads are already executing, but if you omit


   pthread_join(t1,NULL);

Then when you try to compile and run the program, it seems to quit without doing anything. Yes, that's because when the main thread of the program exits, the operating system shuts down all the resources the application USES, including threads... So in the end we have to think of some way to get main function program to stop, the function of the pthread_join method is waiting thread ends, to wait for the thread is the first parameter, the program will stop in this place, until the end of the thread, the second parameter is used to accept the thread function return values, is void * * pointer type, if there is no return value, can be directly set as NULL.

Once the program is written, how do we compile and run it?
If you are using Linux:
Enter
in the terminal


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

You can compile and run

If you use VC:
Add several library files
from the development kit to the project properties Put those DLL files in your project path, which is the working path of the program when it runs. This seems different in VC6 than it did in 2005. If you are not sure, just put them in SYSTEM32...
Here's the easy part:
Click run, prompt compile, ok, there you go...

Is the feeling that multithreading is so simple, just a few lines of code to get rid of, I think you can already write a simple multithreaded program, ha ha, in fact, the problem is not so simple, multithreading we have to face the problem of thread synchronization, I will give you in the next topic.  


Related articles: