c++ thread函数原型
时间: 2023-09-04 13:16:22 浏览: 105
C++11 中,`thread` 函数的原型如下:
```cpp
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
```
其中,`Function` 是要在新线程中执行的函数对象类型,可以是函数指针、函数对象、Lambda 表达式等;`Args` 是传递给函数对象的参数类型。
在创建一个新线程时,可以使用以上原型,将函数对象和参数传递给 `thread` 构造函数,例如:
```cpp
void my_func(int x, double y) {
// do something
}
int main() {
std::thread t(my_func, 42, 3.14);
t.join();
return 0;
}
```
以上代码会创建一个新的线程 `t`,并将 `my_func(42, 3.14)` 作为线程的执行函数。调用 `join()` 等待线程执行完毕后退出。
相关问题
c++ 中 thread 函数
C语言中的thread函数是用于创建线程的函数,通过它可以在程序中创建一个新的线程来进行并发处理。它的原型为:
```c
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
```
其中,参数说明如下:
- thread:指向线程标识符的指针,用于返回新创建的线程标识符。
- attr:用于设置线程的属性,一般情况下可以设置为NULL。
- start_routine:线程的入口函数,它是一个指向函数的指针,该函数的返回值为void*类型,参数为void*类型。
- arg:传递给线程入口函数的参数,它可以是任何类型的数据,因为它是void*类型。
pthread_create函数的返回值是0表示线程创建成功,其他值则表示出错。
线程创建成功后,它会立即开始执行线程入口函数,并且运行在一个新的线程中。当线程入口函数返回时,线程也就结束了,但并不会释放线程的资源,需要通过pthread_join函数等待线程执行结束并释放其资源。
c++中的多线函数和linux中的多线程函数分别有哪些,全部给出函数原型
以下是C++标准库中常用的多线程函数的函数原型:
1. `std::thread`:
```cpp
namespace std {
class thread {
public:
thread() noexcept;
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
~thread();
thread( const thread& ) = delete;
thread& operator=( const thread& ) = delete;
thread( thread&& other ) noexcept;
thread& operator=( thread&& other ) noexcept;
void swap( thread& other ) noexcept;
bool joinable() const noexcept;
void join();
void detach();
id get_id() const noexcept;
static unsigned int hardware_concurrency() noexcept;
};
}
```
2. `std::mutex`:
```cpp
namespace std {
class mutex {
public:
mutex();
~mutex();
mutex( const mutex& ) = delete;
mutex& operator=( const mutex& ) = delete;
void lock();
bool try_lock();
void unlock();
};
}
```
3. `std::condition_variable`:
```cpp
namespace std {
class condition_variable {
public:
condition_variable();
~condition_variable();
condition_variable( const condition_variable& ) = delete;
condition_variable& operator=( const condition_variable& ) = delete;
void notify_one() noexcept;
void notify_all() noexcept;
void wait( unique_lock<mutex>& lock );
template< class Predicate >
void wait( unique_lock<mutex>& lock, Predicate pred );
template< class Clock, class Duration >
cv_status wait_until( unique_lock<mutex>& lock, const chrono::time_point<Clock,Duration>& abs_time );
template< class Clock, class Duration, class Predicate >
bool wait_until( unique_lock<mutex>& lock, const chrono::time_point<Clock,Duration>& abs_time, Predicate pred );
template< class Rep, class Period >
cv_status wait_for( unique_lock<mutex>& lock, const chrono::duration<Rep,Period>& rel_time );
template< class Rep, class Period, class Predicate >
bool wait_for( unique_lock<mutex>& lock, const chrono::duration<Rep,Period>& rel_time, Predicate pred );
};
}
```
4. `std::atomic`:
```cpp
namespace std {
template< class T >
struct atomic {
atomic() noexcept = default;
constexpr atomic( T desired ) noexcept;
atomic( const atomic& ) = delete;
atomic& operator=( const atomic& ) = delete;
T load( memory_order order = memory_order_seq_cst ) const noexcept;
void store( T desired, memory_order order = memory_order_seq_cst ) noexcept;
operator T() const noexcept;
T exchange( T desired, memory_order order = memory_order_seq_cst ) noexcept;
bool compare_exchange_weak( T& expected, T desired,
memory_order success = memory_order_seq_cst,
memory_order failure = memory_order_seq_cst ) noexcept;
bool compare_exchange_strong( T& expected, T desired,
memory_order success = memory_order_seq_cst,
memory_order failure = memory_order_seq_cst ) noexcept;
atomic& operator=( T desired ) noexcept;
T operator++( int ) noexcept;
T operator--( int ) noexcept;
T operator++() noexcept;
T operator--() noexcept;
T operator+=( T arg ) noexcept;
T operator-=( T arg ) noexcept;
T operator&=( T arg ) noexcept;
T operator|=( T arg ) noexcept;
T operator^=( T arg ) noexcept;
};
}
```
以下是Linux系统中常用的多线程函数的函数原型:
1. `pthread_create`:
```cpp
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
```
2. `pthread_join`:
```cpp
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
```
3. `pthread_mutex_lock`:
```cpp
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
```
4. `pthread_mutex_unlock`:
```cpp
#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
```
5. `pthread_cond_wait`:
```cpp
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
```
6. `pthread_cond_signal`:
```cpp
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);
```
阅读全文