当前位置: 首页>>代码示例>>C++>>正文


C++ pthread_cancel函数代码示例

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


在下文中一共展示了pthread_cancel函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: do_one_test

static int
do_one_test (void)
{
  in_sh_body = 0;
  cleanups = 0;
  if (pipe (fd) != 0 || pipe (fd + 2) != 0)
    {
      puts ("pipe failed");
      return 1;
    }

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent thread: barrier_wait failed");
      return 1;
    }

  sleep (1);

  r = pthread_kill (th, SIGHUP);
  if (r)
    {
      errno = r;
      printf ("pthread_kill failed %m\n");
      return 1;
    }

  while (in_sh_body == 0)
    sleep (1);

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      return 1;
    }

  void *ret;
  if (pthread_join (th, &ret) != 0)
    {
      puts ("join failed");
      return 1;
    }

  if (ret != PTHREAD_CANCELED)
    {
      puts ("result is wrong");
      return 1;
    }

  if (cleanups != 0x1234L)
    {
      printf ("called cleanups %lx\n", cleanups);
      return 1;
    }

  /* The pipe closing must be issued after the cancellation handling to avoid
     a race condition where the cancellation runs after both pipe ends are
     closed.  In this case the read syscall returns EOF and the cancellation
     must not act.  */
  close (fd[0]);
  close (fd[1]);
  close (fd[2]);
  close (fd[3]);

  return 0;
}
开发者ID:eva-oss,项目名称:glibc-linaro,代码行数:74,代码来源:tst-cancel20.c

示例2: limitMonitorJoin

static void limitMonitorJoin(void)
{
    pthread_cancel(limit_monitor.th);
    pthread_join(limit_monitor.th, NULL);
    pthread_mutex_destroy(&limit_monitor.lock);
}
开发者ID:danfis,项目名称:maplan,代码行数:6,代码来源:search.c

示例3: if

int16 XSERDPort::open(uint16 config)
{
	// Don't open NULL name devices
	if (device_name == NULL)
		return openErr;

	// Init variables
	io_killed = false;
	quitting = false;

	// Open port, according to the syntax of the path
	if (device_name[0] == '|') {
		// Open a process via ptys
		if (!open_pty())
			goto open_error;
	}
	else if (!strcmp(device_name, "midi")) {
		// MIDI:  not yet implemented
		return openErr;
	}
	else {
		// Device special file
		fd = ::open(device_name, O_RDWR);
		if (fd < 0)
			goto open_error;

#if defined(__linux__)
		// Parallel port?
		struct stat st;
		if (fstat(fd, &st) == 0)
			if (S_ISCHR(st.st_mode))
				protocol = ((MAJOR(st.st_rdev) == LP_MAJOR) ? parallel : serial);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
		// Parallel port?
		struct stat st;
		if (fstat(fd, &st) == 0)
			if (S_ISCHR(st.st_mode))
				protocol = (((st.st_rdev >> 16) == 16) ? parallel : serial);
#endif
	}

	// Configure port for raw mode
	if (protocol == serial || protocol == pty) {
		if (tcgetattr(fd, &mode) < 0)
			goto open_error;
		cfmakeraw(&mode);
		mode.c_cflag |= HUPCL;
		mode.c_cc[VMIN] = 1;
		mode.c_cc[VTIME] = 0;
		tcsetattr(fd, TCSAFLUSH, &mode);
	}
	configure(config);

	// Start input/output threads
	input_thread_cancel = false;
	output_thread_cancel = false;
	if (sem_init(&input_signal, 0, 0) < 0)
		goto open_error;
	if (sem_init(&output_signal, 0, 0) < 0)
		goto open_error;
	input_thread_active = (pthread_create(&input_thread, &thread_attr, input_func, this) == 0);
	output_thread_active = (pthread_create(&output_thread, &thread_attr, output_func, this) == 0);
	if (!input_thread_active || !output_thread_active)
		goto open_error;
	return noErr;

open_error:
	if (input_thread_active) {
		input_thread_cancel = true;
#ifdef HAVE_PTHREAD_CANCEL
		pthread_cancel(input_thread);
#endif
		pthread_join(input_thread, NULL);
		sem_destroy(&input_signal);
		input_thread_active = false;
	}
	if (output_thread_active) {
		output_thread_cancel = true;
#ifdef HAVE_PTHREAD_CANCEL
		pthread_cancel(output_thread);
#endif
		pthread_join(output_thread, NULL);
		sem_destroy(&output_signal);
		output_thread_active = false;
	}
	if (fd > 0) {
		::close(fd);
		fd = -1;
	}
	return openErr;
}
开发者ID:habnabit,项目名称:macemu,代码行数:91,代码来源:serial_unix.cpp

