Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. void* WorkerThread(void* args)
  2. {
  3. DoSomeImportantInitialization(); // initialize listen socket and some thread specific stuff
  4.  
  5. while (HasExitConditionBeenSet()==false)
  6. {
  7. listensize = sizeof(listenaddr);
  8. int sock = accept(listensocket, &listenaddr, &listensize);
  9.  
  10. // check if exit condition has been set using thread safe semantics
  11. if (HasExitConditionBeenSet())
  12. {
  13. break;
  14. }
  15.  
  16. if (sock < 0)
  17. {
  18. printf("accept returned %d (errno==%d)n", sock, errno);
  19. }
  20. else
  21. {
  22. HandleNewNetworkCondition(sock, &listenaddr);
  23. }
  24. }
  25.  
  26. DoSomeImportantCleanup(); // close listen socket, close connections, cleanup etc..
  27. return NULL;
  28. }
  29.  
  30. void SignalHandler(int sig)
  31. {
  32. printf("Caught CTRL-Cn");
  33. }
  34.  
  35. void NotifyWorkerThreadToExit(pthread_t thread_handle)
  36. {
  37. // signal thread to exit
  38. }
  39.  
  40. int main()
  41. {
  42. void* ptr_ret= NULL;
  43. pthread_t workerthread_handle = 0;
  44.  
  45. pthread_create(&workerthread, NULL, WorkerThread, NULL);
  46.  
  47. signal(SIGINT, SignalHandler);
  48.  
  49. sleep((unsigned int)-1); // sleep until the user hits ctrl-c
  50.  
  51. printf("Returned from sleep call...n");
  52.  
  53. SetThreadExitCondition(); // sets global variable with barrier that worker thread checks on
  54.  
  55. // this is the function I'm stalled on writing
  56. NotifyWorkerThreadToExit(workerthread_handle);
  57.  
  58. // wait for thread to exit cleanly
  59. pthread_join(workerthread_handle, &ptr_ret);
  60.  
  61. DoProcessCleanupStuff();
  62.  
  63. }
  64.  
  65. // NotifyPipe.h
  66. #ifndef NOTIFYPIPE_H_INCLUDED
  67. #define NOTIFYPIPE_H_INCLUDED
  68.  
  69. class NotifyPipe
  70. {
  71. int m_receiveFd;
  72. int m_sendFd;
  73.  
  74. public:
  75. NotifyPipe();
  76. virtual ~NotifyPipe();
  77.  
  78. int receiverFd();
  79. void notify();
  80. };
  81.  
  82. #endif // NOTIFYPIPE_H_INCLUDED
  83.  
  84. // NotifyPipe.cpp
  85.  
  86. #include "NotifyPipe.h"
  87.  
  88. #include <unistd.h>
  89. #include <assert.h>
  90. #include <fcntl.h>
  91.  
  92. NotifyPipe::NotifyPipe()
  93. {
  94. int pipefd[2];
  95. int ret = pipe(pipefd);
  96. assert(ret == 0); // For real usage put proper check here
  97. m_receiveFd = pipefd[0];
  98. m_sendFd = pipefd[1];
  99. fcntl(m_sendFd,F_SETFL,O_NONBLOCK);
  100. }
  101.  
  102.  
  103. NotifyPipe::~NotifyPipe()
  104. {
  105. close(m_sendFd);
  106. close(m_receiveFd);
  107. }
  108.  
  109.  
  110. int NotifyPipe::receiverFd()
  111. {
  112. return m_receiveFd;
  113. }
  114.  
  115.  
  116. void NotifyPipe::notify()
  117. {
  118. write(m_sendFd,"1",1);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement