Linux under C + +, multithreading pthread "reprint"

Source: Internet
Author: User
Tags function prototype

Original URL: http://www.cnblogs.com/xianghang123/archive/2011/08/11/2134927.html  • Thread Creation

Function prototype: int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void * (*start_ RTN) (void), void *restrict arg);

Return value: If the thread is successfully established returns 0, the wrong number is returned.

Formal parameters: pthread_t *restrict TIDP The thread ID pointer of the thread to be created; const pthread_attr_t *restrict attr Thread properties when creating threads; void* (START_RTN) (void) The return value is a pointer function of type void, and the formal parameter of void *restrict arg start_rtn.

  • Thread hangs: The function causes the current thread to hang, waiting for another thread to return before proceeding. This means that when the program runs to this place, the program stops and waits for the thread ID thread to return, and then the program will execute intermittently.

Function prototype: int pthread_join(pthread_t thread, void **value_ptr);

The parameters are described as follows: Thread waits for the thread to exit threads, value_ptr The return value of the exit thread.

· thread Exits

  Function prototype: void pthread_exit(void *rval_ptr);

· gets the current thread ID

Function prototype: pthread_t pthread_self(void);

· Mutual exclusion Lock

Create pthread_mutex_init; destroy Pthread_mutex_destroy; locking pthread_mutex_lock; unlock pthread_mutex_unlock.

· Conditional Lock

Create pthread_cond_init; destroy Pthread_cond_destroy; trigger pthread_cond_signal; broadcast Pthread_cond_broadcast S; wait for Pthread_ Cond_wait.

• Correctly handle thread-end issues under Linux platforms

Under the Linux platform, one of the issues to be aware of when processing threads end is how to make a thread finish and let its resources be released correctly. On the Linux platform by default, the termination of one thread does not notify or affect other threads, although the threads are independent of each other. But the resources of the terminated thread will not be freed as the thread terminates, we need to call Pthread_join () to get the terminating state of the other thread and release the resources that the thread occupies.

"With C"

/* example.c*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void thread (void)
{
int i;
for (i=0;i<3;i++) {
Sleep (1);
printf ("This is a pthread.\n");}
}
int main (void)
{
pthread_t ID;
int I,ret;
Ret=pthread_create (&id,null, (void *) thread,null);
if (ret!=0) {
printf ("Create pthread error!\n");
Exit (1);
}
for (i=0;i<3;i++) {
printf ("This is the main process.\n");
Sleep (1);
}
Pthread_join (Id,null);
return (0);
}

Compile command Gcc-o example Example.c-lpthread

Note: the "1" pthread library is not the default library for Linux systems and requires static library LIBPTHREAD.A when connecting, so when creating a thread with pthread_create () and calling the Pthread_atfork () function to set up a fork handler, You need to link to the library.

"2" also add header file Pthread.h

"3" because C library comes with sleep, so you can not add Unistd.h header file

"In C + +"

/* example.cpp*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *thread (void *ptr)
{
int i;
for (i=0;i<3;i++) {
Sleep (1);
printf ("This is a pthread.\n");}
}
int main (void)
{
pthread_t ID;
int I,ret;
Ret=pthread_create (&id,null,thread,null);
if (ret!=0) {
printf ("Create pthread error!\n");
Exit (1);
}
for (i=0;i<3;i++) {
printf ("This is the main process.\n");
Sleep (1);
}
Pthread_join (Id,null);
return (0);
}

Compile link command g++-o example2 example.c-lpthread

"Note" In the highlighted section, note the C + + development and C differences, explained as follows:

We note the prototype of the threading function in the POSIX definition:
extern int pthread_create (pthread_t *__restrict __threadp,
                             __const pthread_attr_t *__restrict __attr,
                             void * (*__start_routine) (void *),
                            void *__ Restrict __arg) __throw; The third argument in the
call is to load a Let's take a look at how this prototype was invoked in the original function, basically a similar invocation:

The meaning of this expression: take a pointer to the function main_thread, and then convert it to a generic pointer.
This means that obviously the above two things are not the same thing, so the correct way to call is
Ret=pthread_create (&id,null,thread,null);
The processing function is defined as follows:
void *thread (void *ptr)
It is important to note that GCC compiles without errors, but with g++ there is a problem: invalid conversion from ' void* ' to ' void* (*) (void*), The reason for this is that the C language compiler allows implicit conversion of a generic pointer to any type of pointer, while C + + does not allow it.

Category: Linux/C + +

Linux under C + +, multithreading pthread "reprint"

Related Article

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.