void oops()
{
  int some_local_state=0;
  func my_func(some_local_state);
  std::thread my_thread(my_func);
  my_thread.detach();
}
示例#2
0
void parallel_while(Input& input, const Size_input& size_input, const Fork_input& fork_input,
                    const Set_in_env& set_in_env, const Body& body) {
#if defined(SEQUENTIAL_ELISION)
  set_in_env(input);
  while (size_input(input) > 0)
    body(input);
#elif defined(USE_CILK_RUNTIME) && defined(__PASL_CILK_EXT)
  parallel_while_cilk_rec(input, size_input, fork_input, set_in_env, body);
#else
  auto b = [&] (Input& state) {
    Input input;
    set_in_env(input);
    input.swap(state);
    while (size_input(input) > 0) { // TODO: replace "size_input(input)" with "should_continue(input)"
      body(input);
      input.swap(state);
      yield();
      input.swap(state);
    }
  };
  using thread_type = parallel_while_base<typeof(b), Input, Size_input, Fork_input, Set_in_env>;
  multishot* join = my_thread();
  thread_type* thread = new thread_type(b, size_input, fork_input, set_in_env, join);
  set_in_env(input);
  input.swap(thread->state);
  join->finish(thread);
#endif
}
示例#3
0
void start_thread() {
    std::vector<int> my_vector(100, 0);
    auto print_vector = std::bind(print_values, std::ref(my_vector));
    std::thread my_thread(print_vector);
    
    if (my_thread.joinable())
        my_thread.detach();
}
示例#4
0
int
main( int argc, char **argv )
{
	pthread_t *td = NULL;
	long n;

	tests_quiet( argc, argv );	/*Set TESTS_QUIET variable */

	if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
		program_time = 6;
	if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
		threshold = 20000000;
	if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
		num_threads = 3;

	td = malloc((num_threads+1) * sizeof(pthread_t));
	if (!td)
		test_fail( __FILE__, __LINE__, "td malloc failed", 1 );

	printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
			program_time, threshold, num_threads );

	if ( PAPI_library_init( PAPI_VER_CURRENT ) != PAPI_VER_CURRENT )
		test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );

	if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
		 PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );

	if ( pthread_key_create( &key, NULL ) != 0 )
		test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );

	gettimeofday( &start, NULL );

	for ( n = 1; n <= num_threads; n++ ) {
		if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
			test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
	}

	my_thread( ( void * ) 0 );

	/* wait for all the threads */
	for ( n = 1; n <= num_threads; n++ ) {
	  	if ( pthread_join( td[n], NULL))
			test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
	}

	free(td);

	printf( "done\n" );

	test_pass( __FILE__, NULL, 0 );
	return ( 0 );
}
int main(int argc, char* argv[]) {
    Radio my_radio("FM");
    auto radio_toggle = std::bind(toggle, std::ref(my_radio));
    std::thread my_thread(radio_toggle);
    
    if (my_thread.joinable())
        my_thread.join();
    
    std::cout << my_radio.get_band() << std::endl;
    
    return 0;
}
示例#6
0
void fork2(const Exp1& exp1, const Exp2& exp2) {
#if defined(SEQUENTIAL_ELISION)
  exp1();
  exp2();
#elif defined(USE_CILK_RUNTIME)
  cilk_spawn exp1();
  exp2();
  cilk_sync;
#else
  my_thread()->fork2(new_multishot_by_lambda(exp1),
                     new_multishot_by_lambda(exp2));
#endif
}
示例#7
0
int main()
{
	int someLocalState = 0;

	func f(someLocalState);
	std::thread my_thread(f); //thread starts here
	my_thread.detach(); //don't wait to finish

	std::cout << "Exiting program..." << std::endl;

	//you will see that the thread doesn't terminate

	return 0;
}
int main(int argc, char **argv)
{
  ros::init(argc, argv, "calibrate_Transporter");
  calibrate_Transporter transporter;
  ros::NodeHandle n;

  signal(SIGINT,quit);

  boost::thread my_thread(boost::bind(&calibrate_Transporter::mainMenu, &transporter));
  
  ros::spin();

  my_thread.interrupt() ;
  my_thread.join() ;  
  return(0);
}
int main(int argc, char** argv)
{
    ros::init(argc, argv, "keyboard_teleop");
    RobotTeleop keyboard_teleop;
    ros::NodeHandle n;

    //std::cout<<"qt funziona";

    signal(SIGINT,quit);

    boost::thread my_thread(boost::bind(&RobotTeleop::keyLoop, &keyboard_teleop));
    ros::Timer timer = n.createTimer(ros::Duration(0.01), boost::bind(&RobotTeleop::watchdog, &keyboard_teleop));
    ros::spin();
    my_thread.interrupt() ;
    my_thread.join() ;
    return(0);
}
示例#10
0
void parallel_while(const Body& body) {
  using input_type = struct { };
  auto size_fct = [] (input_type&) {
    return 2;
  };
  auto fork_fct = [] (input_type&, input_type&) {
  };
  auto set_fct = [] (input_type&) {
  };
  auto b = [&] (input_type&) {
    body();
  };
  using thread_type = parallel_while_base<typeof(b), input_type, typeof(size_fct), typeof(fork_fct), typeof(set_fct)>;
  multishot* join = my_thread();
  thread_type* thread = new thread_type(b, size_fct, fork_fct, set_fct, join);
  join->finish(thread);
}
int main(int argc, char** argv)
{
  ros::init(argc, argv, "aqua_keyboard_teleop");
  TurtlebotTeleop turtlebot_teleop;
  ros::NodeHandle n;

  signal(SIGINT,quit);

  boost::thread my_thread(boost::bind(&TurtlebotTeleop::keyLoop, &turtlebot_teleop));
  
  ros::Timer timer = n.createTimer(ros::Duration(0.1), boost::bind(&TurtlebotTeleop::watchdog, &turtlebot_teleop));

  ros::spin();

  my_thread.interrupt() ;
  my_thread.join() ;
      
  return(0);
}
示例#12
0
void reception::procReception() {
    QByteArray datagram;
    ostringstream oss;
    datagram.resize(soc->pendingDatagramSize());
    soc->readDatagram(datagram.data(),datagram.size(), &hostAddr, &hostPort);
    oss << datagram.data();
    cout << datagram.size() << endl;

    char * passage = (char *)malloc(sizeof(char)*datagram.size()+1);

    for (int i = 0; i < datagram.size(); ++i)
    {
        passage[i] = datagram.data()[i];
    }
    passage[datagram.size()]='\0';

    boost::thread my_thread(&reception::traitement, boost::ref(*this), passage, datagram.size());

    usleep(50);

}
示例#13
0
int main(int argc, char** argv)
{
  // initialize ROS and the node
  ros::init(argc, argv, "jaco_key_teleop");

  // initialize the keyboard controller
  jaco_key_teleop key_controller;
  ros::NodeHandle n;

  // setup the SIGINT signal for exiting
  signal(SIGINT, shutdown);

  // setup the watchdog and key loop in a thread
  boost::thread my_thread(boost::bind(&jaco_key_teleop::loop, &key_controller));
  ros::Timer timer = n.createTimer(ros::Duration(0.1), boost::bind(&jaco_key_teleop::watchdog, &key_controller));
  ros::spin();

  // wait for everything to end
  my_thread.interrupt();
  my_thread.join();

  return EXIT_SUCCESS;
}
示例#14
0
int main(int argc, char** argv)
{
  ros::init(argc, argv, "quadcopter_teleop");
  if(!ros::master::check()){
    printf("ROS master doesn't exist\n");
    return(0);
  }
  
  signal(SIGINT,quit);
  
  QuadcopterTeleop quadcopter_teleop(kfd);
  ros::NodeHandle node("~");
  ros::Subscriber targetInfo=node.subscribe("/vrep/quadtargetpose",1,&QuadcopterTeleop::targetCallback,&quadcopter_teleop);
  printf("Subscribed to quadcopter pose data\n");
  	
  boost::thread my_thread(boost::bind(&QuadcopterTeleop::keyLoop, &quadcopter_teleop));
  
  ros::spin();

  my_thread.interrupt() ;
  my_thread.join() ;
      
  return(0);
}
示例#15
0
int main(int , char**)
{

    std::cout << "Starting hard_work, ready?";
    std::cin.ignore(); 
    std::thread my_thread(hard_work);
    my_thread.join();

    std::cout << "Starting background_task, ready?";
    std::cin.ignore();
    background_task bt;
    bt();
    std::thread my_thread2(bt);
    //std::thread my_thread2({background_task()});
    my_thread2.join();

    std::cout << "Starting lambda, ready?";
    std::cin.ignore();
    std::thread my_thread3(
        []() 
        {
            hard_work();
            more_hard_work_after_hard_work();
        });
    my_thread3.join();

    std::cout << "ready to crash?";
    std::cin.ignore();
    {
        std::thread my_thread(
        []() 
        {
            hard_work();
            more_hard_work_after_hard_work();
        });
        my_thread.detach();
    }

    {
        std::cout << "ready to pass arguments?" << std::endl;
        std::cin.ignore();
        int a = 42;
        std::thread my_thread(foo, a, "soy un thread!");
        std::string str = "soy otro thread";
        std::thread my_thread2([](int a1, const std::string& a2) { std::cout << "t2: a1=" << a1 << " a2=" << a2 << std::endl; }, a, str);
        std::thread my_thread2_capture([&]() { std::cout << "t2 capturing: a1=" << a << " a2=" << str << std::endl; });
        background_task_with_args bt1;
        std::thread my_thread3(bt1,a, "casi el final...");
        background_task_with_args_in_ctor bt2(a, "el final del todo");
        std::thread my_thread4(bt2);

        my_thread.join();
        my_thread2.join();
        my_thread2_capture.join();
        my_thread3.join();
        my_thread4.join();

    }
    {

        //std::cout << "ready to pass arguments (oops)?" << std::endl;
        //std::cin.ignore();
        //ooops(1000);
        //std::cin.ignore();
    }
    {
        std::unique_ptr<big_object> bo(new big_object);
        bo->load("machodedatos.raw");
        //std::thread process_thread(process_big_object, std::move(bo));
        //process_thread.join();
    }
    {
        std::thread r_thread = return_thread();
        join_thread(std::move(r_thread));
    }
    {
        std::cout << "ready to pass arguments with ScopedThread?" << std::endl;
        std::cin.ignore();
        int a = 42;
        ScopedThread my_thread(std::thread(foo, a, "soy un thread!"));
        std::string str = "soy otro thread";
        ScopedThread my_thread2(std::thread([](int a1, const std::string& a2) { std::cout << "t2: a1=" << a1 << " a2=" << a2 << std::endl; }, a, str));
        ScopedThread my_thread3(std::thread([&]() { std::cout << "t2 capturing: a1=" << a << " a2=" << str << std::endl; }));
        background_task_with_args bt1;
        ScopedThread my_thread4(std::thread(bt1,a, "casi el final..."));
        background_task_with_args_in_ctor bt2(a, "el final del todo");
        ScopedThread my_thread5((std::thread(bt2)));
    }

    {
        std::cout << "ready to count cpus?" << std::endl;
        std::cin.ignore();
        std::cout << "num of cpus:" << std::thread::hardware_concurrency() << std::endl;
    }

    {
        std::cout << "ready to check ids?" << std::endl;
        std::cin.ignore();
        std::cout << "main thread id=" << std::this_thread::get_id() << std::endl;
        std::thread t(check_id);
        std::thread::id id = t.get_id();
        t.join();
        std::cout << "thread id=" << id << std::endl;
    }

    {
        std::cout << "ready to fill and find in a list?" << std::endl;
        std::cin.ignore();
        auto producer_function = [](unsigned int numelements)
        {
            for(unsigned int i=0;i<numelements;i++)
            {
                add_to_list(i);
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
            }
        };
        ScopedThread thread_producer1(std::thread(producer_function,100));
        ScopedThread thread_producer2(std::thread(producer_function,100));
        auto finder_function = [](unsigned int value_to_find) 
        {
            auto attempts = 0u;
            bool found = false;
            while(!found && attempts < 100)
            {
                found = contains_in_list(value_to_find);
                attempts++;
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
            }
            if(found)
                std::cout << std::this_thread::get_id() << ": Found! after " << attempts << " attempts" << std::endl;
            else
                std::cout << std::this_thread::get_id() << ": not found! :(" << std::endl;
        };
        ScopedThread thread_finder1(std::thread(finder_function,88));
        ScopedThread thread_finder2(std::thread(finder_function,101));
    }
    {

        std::cout << "ready to crush a stack?" << std::endl;
        std::cin.ignore();
        std::stack<int> s;
        s.push(42);
        if(!s.empty())
        {
            int const value = s.top();
            s.pop();
            foo(value, "do_something");
        }
        std::atomic<int> producersWorking(0);
        std::atomic<bool> started(false);
        ThreadSafeStack<int> ts;
        auto producer_function = [&started, &producersWorking](ThreadSafeStack<int>& ts, unsigned int numelements)
        {
            producersWorking++;
            started = true;
            for(unsigned int i=0;i<numelements;i++)
            {
                ts.push(i);
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
            }
            producersWorking--;
        };
        auto consumer_function = [&started, &producersWorking](ThreadSafeStack<int>& ts)
        {
            while (!started) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); }
            while(producersWorking > 0)
            {
                int value;
                if (ts.pop(value))
                {
                    std::cout << std::this_thread::get_id() << ":popped value=" << value << std::endl;
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                }
            }
            std::cout << std::this_thread::get_id() << ":stack is empty" << std::endl;
        };
        ScopedThread thread_producer1(std::thread(producer_function,std::ref(ts),100));
        ScopedThread thread_producer2(std::thread(producer_function,std::ref(ts),100));
        ScopedThread thread_producer3(std::thread(producer_function, std::ref(ts), 100));
        ScopedThread thread_producer4(std::thread(producer_function, std::ref(ts), 100));
        ScopedThread thread_consumer1(std::thread(consumer_function,std::ref(ts)));
        ScopedThread thread_consumer2(std::thread(consumer_function,std::ref(ts)));
        ScopedThread thread_consumer3(std::thread(consumer_function, std::ref(ts)));
    }

    return EXIT_SUCCESS;
}
示例#16
0
void finish(const Body& body) {
  multishot* join = my_thread();
  multishot* thread = new_multishot_by_lambda([&] { body(join); });
  join->finish(thread);
}
示例#17
0
void parallel_while_cas_ri(Input& input, const Size_input& size_input, const Fork_input& fork_input,
                           const Set_in_env& set_in_env, const Body& body) {
#if defined(SEQUENTIAL_ELISION)
  parallel_while(input, size_input, fork_input, set_in_env, body);
#elif defined(USE_CILK_RUNTIME) && defined(__PASL_CILK_EXT)
  parallel_while(input, size_input, fork_input, set_in_env, body);
#else
  using request_type = worker_id_t;
  const request_type Request_blocked = -2;
  const request_type Request_waiting = -1;
  using answer_type = enum {
    Answer_waiting,
    Answer_transfered
  };
  data::perworker::base<Input> frontier;
  data::perworker::base<std::atomic<request_type>> request;
  data::perworker::base<std::atomic<answer_type>> answer;
  data::perworker::counter::cbase<long> counter;
  worker_id_t leader_id = threaddag::get_my_id();
  msg([&] { std::cout << "leader_id=" << leader_id << std::endl; });
  frontier.for_each([&] (worker_id_t, Input& f) {
    set_in_env(f);
  });
  request.for_each([&] (worker_id_t i, std::atomic<request_type>& r) {
    request_type t = (i == leader_id) ? Request_waiting : Request_blocked;
    r.store(t);
  });
  answer.for_each([] (worker_id_t, std::atomic<answer_type>& a) {
    a.store(Answer_waiting);
  });
  counter.init(0);
  std::atomic<bool> is_done(false);
  auto b = [&] {
    worker_id_t my_id = threaddag::get_my_id();
    scheduler_p sched = threaddag::my_sched();
    multishot* thread = my_thread();
    int nb_workers = threaddag::get_nb_workers();
    Input my_frontier;
    set_in_env(my_frontier); // probably redundant
    if (my_id == leader_id) {
      counter++;
      my_frontier.swap(input);
    }
    msg([&] { std::cout << "entering my_id=" << my_id << std::endl; });
    long sz;
    bool init = (my_id != leader_id);
    while (true) {
      if (init) {
        init = false;
        goto acquire;
      }
      // try to perform some local work
      while (true) {
        thread->yield();
        if (is_done.load())
          return;
        sz = (long)size_input(my_frontier);
        if (sz == 0) {
          counter--;
          msg([&] { std::cout << "decr my_id=" << my_id << " sum=" << counter.sum() << std::endl; });
          break;
        } else { // have some work to do
          body(my_frontier);
          // communicate
          msg([&] { std::cout << "communicate my_id=" << my_id << std::endl; });
          request_type req = request[my_id].load();
          assert(req != Request_blocked);
          if (req != Request_waiting) {
            worker_id_t j = req;
            if (size_input(my_frontier) > 1) {
              counter++;
              msg([&] { std::cout << "transfer from my_id=" << my_id << " to " << j << " sum=" << counter.sum() << std::endl; });
              fork_input(my_frontier, frontier[j]);
            } else {
              msg([&] { std::cout << "reject from my_id=" << my_id << " to " << j << std::endl; });
            }
            answer[j].store(Answer_transfered);
            request[my_id].store(Request_waiting);
          }
        }
      }
      assert(sz == 0);
    acquire:
      sz = 0;
      // reject
      while (true) {
        request_type t = request[my_id].load();
        if (t == Request_blocked) {
          break;
        } else if (t == Request_waiting) {
          request[my_id].compare_exchange_strong(t, Request_blocked);
        } else {
          worker_id_t j = t;
          request[my_id].compare_exchange_strong(t, Request_blocked);
          answer[j].store(Answer_transfered);
        }
      }
      // acquire
      msg([&] { std::cout << "acquire my_id=" << my_id << std::endl; });
      while (true) {
        thread->yield();
        if (is_done.load())
          return;
        answer[my_id].store(Answer_waiting);
        if (my_id == leader_id && counter.sum() == 0) {
          is_done.store(true);
          continue;
        }
        util::ticks::microseconds_sleep(1.0);
        if (nb_workers > 1) {
          worker_id_t id = sched->random_other();
          if (request[id].load() == Request_blocked)
            continue;
          request_type orig = Request_waiting;
          bool s = request[id].compare_exchange_strong(orig, my_id);
          if (! s)
            continue;
          while (answer[my_id].load() == Answer_waiting) {
            thread->yield();
            util::ticks::microseconds_sleep(1.0);
            if (is_done.load())
              return;
          }
          frontier[my_id].swap(my_frontier);
          sz = (long)size_input(my_frontier);
        }
        if (sz > 0) {
          msg([&] { std::cout << "received " << sz << " items my_id=" << my_id << std::endl; });
          request[my_id].store(Request_waiting);
          break;
        }
      }
    }
    msg([&] { std::cout << "exiting my_id=" << my_id << std::endl; });
  };
  parallel_while(b);
#endif
}
示例#18
0
static int exclusive_thread(void *data)
{
    return my_thread(data,0);
}
示例#19
0
void async(const Body& body, multishot* join) {
  multishot* thread = new_multishot_by_lambda(body);
  my_thread()->async(thread, join);
}
示例#20
0
static int normal_thread(void *data)
{
    return my_thread(data,1);
}
示例#21
0
文件: original.c 项目: jvia/os
void *thread_K(void *input) { 
    while (!initialized);
    return my_thread(input); 
} 
示例#22
0
int main() {
    std::thread my_thread([]() {
        std::cout << "Hello, Concurrent World!\n";
    });
    my_thread.join();
}
示例#23
0
文件: original.c 项目: jvia/os
// first thread initializes mutexes 
void *thread_A(void *input) { 
  if (!initialized) {
    initialized=TRUE;
  }  
  return my_thread(input); 
}
示例#24
0
static inline void yield() {
  multishot* thread = my_thread();
  assert(thread != nullptr);
  thread->yield();
}