示例4: discovery_stop

void discovery_stop(discovery_t* self) {
  assert(self);

  pthread_cancel(self->thread);
}
开发者ID:TeamElevate,项目名称:network,代码行数:5,代码来源:discovery.c

示例5: spi_console_exit

static void spi_console_exit(int sig)
{
	DEBUG_PRINT("Get SIGINT.\n");
	pthread_cancel(spi_bridge.read_mcu_tidp);
	pthread_cancel(spi_bridge.read_stdin_tidp);
}
开发者ID:SchumyHao,项目名称:openwrt-packages,代码行数:6,代码来源:main.c

示例6: vlc_cancel

/* Marks a thread as cancelled. Next time the target thread reaches a
 * cancellation point (while not having disabled cancellation), it will
 * run its cancellation cleanup handler, the thread variable destructors, and
 * terminate. vlc_join() must be used afterward regardless of a thread being
 * cancelled or not. */
void vlc_cancel (vlc_thread_t thread_id)
{
    pthread_cancel (thread_id);
}
开发者ID:chucolin,项目名称:vlc,代码行数:9,代码来源:thread.c

示例7: main


//.........这里部分代码省略.........

	while (1) {
		/* Reset buffer */
		memset(buf, 0, sizeof(buf));

		/* Receive DNS UDP request */
		recv_size = dns_udp_recv_query(udp_client_sock, buf, sizeof(buf),
				(struct sockaddr *) &saddr);
		if (recv_size < 0) {
			/* At this point... better clean exit */
			goto error;
		}

		/* Change buf to fit DNS TCP request */
		forge_tcp_request(buf, &recv_size);

		/*
		 * Make the DNS query on the TCP transport layer either using inprocess
		 * SSH or using an already created TCP tunnel (Ex: ssh -L ... on the
		 * command line).
		 */
		if (ssh_host != NULL) {
			/*
			 * Round robin option. We must create the ssh tunnel and close it
			 * at each DNS query.
			 */
			if (opt_rr_dns) {
				if ((open_dns_count - dns_index) == 0) {
					dns_index = 0;
				}

				/*
				 * We don't create a new SSH channel for the first run since it
				 * was created before the main loop hence the reason for
				 * dns_index being -1.
				 */
				if (dns_index != -1) {
					dns_ip = open_dns_list[dns_index];
					/*
					 * Create new SSH direct tcp channel. We don't care about
					 * the return value because on error, the next call will
					 * handle it.
					 */
					libssh2_channel_free(ssh_info->channel);
					ret = ssh_setup_tunnel(ssh_info, dns_ip);
					if (ret < 0) {
						continue;
					}
					DBG("Round robin DNS %s", dns_ip);
				}
				dns_index++;
			}
			recv_size = dig_ssh_request(ssh_info, buf, sizeof(buf),
					recv_size);
			if (ret < 0) {
				do {
					sleep(DEFAULT_RECONNECT_TIME);
					ret = ssh_setup_tunnel(ssh_info, dns_ip);
				} while (ret < 0);
				continue;
			}
		} else {
			recv_size = dig_tcp_request(tcp_dns_sock, buf, sizeof(buf),
					recv_size);
			if (recv_size < 0) {
				/* Connect to forward TCP server */
				do {
					/* Retry every DEFAULT_RECONNECT_TIME sec */
					sleep(DEFAULT_RECONNECT_TIME);
					tcp_dns_sock = setup_forward_tcp(forward_port, forward_ip);
				} while (tcp_dns_sock < 0);
				continue;
			}
		}

		/* Change buf to fit DNS UDP request */
		forge_udp_request(buf, &recv_size);

		ret = dns_udp_send_reply(udp_client_sock, buf, recv_size,
				(struct sockaddr *) &saddr, sizeof(saddr));
		if (ret < 0) {
			/* UDP client disconnected, continue serving */
			continue;
		}
	}

	/* Not suppose to get here */

