35 #include <semaphore.h>
40 pthread_mutex_t
lock = PTHREAD_MUTEX_INITIALIZER;
53 std::cout <<
"Hello Fast Probing - multithread!" << std::endl;
57 if ((socketudp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
59 perror(
"General UDP socket - socket");
72 if ((status == setsockopt(socketudp, IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl))) < 0)
74 perror(
"General UDP socket - setsockopt");
79 if ((status == setsockopt(socketudp, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on))) < 0)
81 perror(
"General UDP socket - setsockopt");
87 struct sockaddr_in src_addr;
88 src_addr.sin_family = AF_INET;
89 src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
90 src_addr.sin_port = htons(src_port);
92 if (bind(socketudp, (
struct sockaddr *)&src_addr,
sizeof(src_addr))<0)
93 perror(
"General UDP socket - bind");
107 std::list<std::string> ips;
109 ips.push_back(
"10.0.12.20");
110 ips.push_back(
"10.0.12.40");
111 ips.push_back(
"10.0.12.60");
112 ips.push_back(
"10.0.12.80");
113 ips.push_back(
"10.0.12.100");
115 int thread_number = 5;
116 int current_thread = 0;
118 std::cout <<
"Main semaphore initialized to " << thread_number << std::endl;
120 sem_init(&
mainsem, 0, thread_number);
132 lOpts.
dst_addr =
new std::string(ips.front());
135 lOpts.
gOpts = option;
137 std::cout <<
"Creating thread " << current_thread+1 <<
" for IP [" << *lOpts.
dst_addr <<
"]" << std::endl;
139 void* pointer = malloc(
sizeof(lOpts));
144 std::cout <<
"ERROR: main thread malloc returned null" << std::endl;
145 pointer = malloc(
sizeof(lOpts));
153 memcpy(pointer, (
void*) &lOpts,
sizeof(lOpts));
158 int rc = pthread_create(&pth, NULL,
send_probe, pointer);
162 std::cout <<
"ERROR: return code from pthread_create() is " << rc << std::endl;
171 pthread_detach(pthread_self());
190 pthread_mutex_lock(&
lock);
196 std::cout <<
"No response obtained from " << *option->
dst_addr << std::endl;
198 pthread_mutex_unlock(&
lock);
203 pthread_detach(pthread_self());