• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ Pthread_create函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中Pthread_create函数的典型用法代码示例。如果您正苦于以下问题:C++ Pthread_create函数的具体用法?C++ Pthread_create怎么用?C++ Pthread_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Pthread_create函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:woai110120130,项目名称:unix,代码行数:30,代码来源:mycat2.c


示例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);
}
开发者ID:azeemshaikh38,项目名称:MultiThreadedServer,代码行数:35,代码来源:tiny_prodcons.c


示例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;
}
开发者ID:xuyunhuan,项目名称:IPC,代码行数:27,代码来源:prodcons3.c


示例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);
}
开发者ID:CanuxCheng,项目名称:UNP2,代码行数:26,代码来源:buffer.c


示例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);
}
开发者ID:r00tjimmy,项目名称:codeBox,代码行数:26,代码来源:synch_error.c


示例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());
}
开发者ID:wjmelements,项目名称:iu,代码行数:29,代码来源:concat.cpp


示例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;
}
开发者ID:whj2819,项目名称:Hello-World,代码行数:28,代码来源:prodcons2.c


示例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);
}
开发者ID:BigR-Lab,项目名称:CodeRes_Cpp,代码行数:29,代码来源:prodcons4.c


示例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);
}
开发者ID:piaoyimq,项目名称:CppSpace,代码行数:29,代码来源:deadlock.c


示例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);
}
开发者ID:CanuxCheng,项目名称:UNP2,代码行数:28,代码来源:prodcons.c


示例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;
}
开发者ID:xuyunhuan,项目名称:IPC,代码行数:28,代码来源:prodcons1.c


示例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);
}
开发者ID:piaoyimq,项目名称:CppSpace,代码行数:28,代码来源:mycat2.c


示例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);
}
开发者ID:crazy-canux,项目名称:cAPI,代码行数:32,代码来源:prodcons.c


示例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);
}
开发者ID:kissghosts,项目名称:uh-courses,代码行数:36,代码来源:prodcons6.c


示例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);
}
开发者ID:feng1o,项目名称:my_unp,代码行数:13,代码来源:thread-error-554.c


示例16: main

int main(int argc, char **argv)
{
	ifstream fin;
	fin.open(argv[1]);
	ms.InitMS(fin);
	cout<<ms;
    ms.Setsocket();
    int sockfd_tcp, sockfd_udp, maxfd;
    struct sockaddr_in srvaddr;
    fd_set allset, rset;
    socklen_t len = sizeof(struct sockaddr_in);
    sockfd_tcp = ms.Getsockfd_tcp();
    sockfd_udp = ms.Getsockfd_udp();
    maxfd = max(sockfd_tcp, sockfd_udp) + 1;
    FD_ZERO(&allset);
    FD_SET(sockfd_tcp, &allset);
    FD_SET(sockfd_udp, &allset);
    char buf[MAXLINE];
    int i;
    pthread_t tid_probe;
    Pthread_create(&tid_probe, NULL, &probing, NULL); //create an another thread to timely probe the time_sheet in order to find the failed server
    while(1)
    {
        Signal(SIGINT, sig_int_handle);
        Signal(SIGTERM, sig_term_handle);
        if (sigsetjmp(jmpbuf, 1)!=0 || sigsetjmp(jmpbuf, 2)!=0) //if SIGINT or SIGTERM signal caught, master close all sockets and exits
        {
            ms.Closesockets();
            cout<<"Master exits, all sockets are closed"<<endl;
            exit(1);
        }
        rset = allset;
        if ((Select(maxfd, &rset, NULL, NULL, NULL)) < 1) //master listen to its udp socket to receive ping from server
            continue;
        if (FD_ISSET(sockfd_udp, &rset))
        {
            i = recvfrom(sockfd_udp, buf, MAXLINE, 0, (SA*)&srvaddr, &len);
            if (i < 0)
            {
                cerr<<"recv error"<<endl;
                continue;
            }
            else
            {
                pthread_t tid;
                Pthread_create(&tid, NULL, &handle_ping, (void *)buf); //when received server ping, handle it
            }
        }
    }
	return 1;
}
开发者ID:fydeng,项目名称:cse535-Asynchronous-Systems,代码行数:51,代码来源:master.cpp


示例17: main