error:
	ret = pthread_cancel(log_thread);
	if (ret == 0) {
		pthread_join(log_thread, &status);
	}
	close(tcp_dns_sock);
	close(udp_client_sock);

	cleanup(ret);

	return 0;
}
开发者ID:dgoulet,项目名称:debind,代码行数:101,代码来源:main.c

示例8: msg_thread_stop

/* this stops the logger thread. hard. */
int msg_thread_stop(void)
{
	return(pthread_cancel(log_thread));
}
开发者ID:BackupTheBerlios,项目名称:vermont-svn,代码行数:5,代码来源:msg.c

示例9: myhtml_thread_cancel

myhtml_status_t myhtml_thread_cancel(mythread_t *mythread, mythread_list_t *thr)
{
    pthread_cancel(thr->pth);
    return MyHTML_STATUS_OK;
}
开发者ID:adrianhust,项目名称:myhtml,代码行数:5,代码来源:thread.c

示例10: fullscrape_deinit

void fullscrape_deinit( ) {
  pthread_cancel( thread_id );
}
开发者ID:Gastlag,项目名称:Opentracker,代码行数:3,代码来源:ot_fullscrape.c

示例11: TRACE_BEGIN

void Thread::OnStop()
{
	TRACE_BEGIN( LOG_LVL_INFO );	
	pthread_cancel( mThread );
}
开发者ID:benpayne,项目名称:jhcommon,代码行数:5,代码来源:Thread.cpp

示例12: test_cleanup1

int
test_cleanup1(void)
#endif
{
    int failed = 0;
    int i;
    pthread_t t[NUMTHREADS + 1];

    memset(&pop_count, 0, sizeof(sharedInt_t));

    InitializeCriticalSection(&pop_count.cs);

    assert((t[0] = pthread_self()).p != NULL);

    for (i = 1; i <= NUMTHREADS; i++)
    {
        threadbag[i].started = 0;
        threadbag[i].threadnum = i;
        assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
    }

    /*
     * Code to control or manipulate child threads should probably go here.
     */
    Sleep(500);

    for (i = 1; i <= NUMTHREADS; i++)
    {
        assert(pthread_cancel(t[i]) == 0);
    }

    /*
     * Give threads time to run.
     */
    Sleep(NUMTHREADS * 100);

    /*
     * Standard check that all threads started.
     */
    for (i = 1; i <= NUMTHREADS; i++)
    {
        if (!threadbag[i].started)
        {
            failed |= !threadbag[i].started;
            fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
        }
    }

    assert(!failed);

    /*
     * Check any results here. Set "failed" and only print output on failure.
     */
    failed = 0;
    for (i = 1; i <= NUMTHREADS; i++)
    {
        int fail = 0;
        void* result = (void*)0;

        assert(pthread_join(t[i], &result) == 0);

        fail = (result != PTHREAD_CANCELED);

        if (fail)
        {
            fprintf(stderr, "Thread %d: started %d: result %d\n",
                    i,
                    threadbag[i].started,
                    (int)(size_t)result);
        }
        failed = (failed || fail);
    }

    assert(!failed);

    assert(pop_count.i == NUMTHREADS);

    DeleteCriticalSection(&pop_count.cs);

    /*
     * Success.
     */
    return 0;
}
开发者ID:wbminera1,项目名称:pthread-win32,代码行数:84,代码来源:cleanup1.c

示例13: main

