pthread是C++98接口且只支持Linux,使用时需要包含头文件#include
int pthread_create (pthread_t *thread,
pthread_attr_t *attr,
void *(*start_routine)(void *),void *arg);
若创建成功,返回0;若出错,则返回错误编号。
pthread_attr_t 操作函数:
pthread_attr_t attr; // 声明attr
pthread_attr_init(&attr); // 创建attr
pthread_attr_destroy(&attr); // 销毁attr
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // 设置为joinable
void pthread_exit (void *retval);
int pthread_cancel (pthread_t thread);
当发生以下情形之一时,线程就会结束:
一个线程可以通过调用 pthread_cancel 函数来请求取消同一进程中的线程,这个线程由thread 参数指定。如果操作成功则返回0,失败则返回对应的错误编号。
int pthread_join(pthread_t threadid, void **value_ptr);
int pthread_detach (threadid);
阻塞是线程之间同步的一种方法。pthread_join 函数会让调用它的线程等待 threadid 线程运行结束之后再运行,value_ptr 存放了其他线程的返回值。
一个可以被join的线程,仅仅可以被别的一个线程 join,如果同时有多个线程尝试 join 同一个线程时,最终结果是未知的。另外,线程不能 join 自己。
值得注意的的是:僵尸线程 ( “zombie” thread )是一种已经退出了的 joinable 的线程,但是等待其他线程调用
pthread_join 来 join 它,以收集它的退出信息(exit status)。如果没有其他线程调用 pthread_join 来
join 它的话,它占用的一些系统资源不会被释放,比如堆栈。如果main()函数需要长时间运行,并且创建大量 joinable的线程,就有可能出现堆栈不足的 error 。 对于那些不需要 join 的线程,最好利用
pthread_detach,这样它运行结束后,资源就会及时得到释放。注意一个线程被使用 pthread_detach
之后,它就不能再被改成 joinable 的了。 总而言之,创建的每一个线程都应该使用 pthread_join 或者
pthread_detach 其中一个,以防止僵尸线程的出现。
pthread_self (); // 返回当前线程的 thread ID
pthread_equal (thread1,thread2); // pthread_equal 比较两个线程的 ID,如果不同则返回0,否则返回一个非零值
// 互斥锁
pthread_mutex_t mutexsum;
pthread_mutex_init (mutex,attr);
pthread_mutex_destroy (pthread_mutex_t *mutex);
pthread_mutexattr_init (attr);
pthread_mutexattr_destroy (attr);
phtread_mutex_lock(pthread_mutex_t *mutex);
phtread_mutex_trylock(pthread_mutex_t *mutex);
phtread_mutex_unlock(pthread_mutex_t *mutex);
#include
#include
#include
#include
#define NUM_THREADS 4
void *BusyWork(void *t)
{
int i;
long tid;
double result = 0.0;
tid = (long) t;
printf("Thread %ld starting...\n", tid);
for (i = 0; i < 1000000; i++)
{
result = result + sin(i) * tan(i);
}
printf("Thread %ld done. Result = %e\n", tid, result);
pthread_exit((void *) t);
}
int main(int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (t = 0; t < NUM_THREADS; t++)
{
printf("Main: creating thread %ld\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *) t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Free attribute and wait for the other threads */
pthread_attr_destroy(&attr);
for (t = 0; t < NUM_THREADS; t++)
{
rc = pthread_join(thread[t], &status);
if (rc)
{
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
printf("Main: completed join with thread %ld having a status of %ld\n", t, (long) status);
}
printf("Main: program completed. Exiting.\n");
pthread_exit(NULL);
}
Main: creating thread 0
Main: creating thread 1
Thread 0 starting...
Thread 1 starting...
Main: creating thread 2
Main: creating thread 3
Thread 2 starting...
Thread 3 starting...
Thread 1 done. Result = -3.153838e+06
Thread 0 done. Result = -3.153838e+06
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 1
Thread 3 done. Result = -3.153838e+06
Thread 2 done. Result = -3.153838e+06
Main: completed join with thread 2 having a status of 2
Main: completed join with thread 3 having a status of 3
Main: program completed. Exiting.
参考链接:C++ 多线程编程(二):pthread的基本使用