- ·上一篇文章:C++ 信号处理
- ·下一篇文章:C++ Web 编程
C++ 多线程
joinable)或可分离的(detached)。只有创建时定义为可连接的线程才可以被连接。如果线程创建时被定义为可分离的,则它永远也不能被连接。这个实例演示了如何使用 pthread_join() 函数来等待线程的完成。实例
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>using namespace std;
#define NUM_THREADS 5void *wait(void *t){int i;long tid;tid = (long)t;sleep(1);cout << "Sleeping in thread " << endl;cout << "Thread with id :
" << tid << " ...exiting " << endl;pthread_exit(NULL);}int main (){int rc;int i;pthread_t threads[NUM_THREADS];pthread_attr_t attr;void *status;// 初始化并设置线程为可连接的(joinable)pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for( i=0; i < NUM_THREADS; i++ ){cout << "main() :
creating thread, " << i << endl;rc = pthread_create(&threads[i], NULL, wait, (void *)&i );if (rc){cout << "Error:
unable to create thread," << rc << endl;exit(-1);}}// 删除属性,并等待其他线程pthread_attr_destroy(&attr);for( i=0; i < NUM_THREADS; i++ ){rc = pthread_join(threads[i], &status);if (rc){cout << "Error:
unable to join," << rc << endl;exit(-1);}cout << "Main:
completed thread id :
" << i ;cout << " exiting with status :
" << status << endl;}cout << "Main:
program exiting." << endl;pthread_exit(NULL);}当上面的代码被编译和执行时,它会产生下列结果:main() :
creating thread, 0main() :
creating thread, 1main() :
creating thread, 2main() :
creating thread, 3main() :
creating thread, 4Sleeping in thread Thread with id :
4 ...exiting Sleeping in thread Thread with id :
3 ...exiting Sleeping in thread Thread with id :
2 ...exiting Sleeping in thread Thread with id :
1 ...exiting Sleeping in thread Thread with id :
0 ...exiting Main:
completed thread id :
0 exiting with status :
0Main:
completed thread id :
1 exiting with status :
0Main:
completed thread id :
2 exiting with status :
0Main:
completed thread id :
3 exiting with status :
0Main:
completed thread id :
4 exiting with status :
0Main:
program exiting.更多实例参考:http:
//www.k88.net/w3cnote/cpp-multithread-demo.html
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>using namespace std;
#define NUM_THREADS 5void *wait(void *t){int i;long tid;tid = (long)t;sleep(1);cout << "Sleeping in thread " << endl;cout << "Thread with id :
" << tid << " ...exiting " << endl;pthread_exit(NULL);}int main (){int rc;int i;pthread_t threads[NUM_THREADS];pthread_attr_t attr;void *status;// 初始化并设置线程为可连接的(joinable)pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for( i=0; i < NUM_THREADS; i++ ){cout << "main() :
creating thread, " << i << endl;rc = pthread_create(&threads[i], NULL, wait, (void *)&i );if (rc){cout << "Error:
unable to create thread," << rc << endl;exit(-1);}}// 删除属性,并等待其他线程pthread_attr_destroy(&attr);for( i=0; i < NUM_THREADS; i++ ){rc = pthread_join(threads[i], &status);if (rc){cout << "Error:
unable to join," << rc << endl;exit(-1);}cout << "Main:
completed thread id :
" << i ;cout << " exiting with status :
" << status << endl;}cout << "Main:
program exiting." << endl;pthread_exit(NULL);}当上面的代码被编译和执行时,它会产生下列结果:main() :
creating thread, 0main() :
creating thread, 1main() :
creating thread, 2main() :
creating thread, 3main() :
creating thread, 4Sleeping in thread Thread with id :
4 ...exiting Sleeping in thread Thread with id :
3 ...exiting Sleeping in thread Thread with id :
2 ...exiting Sleeping in thread Thread with id :
1 ...exiting Sleeping in thread Thread with id :
0 ...exiting Main:
completed thread id :
0 exiting with status :
0Main:
completed thread id :
1 exiting with status :
0Main:
completed thread id :
2 exiting with status :
0Main:
completed thread id :
3 exiting with status :
0Main:
completed thread id :
4 exiting with status :
0Main:
program exiting.更多实例参考:http:
//www.k88.net/w3cnote/cpp-multithread-demo.html
C++ 多线程