int main()
{
	int num,i = 0;
	char tmp[MAX_TH_NUM];
	times = 1;
	num = 2;
	printf("Usage:\n"
		"\tgo: start test\n"
		"\tabort: cancel thread\n"
		"\tstatus: show test status\n"
		"\texit: exit the program\n"
		"\tthread n: create n threads to test\n"
		"\ttimes n: each mem unit tests n times\n"
		"Default: 1 times and 2 threads\n"
		);
	fflush(stdout);
	while(1)
	{
		printf(">>>");
		fflush(stdout);
		scanf(" %s",tmp);
		if(strcmp(tmp,"times") == 0)
		{
			scanf(" %d",&times);
			continue;
		}
		if(strcmp(tmp,"thread") == 0)
		{
			scanf(" %d",&num);
			continue;
		}
		if(strcmp(tmp,"exit") == 0)
		{
			exit(0);
		}
		if(strcmp(tmp,"go") == 0)
		{
			for(i=0;i<num;i++)
			{
				test_addr[i] = (unsigned long) malloc(MEM_SIZE*sizeof(unsigned char));
				pthread_create(&tid[i],memtest,test_addr[i]);
			}
			continue;
		}
		if(strcmp(tmp,"status") == 0)
		{
			for(i=0;i<num;i++)
			{
				printf("Thread %d :  ",tid[i]);
				switch(pthread_status(tid[i]))
				{
					case -1: 
						printf("is to be created\n");
						break;
					case 0: 
						printf("is running\n");
						break;
					case 1:
						printf("is waiting\n");
						break;
					case 2:
						printf("is waiting\n");
						break;
					case 4:
						printf("is stopped\n");
						pthread_join(tid[i],&result[i]);
						printf("\tTest Addr: %X Result: %d/%d(OK)\n",test_addr[i],result[i],MEM_SIZE);
						break;
					case 5:
						printf("is canceled\n");
						break;
					default:
						printf("no such thread\n");
						break;
				}
			}
			continue;
		}
		if(strcmp(tmp,"abort") == 0)
		{
			for(i=0;i<num;i++)
			{
				if(tid[i])
				{
					pthread_cancel(tid[i]);
				}
			}
			continue;
		}
	}
	
}
开发者ID:guker,项目名称:HIT-OSLab,代码行数:92,代码来源:memtest.c

示例14: cancel_all_threads

static void cancel_all_threads()
{
     int i = 0;
     int wait_listener_time = 10000;
     int wait_for_workers = CHILD_SHUTDOWN_TIMEOUT>5?CHILD_SHUTDOWN_TIMEOUT:5;
     int servers_running;
     /*Cancel listener thread .... */
     /*we are going to wait maximum about 10 ms */
     while (wait_listener_time > 0 && listener_running != 0) {
          /*Interrupt listener if it is waiting for threads or 
             waiting to accept new connections */
#ifdef SINGLE_ACCEPT
          pthread_cancel(listener_thread_id);   /*..... */
#else
          pthread_kill(listener_thread_id, SIGHUP);
#endif
          ci_thread_cond_signal(&free_server_cond);
          ci_usleep(1000);
          wait_listener_time -= 10;
     }
     if (listener_running == 0) {
          ci_debug_printf(5,
                          "Going to wait for the listener thread (pid: %d) to exit!\n",
                          threads_list[0]->srv_id);
          ci_thread_join(listener_thread_id);
          ci_debug_printf(5, "OK, cancelling the listener thread (pid: %d)!\n",
                          threads_list[0]->srv_id);
     }
     else {
          /*fuck the listener! going down ..... */
     }

     /*We are going to interupt the waiting for queue childs.
        We are going to wait threads which serve a request. */
     ci_thread_cond_broadcast(&(con_queue->queue_cond));
     /*wait for a milisecond*/
     ci_usleep(1000);
     servers_running = CI_CONF.THREADS_PER_CHILD;
     while (servers_running && wait_for_workers >= 0) {
         /*child_data->to_be_killed, may change while we are inside this loop*/
         if (child_data->to_be_killed == IMMEDIATELY) {
             CHILD_HALT = 1;
         }
         for (i=0; i<CI_CONF.THREADS_PER_CHILD; i++) {
             if (threads_list[i] != NULL) { /* if the i thread is still alive*/
                 if (!threads_list[i]->running) { /*if the i thread is not running any more*/
                     ci_debug_printf(5, "Cancel server %d, thread_id %lu (%d)\n",
                                     threads_list[i]->srv_id, threads_list[i]->srv_pthread,
                                     i);
                     ci_thread_join(threads_list[i]->srv_pthread);
                     release_thread_i(i);
                     servers_running --;
                 }
                 else if (child_data->to_be_killed == IMMEDIATELY){ 
                     /*The thread is still running, and we have a timeout for waiting 
                       the thread to exit. */
                     if (wait_for_workers <= 2) {
                         ci_debug_printf(5, "Thread %ld still running near the timeout. Try to kill it\n", threads_list[i]->srv_pthread);
                         pthread_kill( threads_list[i]->srv_pthread, SIGTERM);
                     }
                 }
             }/*the i thread is still alive*/
         } /* for(i=0;i< CI_CONF.THREADS_PER_CHILD;i++)*/

         /*wait for 1 second for the next round*/
         ci_usleep(999999);

         /*
           The child_data->to_be_killed may change while we are running this function.
           In the case it has/got the value IMMEDIATELY decrease wait_for_workers:
         */
         if (child_data->to_be_killed == IMMEDIATELY)
             wait_for_workers --;
         
     } /* while(servers_running)*/

     if (servers_running) {
         ci_debug_printf(5, "Not all the servers canceled. Anyway exiting....\n");
     }
     else {
         ci_debug_printf(5, "All servers canceled\n");
         free(threads_list);
     }
}
开发者ID:Shield-Firewall,项目名称:c-icap,代码行数:84,代码来源:mpmt_server.c