int main() 
{
    pthread_t tid1, tid2;

    Pthread_create(&tid1, NULL, count, NULL);
    Pthread_create(&tid2, NULL, count, NULL);
    Pthread_join(tid1, NULL);
    Pthread_join(tid2, NULL);

    if (cnt != (unsigned)NITERS*2)
	printf("BOOM! cnt=%d\n", cnt);
    else
	printf("OK cnt=%d\n", cnt);
    exit(0);
}
开发者ID:09zwcbupt,项目名称:csapp,代码行数:15,代码来源:badcnt.c


示例18: main

int main(int argc, char *argv[]){
    if(argc!=2){
        fprintf(stderr, "parameter useage: theread<value>\n");
        exit(1);
    }
    loops = atoi(argv[1]);
    pthread_t p1,p2;
    printf("Initial value of Counter : %d\n",counter);
    Pthread_create(&p1,NULL,worker,NULL);
    Pthread_create(&p2,NULL,worker,NULL);
    Pthread_join(p1,NULL);
    Pthread_join(p2,NULL);
    printf("Final value of Counter : %d\n",counter);
    return 0;
}
开发者ID:luckyharryji,项目名称:Operating-Systems-Reading,代码行数:15,代码来源:thread.c


示例19: main

int
main(int argc, char **argv)
{
	int				listenfd_e,listenfd_t, *iptr,*jptr;
	char port_echo[6],port_time[6];
	pthread_t		tid;
	socklen_t		addrlen, len;
	struct sockaddr	*cliaddr;
	int			maxfdp1;
	fd_set		rset;
	
	strcpy(port_echo,"9870");  //assumed to be the well-known port number for echo service
	strcpy(port_time,"9880");  //assumed to be the well-known port number for time service

    listenfd_e = Tcp_listen(NULL, port_echo, &addrlen); //set up listening socket for echo service
	listenfd_t= Tcp_listen(NULL, port_time, NULL);  //set up listening socket for time service
	
	cliaddr = Malloc(addrlen);
	FD_ZERO(&rset);

	for ( ; ; ) {
		len = addrlen;
		iptr = Malloc(sizeof(int));
		jptr = Malloc(sizeof(int));

		FD_SET(listenfd_e, &rset);
		FD_SET(listenfd_t, &rset);
		maxfdp1 = max(listenfd_e,listenfd_t) + 1;
	//	printf("Entering main select...");
		Select(maxfdp1, &rset, NULL, NULL, NULL);
	//	printf("Exited main select");

		if (FD_ISSET(listenfd_e, &rset)) 
		{	// Listening socket for echo service is readable 
          	 *iptr = Accept(listenfd_e, cliaddr, &len);
		     Pthread_create(&tid, NULL, &doecho, iptr);
		}

		
		if (FD_ISSET(listenfd_t, &rset)) 
		{	// Listening socket for time service is readable
	          	*jptr = Accept(listenfd_t, cliaddr, &len);
				Pthread_create(&tid, NULL, &dotime, jptr);
		}


	}
}
开发者ID:parikshit84,项目名称:Network-programming,代码行数:48,代码来源:tcpechotimesrv.c


示例20: main

int
main(int argc, char **argv)
{
	int		i, nthreads;
	pthread_t	tid[MAXNTHREADS];

	if (argc != 3)
		err_quit("usage: incr_pxsem1 <#loops> <#threads>");
	nloop = atoi(argv[1]);
	nthreads = min(atoi(argv[2]), MAXNTHREADS);

		/* 4initialize memory-based semaphore to 0 */
	Sem_init(&shared.mutex, 0, 0);

		/* 4create all the threads */
	Set_concurrency(nthreads);
	for (i = 0; i < nthreads; i++) {
		Pthread_create(&tid[i], NULL, incr, NULL);
	}
		/* 4start the timer and release the semaphore */
	Start_time();
	Sem_post(&shared.mutex);

		/* 4wait for all the threads */
	for (i = 0; i < nthreads; i++) {
		Pthread_join(tid[i], NULL);
	}
	printf("microseconds: %.0f usec\n", Stop_time());
	if (shared.counter != nloop * nthreads)
		printf("error: counter = %ld\n", shared.counter);

	exit(0);
}
开发者ID:BigR-Lab,项目名称:CodeRes_Cpp,代码行数:33,代码来源:incr_pxsem1.c



注:本文中的Pthread_create函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ Pthread_mutex_lock函数代码示例发布时间:2022-05-30
下一篇:
C++ Pt2di函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap