本文整理汇总了C++中Pthread_create函数的典型用法代码示例。如果您正苦于以下问题:C++ Pthread_create函数的具体用法?C++ Pthread_create怎么用?C++ Pthread_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pthread_create函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc,char **argv)
{
pthread_t produce_tid,consume_tid;
if(argc!=2)
err_quit("usage: mycat <pathname> ");
if((fd=open(argv[1],O_RDONLY))==-1)
err_sys("open error");
Sem_init(&shared.mutex,0,1);
Sem_init(&shared.nempty,0,NBUFF);
Sem_init(&shared.nstored,0,0);
Set_concurrency(2);
Pthread_create(&produce_tid,NULL,produce,NULL);
Pthread_create(&consume_tid,NULL,consume,NULL);
Pthread_join(produce_tid,NULL);
Pthread_join(consume_tid,NULL);
Sem_destroy(&shared.mutex);
Sem_destroy(&shared.nempty);
Sem_destroy(&shared.nstored);
exit(0);
}
示例2: main
int main(int argc, char **argv)
{
int listenfd, *connfd, port, clientlen, i;
struct sockaddr_in clientaddr;
pthread_t tid;
port_queue prod_var;
/* Check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
port = atoi(argv[1]);
connfdqp = queueInit ();
prod_var.port = port;
prod_var.q = connfdqp;
Pthread_create(&tid, NULL, producer, &prod_var);
printf("Producer thread created %u\n", (unsigned int) tid);
Pthread_detach(tid);
printf ("Producer thread detached\n");
for (i = 0; i < MAXTHREAD; i++) {
Pthread_create(&tid, NULL, consumer, connfdqp);
printf("Consumer thread created %u\n", (unsigned int) tid);
Pthread_detach(tid);
printf("Consumer thread detached\n");
}
printf("Main thread exited\n");
Pthread_exit(NULL);
}
示例3: main
int main(int argc, char *argv[])
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if(argc != 3)
err_quit("usage: prodcons2 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
Set_concurrency(nthreads + 1);/*线程并发设置*/
/*创建线程生产者*/
for(i = 0; i < nthreads; ++i){
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
/*创建消费者线程*/
Pthread_create(&tid_consume, NULL, comsume, NULL);
/*主线程等待子线程结束*/
for(i = 0; i < nthreads; ++i){
Pthread_join(tid_produce[i], NULL);
printf ("count[%d] = %d\n",i, count[i]);
}
Pthread_join(tid_consume, NULL);
return 0;
}
示例4: main
int main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if (argc != 2)
err_quit("Usage: buffer <pathname>");
fd = Open(argv[1], O_RDONLY);
Sem_init(&shared.mutex, 0, 1);
Sem_init(&shared.nempty, 0, NBUFF);
Sem_init(&shared.nstored, 0, 0);
Pthread_setconcurrency(2);
Pthread_create(&tid_produce, NULL, produce, NULL);
Pthread_create(&tid_consume, NULL, consume, NULL);
Pthread_join(tid_produce, NULL);
Pthread_join(tid_consume, NULL);
Sem_destroy(&shared.mutex);
Sem_destroy(&shared.nempty);
Sem_destroy(&shared.nstored);
exit(0);
}
示例5: main
int main(int argc, char **argv)
{
int niters;
pthread_t tid1, tid2;
//check input argument
if (argc != 2) {
printf("usage: %s <niters>\n", argv[0] );
exit(0);
}
niters = atoi(argv[1]);
//create threads and wait for them to finish
Pthread_create(&tid1, NULL, thread, &niters);
Pthread_create(&tid2, NULL, thread, &niters);
Pthread_join(tid1, NULL);
Pthread_join(tid2, NULL);
//check result
if (cnt != (2 * niters))
printf("BOOM! cnt=%d\n", cnt);
else
printf("OK cnt=%d\n", cnt);
exit(0);
}
示例6: abc_para_test
template<bool check_depleted> void abc_para_test() {
concat<char> str
, *dep1 = new concat<char>
, *dep2 = new concat<char>
;
assert(!str.ready());
assert(!dep1->closed());
assert(!dep2->closed());
pthread_t thread[2];
Pthread_create(&thread[0], NULL, abc, dep1);
Pthread_detach(thread[0]);
Pthread_create(&thread[1], NULL, abc, dep2);
Pthread_detach(thread[1]);
str.give(dep1);
str.give(dep2);
str.close();
for (int i = 0; i < 2; i++) {
for (char let = 'a'; let <= 'z'; let++) {
if (check_depleted) {
assert(!str.depleted());
assert(str.ready());
}
assert(str.get() == let);
}
}
assert(!str.ready());
assert(str.depleted());
assert(str.closed());
}
示例7: main
int
main(int argc, char *argv[])
{
int i, nthreads, count[MAXNITEMS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if (argc != 3)
err_quit("usage: producons2 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads= min(atoi(argv[2]), MAXNTHREADS);
Set_concurrency(nthreads);
for (i = 0; i < nthreads; i++) {
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
for (i = 0; i < nthreads; i++) {
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d \n", i, count[i]);
}
Pthread_create(&tid_consume, NULL, consume, NULL);
Pthread_join(tid_consume, NULL);
return 0;
}
示例8: main
/* include main */
int
main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if (argc != 3)
err_quit("usage: prodcons4 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);
Set_concurrency(nthreads + 1);
/* 4create all producers and one consumer */
for (i = 0; i < nthreads; i++) {
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
Pthread_create(&tid_consume, NULL, consume, NULL);
/* wait for all producers and the consumer */
for (i = 0; i < nthreads; i++) {
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
Pthread_join(tid_consume, NULL);
exit(0);
}
示例9: main
int
main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if (argc != 1)
err_quit("usage: deadlock <#items>");
nitems = atoi(argv[1]);
/* 4create three semaphores */
shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL,
FILE_MODE, 1);
shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL,
FILE_MODE, NBUFF);
shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL,
FILE_MODE, 0);
Set_concurrency(2);
Pthread_create(&tid_produce, NULL, produce, NULL);
Pthread_create(&tid_consume, NULL, consume, NULL);
Pthread_join(tid_produce, NULL);
Pthread_join(tid_consume, NULL);
Sem_unlink(Px_ipc_name(SEM_MUTEX));
Sem_unlink(Px_ipc_name(SEM_NEMPTY));
Sem_unlink(Px_ipc_name(SEM_NSTORED));
exit(0);
}
示例10: main
int main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if (argc != 2)
err_quit("Usage: pc <#items>");
nitems = atoi(argv[1]);
//create three semaphore
shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1);
shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF);
shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0);
//create one producer thread and one consumer thread
Pthread_setconcurrency(2);
Pthread_create(&tid_produce, NULL, produce, NULL);
Pthread_create(&tid_consume, NULL, consume, NULL);
//wait for the two threads
Pthread_join(tid_produce, NULL);
Pthread_join(tid_consume, NULL);
//remove the semaphores
Sem_unlink(Px_ipc_name(SEM_MUTEX));
Sem_unlink(Px_ipc_name(SEM_NEMPTY));
Sem_unlink(Px_ipc_name(SEM_NSTORED));
exit(0);
}
示例11: main
int main(int argc, char *argv[])
{
pthread_t tid_produce, tid_consume;
if(argc != 2)
err_quit("usage: prodcons1 <#items>");
nitems = atoi(argv[1]);
/*创建信号量*/
shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1);
shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF);
shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0);
Set_concurrency(2);/*线程并发处理*/
/*创建两个线程*/
Pthread_create(&tid_produce, NULL, produce, NULL);
Pthread_create(&tid_consume, NULL, consume, NULL);
/*主线程等待两个线程*/
Pthread_join(tid_produce, NULL);
Pthread_join(tid_consume, NULL);
/*释放信号量*/
Sem_unlink(Px_ipc_name(SEM_MUTEX));
Sem_unlink(Px_ipc_name(SEM_NEMPTY));
Sem_unlink(Px_ipc_name(SEM_NSTORED));
return 0;
}
示例12: main
int
main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if (argc != 2)
err_quit("usage: mycat2 <pathname>");
fd = Open(argv[1], O_RDONLY);
/* 4initialize three semaphores */
Sem_init(&shared.mutex, 0, 1);
Sem_init(&shared.nempty, 0, NBUFF);
Sem_init(&shared.nstored, 0, 0);
/* 4one producer thread, one consumer thread */
Set_concurrency(2);
Pthread_create(&tid_produce, NULL, produce, NULL); /* reader thread */
Pthread_create(&tid_consume, NULL, consume, NULL); /* writer thread */
Pthread_join(tid_produce, NULL);
Pthread_join(tid_consume, NULL);
Sem_destroy(&shared.mutex);
Sem_destroy(&shared.nempty);
Sem_destroy(&shared.nstored);
exit(0);
}
示例13: main
int main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if (argc != 3)
err_quit("Usage: prodcons2 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);
Pthread_setconcurrency(nthreads);
// start all the producer threads
for (i = 0; i < nthreads; i++)
{
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
//wait for all the producer threads
for (i = 0; i < nthreads; i++)
{
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
//start then wait for the consumer thread
Pthread_create(&tid_consume, NULL, consume, NULL);
Pthread_join(tid_consume, NULL);
exit(0);
}
示例14: main
/* include main */
int
main(int argc, char **argv)
{
int i, nproducers, nconsumers, countp[MAXNTHREADS], countc[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS];
if (argc != 4)
err_quit("usage: prodcons6 <#items> <#producers> <#consumers>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nproducers = min(atoi(argv[2]), MAXNTHREADS);
nconsumers = min(atoi(argv[3]), MAXNTHREADS);
Set_concurrency(nproducers + nconsumers);
/* 4create all producers and one consumer */
for (i = 0; i < nproducers; i++) {
countp[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &countp[i]);
}
for (i = 0; i < nconsumers; i++) {
countc[i] = 0;
Pthread_create(&tid_consume[i], NULL, consume, &countc[i]);
}
/* wait for all producers and the consumer */
for (i = 0; i < nproducers; i++) {
Pthread_join(tid_produce[i], NULL);
printf("countp[%d] = %d\n", i, countp[i]);
}
for (i = 0; i < nconsumers; i++) {
Pthread_join(tid_consume[i], NULL);
printf("countc[%d] = %d\n", i, countc[i]);
}
exit(0);
}
示例15: main
int main(int argc, char **argv)
{
pthread_t tidA, tidB;
Pthread_create(&tidA, NULL, &doit, NULL);
Pthread_create(&tidB, NULL, &doit, NULL);
/* 4wait for both threads to terminate */
Pthread_join(tidA, NULL);
Pthread_join(tidB, NULL);
exit(0);
}