示例15: sigemptyset

void *restore_thrd(void *arg) {
	sigset_t sigmask, old_mask;
	int i, sig;

	sigemptyset(&sigmask);
	sigaddset(&sigmask, SIGINT);
	sigaddset(&sigmask, SIGTERM);
	sigaddset(&sigmask, SIGUSR1);
	pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask);

	while (1) {
		sigwait(&sigmask, &sig);

		if ((sig == SIGINT || sig == SIGTERM) && !interrupted) {
			ERROR("Interrupting will restore original " "filesystem!\n");
			ERROR("Interrupt again to quit\n");
			interrupted = TRUE;
			continue;
		}

		/* kill main thread/worker threads and restore */
		set_progressbar_state(FALSE);
		disable_info();

		/* first kill the reader thread */
		pthread_cancel(reader_thread);
		pthread_join(reader_thread, NULL);

		/*
		 * then flush the reader to deflator thread(s) output queue.
		 * The deflator thread(s) will idle
		 */
		queue_flush(to_deflate);

		/* now kill the deflator thread(s) */
		for (i = 0; i < processors; i++)
			pthread_cancel(deflator_thread[i]);
		for (i = 0; i < processors; i++)
			pthread_join(deflator_thread[i], NULL);

		/*
		 * then flush the reader to process fragment thread(s) output
		 * queue.  The process fragment thread(s) will idle
		 */
		queue_flush(to_process_frag);

		/* now kill the process fragment thread(s) */
		for (i = 0; i < processors; i++)
			pthread_cancel(frag_thread[i]);
		for (i = 0; i < processors; i++)
			pthread_join(frag_thread[i], NULL);

		/*
		 * then flush the reader/deflator/process fragment to main
		 * thread output queue.  The main thread will idle
		 */
		seq_queue_flush(to_main);

		/* now kill the main thread */
		pthread_cancel(main_thread);
		pthread_join(main_thread, NULL);

		/* then flush the main thread to fragment deflator thread(s)
		 * queue.  The fragment deflator thread(s) will idle
		 */
		queue_flush(to_frag);

		/* now kill the fragment deflator thread(s) */
		for (i = 0; i < processors; i++)
			pthread_cancel(frag_deflator_thread[i]);
		for (i = 0; i < processors; i++)
			pthread_join(frag_deflator_thread[i], NULL);

		/*
		 * then flush the main thread/fragment deflator thread(s)
		 * to writer thread queue.  The writer thread will idle
		 */
		queue_flush(to_writer);

		/* now kill the writer thread */
		pthread_cancel(writer_thread);
		pthread_join(writer_thread, NULL);

		TRACE("All threads cancelled\n");

		restorefs();
	}
}
开发者ID:OliverGesch,项目名称:epk2extract,代码行数:88,代码来源:restore.c


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